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

Closed
opened 2025-12-29 14:22:09 +01:00 by adam · 5 comments
Owner

Originally created by @raheel0452 on GitHub (Apr 10, 2018).

Dear Team,

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

// setup
var server1 = FluentMockServer.Start(new FluentMockServerSettings
{
Urls = new[] { "http://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

Originally created by @raheel0452 on GitHub (Apr 10, 2018). Dear Team, In a console application, I've below code to start WireMock.Net // setup var server1 = FluentMockServer.Start(new FluentMockServerSettings { Urls = new[] { "http://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
adam closed this issue 2025-12-29 14:22:09 +01:00
Author
Owner

@StefH commented on GitHub (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://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.

@StefH commented on GitHub (Apr 10, 2018): When using wiremock in unit tests, you should not use a fixed port. Use code like this: ``` c# // 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://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.
Author
Owner

@raheel0452 commented on GitHub (Apr 11, 2018):

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.

@raheel0452 commented on GitHub (Apr 11, 2018): 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:<port> - 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("<ProjectDirectory>\\bin\\Debug\\netcoreapp2.0\\__admin\\mappings"); Any thoughts ? Thanks.
Author
Owner

@StefH commented on GitHub (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?

@StefH commented on GitHub (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?
Author
Owner

@raheel0452 commented on GitHub (Apr 11, 2018):

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 ?

@raheel0452 commented on GitHub (Apr 11, 2018): 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 ?
Author
Owner

@StefH commented on GitHub (May 18, 2018):

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

@StefH commented on GitHub (May 18, 2018): Probably expected, maybe not all async code is really listening.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#88