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

Wiremock not working with NUnit3 in .Net Framework 4.6.1 after Wiremock 1.5.47 #1089

Closed MarcoMartins86 closed 2 months ago

MarcoMartins86 commented 3 months ago

Describe the bug

I've made a clean project with only

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="NUnit" Version="3.14.0" />
    <PackageReference Include="WireMock.Net.StandAlone" Version="1.5.47" />
  </ItemGroup>
</Project>
[TestFixture]
public class TestingWiremock
{
    private WireMockServer _server;
    [SetUp]
    protected async Task Setup()
    {
        _server = StandAloneApp.Start(new WireMockServerSettings());
        _server
            .Given(Request.Create().UsingGet())
            .RespondWith(Response.Create());
        await Task.Delay(2000);
    }

    [Test]
    public void Test()
    {
        var client = new HttpClient();
        var result = client.GetAsync(_server.Url).Result;
        Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK)); // test will timeout in 1.5.47+, pass on 1.5.46
    }
}

The behavior I've got is similar to the one described on this other thread https://github.com/WireMock-Net/WireMock.Net/issues/470, there's no error and it seems to be on some kind of deadlock somewhere but I can't pinpoint it.

Expected behavior:

Should work with .Net Framework 4.6.1 and NUnit3

Test to reproduce

Other related info

With Wiremock version 1.5.46 it is working. Also, it is working with .Net Framework 4.6.2 for 1.5.47+ Workaround, stay on 1.5.46 for now.

StefH commented 3 months ago

@MarcoMartins86 Did you try to make the test async? Like:

[Test]
    public async Task Test()
    {
        var client = new HttpClient();
        var result = await client.GetAsync(_server.Url);
        Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
    }
MarcoMartins86 commented 2 months ago

@StefH Fail with the same behavior.

System.Net.Http.HttpRequestException : An error occurred while sending the request.
  ----> System.Net.WebException : The underlying connection was closed: An unexpected error occurred on a receive.
  ----> System.IO.IOException : Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
  ----> System.Net.Sockets.SocketException : An existing connection was forcibly closed by the remote host
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

Adding a logger I can see that the server prints Server listening at http://localhost:57550 but then no request reachs the WireMockMiddleware.Invoke() (reach when working). Feels like the server have a deadlock somewhere at the beginning of request pipeline, because, it reserved the port successfully since if I try to reuse the same port while the process is running it says it is already in use.

MarcoMartins86 commented 2 months ago

I will close this, I've added <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> and it started working for all versions. For me, that is good enough. Thank you for your time and good work.