Localhost and free port problem #156

Closed
opened 2025-12-29 14:23:51 +01:00 by adam · 3 comments
Owner

Originally created by @SabrinaMH on GitHub (Dec 13, 2018).

I have a unit test, where I have the following code

var server = FluentMockServer.Start();
server.Given(Request.Create().WithPath("/recommendations").UsingGet()).RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK)
.WithBodyAsJson(new List<Message>{
new Message(
new Header(Guid.NewGuid(),
new VersionNumber(1, 0, 0),
new VersionNumber(1, 0, 0),
"dummyPayload",""),@"{""Id"":""ec2a2675-47bf-4b60-9d10-646756052e39"",""SentByUser"":""John Doe""}")}));

And then later in the test I have

var token = await _tokenProvider.FetchNewTokenAsync();
var getUriBuilder = new UriBuilder(new Uri(ConfigurationManager.AppSettings["RRQ.Broker.host"]));
getUriBuilder.Path = ConfigurationManager.AppSettings["RRQ.Broker.path.getRecommendations"];

var getRequest = new HttpRequestMessage(HttpMethod.Get, getUriBuilder.Uri);
getRequest.Headers.Add("Authorization", "Bearer " + token);

var getResponse = await _httpClient.SendAsync(getRequest);

where the getRequest is targeted http://localhost:9002/recommendations
However, when I run this test, I end up with a TaskCanceled exception. It hangs in the _httpClient.SendAsync method for quite a while, which then throws this exceptions.

Originally created by @SabrinaMH on GitHub (Dec 13, 2018). I have a unit test, where I have the following code ``` c# var server = FluentMockServer.Start(); server.Given(Request.Create().WithPath("/recommendations").UsingGet()).RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK) .WithBodyAsJson(new List<Message>{ new Message( new Header(Guid.NewGuid(), new VersionNumber(1, 0, 0), new VersionNumber(1, 0, 0), "dummyPayload",""),@"{""Id"":""ec2a2675-47bf-4b60-9d10-646756052e39"",""SentByUser"":""John Doe""}")})); ``` And then later in the test I have ``` c# var token = await _tokenProvider.FetchNewTokenAsync(); var getUriBuilder = new UriBuilder(new Uri(ConfigurationManager.AppSettings["RRQ.Broker.host"])); getUriBuilder.Path = ConfigurationManager.AppSettings["RRQ.Broker.path.getRecommendations"]; var getRequest = new HttpRequestMessage(HttpMethod.Get, getUriBuilder.Uri); getRequest.Headers.Add("Authorization", "Bearer " + token); var getResponse = await _httpClient.SendAsync(getRequest); ``` where the getRequest is targeted http://localhost:9002/recommendations However, when I run this test, I end up with a TaskCanceled exception. It hangs in the _httpClient.SendAsync method for quite a while, which then throws this exceptions.
adam added the question label 2025-12-29 14:23:51 +01:00
adam closed this issue 2025-12-29 14:23:51 +01:00
Author
Owner

@StefH commented on GitHub (Dec 13, 2018):

Difficult to see what's wrong. Can you post your complete test?

A question : are you sure the real code is connecting to the WireMock server?
As an example see:
dffeb95116/test/WireMock.Net.Tests/FluentMockServerTests.cs (L98)
Example:

// step 1
var server = FluentMockServer.Start();

// step 2
await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo");
@StefH commented on GitHub (Dec 13, 2018): Difficult to see what's wrong. Can you post your complete test? A question : are you sure the real code is connecting to the WireMock server? As an example see: https://github.com/WireMock-Net/WireMock.Net/blob/dffeb95116c4fabf0f80dba3bc7b091eff6ed5be/test/WireMock.Net.Tests/FluentMockServerTests.cs#L98 Example: ```c# // step 1 var server = FluentMockServer.Start(); // step 2 await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo"); ```
Author
Owner

@SabrinaMH commented on GitHub (Dec 17, 2018):

Thanks!!
I thought (wrongly) that if I only used WithPath("XXX"), then WireMock would intercept traffic on all ports and thus be able to stub all endpoints (regardless of the port) that had path XXX.

@SabrinaMH commented on GitHub (Dec 17, 2018): Thanks!! I thought (wrongly) that if I only used WithPath("XXX"), then WireMock would intercept traffic on all ports and thus be able to stub all endpoints (regardless of the port) that had path XXX.
Author
Owner

@StefH commented on GitHub (Dec 17, 2018):

OK. Closing this question.

@StefH commented on GitHub (Dec 17, 2018): OK. Closing this question.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#156