Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WithProxy(...) does not save the mappings to file #549

Closed
martinkra opened this issue Dec 5, 2020 · 18 comments
Closed

WithProxy(...) does not save the mappings to file #549

martinkra opened this issue Dec 5, 2020 · 18 comments
Assignees
Labels

Comments

@martinkra
Copy link

martinkra commented Dec 5, 2020

I've created a console app that runs the server trying to record/capture mappings:

var server = WireMock.Server.WireMockServer.Start(new WireMockServerSettings
{
    Port = 1234,
    StartAdminInterface = true,
    ProxyAndRecordSettings = new ProxyAndRecordSettings
    {
        SaveMapping = true,
        SaveMappingToFile = true
    }
}); ;

server
    .Given(Request.Create().UsingGet())
    .RespondWith(Response.Create()
        .WithProxy(new ProxyAndRecordSettings
        {
            Url = "https://github.com/",
            SaveMapping = true,
            SaveMappingToFile = true
        }));

Console.WriteLine("Press any key to stop the server");
Console.ReadLine();

The server works fine, e.g. when accessing localhost:1234/test1 I get the data from https://github.com/test1. The problem is that I am unable to find the way to get the recorded mapping... No files are created. What am I missing?

@StefH StefH added the question label Dec 5, 2020
@StefH
Copy link
Collaborator

StefH commented Dec 5, 2020

Files are created in the folder {framework}/__admin/mappings.

This folder is located in the same place where the binaries are located:
image

@martinkra
Copy link
Author

In my case the folder is netcoreapp3.1/__admin mappings are not created.
image

I've tried with .Net Core 2.1, didn't help.
image

@StefH
Copy link
Collaborator

StefH commented Dec 5, 2020

Hello @martinkra, now I see that you are defining proxy settings in two places.

Try like this example: https://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.Console.Proxy.NETCoreApp2/Program.cs

@martinkra
Copy link
Author

martinkra commented Dec 5, 2020

Your hint 'defining proxy settings in two places' helped, example too, thank you.

I figured that the problem was with defining proxy settings for a specific request. Recording settings seem to work only when configured during server start and should not be touched later. The code that works:

            var server = WireMock.Server.WireMockServer.Start(new WireMockServerSettings
            {
                Port = 1234,
                StartAdminInterface = true,
                ProxyAndRecordSettings = new ProxyAndRecordSettings
                {
                    Url = "http:https://postman-echo.com/post",
                    SaveMapping = true,
                    SaveMappingToFile = true
                }
            });

            Console.WriteLine("Press any key to stop the server");
            Console.ReadLine();

            server.Stop();

API makes appear that it is possible to configure recording for a specific request but that does not work:

            // does not work, should work or not be possible to configure
            server
                .Given(Request.Create().UsingGet())
                .RespondWith(Response.Create()
                   .WithProxy(new ProxyAndRecordSettings
                   {
                       Url = "https://github.com/",
                       SaveMapping = true,
                       SaveMappingToFile = true
                   }));

@StefH
Copy link
Collaborator

StefH commented Dec 6, 2020

@martinkra I see you point. I'll check if I can change the code to save the mapping in all scenarios.

@StefH StefH added bug and removed question labels Dec 6, 2020
@StefH StefH self-assigned this Dec 6, 2020
@StefH StefH changed the title How to setup proxy and recording properly WithProxy(...) does not save the mappings to file Dec 6, 2020
@StefH
Copy link
Collaborator

StefH commented Dec 6, 2020

#550

@martinkra
Copy link
Author

martinkra commented Dec 7, 2020 via email

@StefH
Copy link
Collaborator

StefH commented Dec 7, 2020

@martinkra
Sorry, the version you need is 1.3.8-ci-14327

@martinkra
Copy link
Author

No such version on nuget.org, MyGet feed https://www.myget.org/F/stefh does no longer work for me.

@StefH
Copy link
Collaborator

StefH commented Dec 7, 2020

@martinkra
Copy link
Author

I've found the feed and the version. In build 1.3.8-ci-14327 recording settings on request level do not break the recording:

            var server = WireMock.Server.WireMockServer.Start(new WireMockServerSettings
            {
                Port = 1234,
                StartAdminInterface = true,
                ProxyAndRecordSettings = new ProxyAndRecordSettings
                {
                    Url = "http:https://postman-echo.com/post",
                    SaveMapping = true,
                    SaveMappingToFile = true
                }
            });

            server
                .Given(Request.Create().UsingGet())
                .RespondWith(Response.Create()
                   .WithProxy(new ProxyAndRecordSettings
                   {
                       Url = "http:https://postman-echo.com/post",
                       SaveMapping = true,
                       SaveMappingToFile = true
                   }));

            Console.WriteLine("Press any key to stop the server");
            Console.ReadLine();

            server.Stop();

However, it is still a bit confusing that one could configure request level recording whithout setting up global recording, it would not work in that scenario.

            var server = WireMock.Server.WireMockServer.Start(new WireMockServerSettings
            {
                Port = 1234,
                StartAdminInterface = true //,
                //ProxyAndRecordSettings = new ProxyAndRecordSettings
                //{
                //    Url = "http:https://postman-echo.com/post",
                //    SaveMapping = true,
                //    SaveMappingToFile = true
                //}
            });

            server
                .Given(Request.Create().UsingGet())
                .RespondWith(Response.Create()
                   .WithProxy(new ProxyAndRecordSettings
                   {
                       Url = "http:https://postman-echo.com/post",
                       SaveMapping = true,
                       SaveMappingToFile = true
                   }));

@StefH
Copy link
Collaborator

StefH commented Dec 7, 2020

My intention was to fix the code so that your last code example would work fine. If this code example does not work correctly, I need to check the code again.

@martinkra
Copy link
Author

Code where recording is set twice works in 1.3.8-ci-14327, that did not work before so the original issue is fixed.

Code where recording is not set during server start and is set on request level (second snippet in my previous comment) does not work. I can manage without that feature but thought it is worth noting that it appears as an inconsistency or a bug.

@StefH
Copy link
Collaborator

StefH commented Dec 7, 2020

New (1.3.8-ci-14337) on MyGet

  1. If WireMockServerSettings.ProxyAndRecordSettings is defined, the SaveMapping value is used, also for WithProxy
  2. If WireMockServerSettings.ProxyAndRecordSettings is not defined,but the ProxyAndRecordSettings.SaveMapping is set top true, then from WithProxy is used

I think this is the full solution.

@martinkra
Copy link
Author

I agree that the proposed is the full solution. However, 1.3.8-ci-14337 does not work as described. The following code does not record mappings:

            var server = WireMock.Server.WireMockServer.Start(new WireMockServerSettings
            {
                Port = 1234,
                StartAdminInterface = true
            });

            server
                .Given(Request.Create().UsingGet())
                .RespondWith(Response.Create()
                   .WithProxy(new ProxyAndRecordSettings
                   {
                       Url = "http:https://postman-echo.com/post",
                       SaveMapping = true,
                       SaveMappingToFile = true
                   }));

@StefH
Copy link
Collaborator

StefH commented Dec 7, 2020

@martinkra Thank you for your testing.

I hope this version 1.3.8-ci-14339 works as defined.

@martinkra
Copy link
Author

martinkra commented Dec 7, 2020

@StefH Thank you for your help and fixes.

Version 1.3.8-ci-14339 works as defined.

@StefH
Copy link
Collaborator

StefH commented Dec 8, 2020

New official version will be released today.

@StefH StefH closed this as completed Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants