WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.35k stars 197 forks source link

C# Unit test: Wiremock Server runs but does not deliver response #1105

Closed elijah-ezaga closed 1 month ago

elijah-ezaga commented 1 month ago

Hi Wiremock.Net Team. I am new to using Wiremock.NET (v1.5.53, the latest) and trying to get it to work with a simple setup but its server does not deliver any response. For context, I have a simple C# Unit test file in a project (.NET Framework 461) with the following contents:

[TestFixture]
public class FooTest
{
    private WireMockServer wireMockServer;

    [SetUp]
    public void StartServer()
    {
        this.wireMockServer = WireMockServer.Start(new WireMock.Settings.WireMockServerSettings
            {
                Urls = new string[] { "http://localhost:8080" },
                StartAdminInterface = true
            }
        );
    }

    [TearDown]
    public void StopServer()
    {
        this.wireMockServer.Stop();
    }

    [Test]
    public async Task TestSomeResponse()
    {
                this.wireMockServer.Given(RequestCreator.Create().WithPath("/api").UsingGet())
           .RespondWith(ResponseCreator.Create().WithStatusCode(200).WithBody("foo"));
        HttpResponseMessage response = await new HttpClient().GetAsync($"{this.wireMockServer.Urls[0]}/api"); // Line with failure
        Assert.That(response.StatusCode, Is.EqualTo(200));
    }
}

When I run the test, I get the error:

Message: 
System.Threading.Tasks.TaskCanceledException : A task was canceled.

  Stack Trace: 
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
TaskAwaiter`1.GetResult()
<TestSomeResponse>d__7.MoveNext() line 27
--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
TaskAwaiter`1.GetResult()
GenericAdapter`1.BlockUntilCompleted()
NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaiter)
AsyncToSyncAdapter.Await(Func`1 invoke)
TestMethodCommand.RunTestMethod(TestExecutionContext context)
TestMethodCommand.Execute(TestExecutionContext context)
<>c__DisplayClass1_0.<Execute>b__0()
DelegatingTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)

Also If I add a line like await Task.Delay(TimeSpan.FromMinutes(5)) to pause execution and I make requests from Postman for admin URLs like __admin/mappings, it waits endlessly until the time exceeds and the test runs out and it doesn't get a response. However, I can confirm from a Windows command prompt, running netstat -ab, that the port is opened and the test process is listening for requests.

Any clues what might be wrong ?

StefH commented 1 month ago

Can you please give the complete test project?

elijah-ezaga commented 1 month ago

Hi Stef. Thanks for offering to help.

While I was preparing the project to attach here, I found the issue and was able to fix it. It was connected with libraries in the project depending on 2 different versions of System.Memory leading to

System.IO.FileNotFoundException: Could not load file or assembly 'System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.

I got it fixed by using binding redirect in app.config as the project type was a non-cross-platform .NET Framework test project type.

Later I re-created the project to be a cross-platform NUnit test project and there was no longer need for the binding redirect.