Git Product home page Git Product logo

Comments (10)

richardszalay avatar richardszalay commented on June 1, 2024

Hi @Jericho, what version of MockHttp are you running?

from mockhttp.

Jericho avatar Jericho commented on June 1, 2024

<PackageReference Include="RichardSzalay.MockHttp" Version="3.2.1" />

from mockhttp.

richardszalay avatar richardszalay commented on June 1, 2024

The difference is likely due to the magic MockHttp has to due in order to run filters like WithFormData. Since the

Still, I'll take a look at your repro and see if there's anything that can be done. Requests should always be created from scratch before sending, but I agree that MockHttp should behave the same as the real handler as much as possible.

from mockhttp.

Jericho avatar Jericho commented on June 1, 2024

Thanks Richard. I ran several integration tests against actual HTTP servers and I can reliably demonstrate the issue and I can also demonstrate that cloning the request and the content before dispatching each request solves my problem but it would be great to be able to demonstrate it in unit tests!

from mockhttp.

stannedelchev avatar stannedelchev commented on June 1, 2024

@richardszalay Any news on this?
I'm running into the exact same problem - MockHttp 5.0.0 doesn't dispose the request content as Microsoft's HttpClient does, which makes it hard for me to unit test multiple retries on the same request.

edit: Realized I was using v3.2.1 and updated to 5.0, but the issue remains.

from mockhttp.

maxxwizard avatar maxxwizard commented on June 1, 2024

Running into the same issue as @stannedelchev attempting to validate call/retry count on the same request.

mockHttpClient.GetMatchCount(request) always returns 1 even though my subject-under-test has made the request multiple times.

I am using nuget version 5.0.0.

from mockhttp.

richardszalay avatar richardszalay commented on June 1, 2024

@stannedelchev The issue is actually that the ObjectDisposedException isn't triggered, not that it isn't disposed. I'll figure out the best way to work this into the core design, but you can work around it for now by adding a custom matcher that consumes the content stream (which will force an exception if it is already disposed):

mockHttp.Expect(HttpMethod.Post, "https://api.fictitious-vendor.com/v1/endpoint")
    .With(req => { req.Content.ReadAsStringAsync().Result; return true; })
    .Respond("application/json", "{'name' : 'This is a test'}");

@maxxwizard The GetMatchCount issue seems like a different problem. Can you log a new issue?

from mockhttp.

maxxwizard avatar maxxwizard commented on June 1, 2024

@richardszalay i just revisited my offending unit test. i changed enough logic that i cannot figure out how to repro so let's call it user error - mea culpa.

from mockhttp.

richardszalay avatar richardszalay commented on June 1, 2024

So I've looked into this with a view of fixing it, but I don't actually think there's an issue:

  1. Attempting to send the same request twice throws an InvalidOperationException (from HttpClient itself)
  2. Sending a request and then attempting to read from it's Content throws an ObjectDisposedException.

I think the problem with the original post here is that the sample code was calling client.SendAsync twice but without awaiting / blocking either of them so they were happening in parallel. It would make sense in that scenario that the request/content hadn't been disposed since the requests were in flight.

If someone can create me a working repro for this issue I'm happy to fix it but for now I'm going to close it.

from mockhttp.

richardszalay avatar richardszalay commented on June 1, 2024

Here are the tests I used to check both scenarios:

[Fact]
public async Task Disposes_Content()
{
    var mockHandler = new MockHttpMessageHandler();
    var client = new HttpClient(mockHandler);

    mockHandler
        .When(HttpMethod.Post, "/test")
        .Respond(HttpStatusCode.OK);

    var req = new HttpRequestMessage(HttpMethod.Post, "http://tempuri.org/test")
    {
        Content = new StringContent("test")
    };

    await client.SendAsync(req);

    Assert.Throws<ObjectDisposedException>(() => req.Content.ReadAsStringAsync().Result);
}

[Fact]
public async Task Cannot_send_request_twice()
{
    var mockHandler = new MockHttpMessageHandler();
    var client = new HttpClient(mockHandler);

    mockHandler
        .When(HttpMethod.Post, "/test")
        .Respond(HttpStatusCode.OK);

    var req = new HttpRequestMessage(HttpMethod.Post, "http://tempuri.org/test")
    {
        Content = new StringContent("test")
    };

    await client.SendAsync(req);

    Assert.Throws<InvalidOperationException>(() => client.SendAsync(req).Result);
}

from mockhttp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.