I have been playing a little with Wiremock.net for mocking out HTTP calls. I've got it working happily running from a console application, but when I try to do it with a unit test it stops responding. Here is my complete code:
FluentMockServer serviceMock;
[TestInitialize]
public void Initialize()
{
serviceMock = FluentMockServer.Start(new FluentMockServerSettings { Urls = new[] { "http://+:5010" } });
serviceMock.Given(Request.Create().UsingPost().WithPath("/callbacks/booked"))
.RespondWith(Response.Create().WithStatusCode(200).WithHeader("Content-Type", "application/json")
.WithBodyAsJson(new List<Callback>
{
new Callback { CustomerId = 1, RequestedFor = new DateTime(2017, 12, 14, 15, 00, 01) },
}));
}
[TestMethod]
public void CanHitMockEndpoint()
{
var client = new RestClient("http://localhost:5010");
var response = client.Get<List<Callback>>(new RestRequest("/callbacks/booked"));
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
[TestCleanup]
public void Cleanup() => serviceMock.Stop();
When I get the response back, it has a status code of zero, and the message "Unable to connect to the remote server"
The strange thing is, if I move the Initialize and Cleanup items into a console app, and run it alongside, the test will work. I've confirmed that the Initialize/Cleanup is running, so I'm definitely either missing something obvious, or something strange is going on!
Any help would be great.
I'm still not totally sure what the issue is here, but I've narrowed it down to the handling of the ports. I've modified the server creation to:
serviceMock = FluentMockServer.Start();
And used the IP it wires to internally for my service setup:
var client = new RestClient(serviceMock.Urls.First());
This then works without problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With