Git Product home page Git Product logo

Comments (5)

hwoodiwiss avatar hwoodiwiss commented on July 18, 2024 1

Thanks for raising this!

I can see your point, but I think your example here applies to a different use-case than the example in the readme.

In your case, you'd apply the message handler to your one client called "myclient". In the example IHttpMessageHandlerBuilderFilter is used to ensure that the interception is applied to all clients instantiated throughout the application via the client factory.

In .NET 8, a more similar example to what we're currently showing might be

var builder = new HttpRequestInterceptionBuilder();

builder.Requests()
       .ForGet()
       .ForAnyHost()
       .Responds()
       .WithStatus(HttpStatusCode.OK)
       .RegisterWith(options);

var services = new ServiceCollection();
services.ConfigureHttpClientDefaults()
    .ConfigureAdditionalHttpMessageHandlers((handlerList, _) => handlerList.Add(options.CreateHttpMessageHandler()));
services.AddHttpClient("myclient");

var serviceProvider = services.BuildServiceProvider();

var httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
var client = httpClientFactory.CreateClient("myclient");

However, we currently target older TFM's that wouldn't support this, so I think our documentation should reflect that for now.

from httpclient-interception.

martincostello avatar martincostello commented on July 18, 2024 1

The advantage of IHttpMessageHandlerBuilderFilter is that it can be registered once and it will hook-up interception for all usages of IHttpClientFactory without the tests having to know any of the implementation details of how you use it in the application under test. It also plays nicely with the case where there are lots of other delegating handlers configured, as you can (mostly) ensure that the interception runs last, so your tests still cover the other handlers, rather than causing them to be short-circuited by the interception.

If you have an application where you have multiple named HTTP clients and wish to integration test them, using the approach in your original comment requires you to repeat the setup for each of those clients in order to intercept their calls, as well as knowing (and then repeating) all of the client registration calls in the tests.

For a concrete example of the above case, in one of my open source projects I configure HTTP clients for each OAuth provider I have, which in this case is 5-7 depending on the application's configuration. All of that detail is hidden from the integration test, which intercept the calls for all of them (and other usages of HttpClient not to do with OAuth) in these 3 lines of code using the IHttpMessageHandlerBuilderFilter approach. For a more extreme example, there's this project - we maintain over 90 OAuth providers, and all their HTTP calls are intercepted with these 2 lines of code.

In a simple application though, you're right, it could be done the other way if you were doing some unit testing where you just want a simple HttpClient that intercepts the calls.

We'd accept a pull request that suggests the approach you give as an alternative for a simple case like you describe, but I still feel that the current approach is the best overall recommendation as for just a few lines of code to add to your tests, it's the simplest way to integrate the library into an existing test suite.

from httpclient-interception.

martincostello avatar martincostello commented on July 18, 2024 1

It doesn't by default, but you can use a scope to constrain the lifetime of the matches where the matches can't otherwise be discriminated by more specific matches (e.g. looking at a header, a part of the path, etc.).

from httpclient-interception.

silkfire avatar silkfire commented on July 18, 2024

Thanks both of you for providing your opinions and knowledge on the subject, I feel much wiser now on how everything works together and the advantages of using the filter when working with a collection of tests.

from httpclient-interception.

silkfire avatar silkfire commented on July 18, 2024

@martincostello Just a question. When using a shared fixture/collection like shown in the example, how does HttpClientInterceptor keep the registrations apart so that each test-specific registration doesn't conflict with the others? Especially as tests are run in parallell in each test class?

from httpclient-interception.

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.