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

WireMock.Net not responding in unit tests - same works in console application #122

Closed
raheel0452 opened this issue Apr 10, 2018 · 5 comments

Comments

@raheel0452
Copy link

Dear Team,

In a console application, I've below code to start WireMock.Net

// setup
var server1 = FluentMockServer.Start(new FluentMockServerSettings
{
Urls = new[] { "http:https://localhost:9091" },
ReadStaticMappings = true,
StartAdminInterface = true
});

This works all good.

When i write same in Unit tests, WireMock.Net does not respond

  • unable to access localhost:9091
  • static mappings are not read
  • isStarted status though displays as true

It's mandatory for us to use port (i've referred to https://github.com/WireMock-Net/WireMock.Net/wiki/Using-WireMock-in-UnitTests)

Thanks in advance

@StefH
Copy link
Collaborator

StefH commented Apr 10, 2018

When using wiremock in unit tests, you should not use a fixed port.

Use code like this:

// given
            _server = FluentMockServer.Start();

            _server.Given(Request.Create().WithPath("/foo").UsingVerb("patch"))
                .RespondWith(Response.Create().WithBody("hello patch"));

            // when
            var msg = new HttpRequestMessage(new HttpMethod("patch"), new Uri("http:https://localhost:" + _server.Ports[0] + "/foo"))
            {
                Content = new StringContent("{\"data\": {\"attr\":\"value\"}}")
            };
            var response = await new HttpClient().SendAsync(msg);

The wiki is somewhat confusing. I will update this in the weekend.

@raheel0452
Copy link
Author

Thank you for response.

Couple of questions from above request

  1. can we not specify a port when running mockserver.net within unit tests?
  2. can we not read static mappings when running mockserver.net within unit tests?

We've attempted to start server without specifying any port. However the behavior remained same

  • unable to access localhost:
  • static mappings are not read
    (isStarted status though displays as true)

It's important for us to read static mapping from specified location.

Code we're trying with

var server1 = FluentMockServer.Start();
server1.ReadStaticMappings("\bin\Debug\netcoreapp2.0\__admin\mappings");

Any thoughts ?

Thanks.

@StefH
Copy link
Collaborator

StefH commented Apr 11, 2018

  1. It is possible to use a fixed port, but then you have to start wiremock once, else you get conflicts. And when running the tests on a build server you can never be 100% sure that the port is free. Thats the reason that I start the server without a port, so that a free port is chosen. And use that port further.

  2. This should work. Please search in my unit tests for the code which uses static mappings. Maybe the code has troubles finding the relative path?

@raheel0452
Copy link
Author

Thank you. This is working now.

However one last point worth mentioning - when i execute unit test in debug mode, even though isStarted is set to true, i can't access the mockserver through browser until the continue debug. Is that expected ?

@StefH
Copy link
Collaborator

StefH commented May 18, 2018

Probably expected, maybe not all async code is really listening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants