Comments (5)
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.
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.
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.
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.
@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)
- MissingMethodException when running on mono with newer Newtonsoft.Json HOT 13
- Allow access to CancellationToken HOT 2
- Question: How to simulate a timeout/no response from downstream service HOT 2
- request: more examples for response changing based on how many times called so far HOT 4
- Improve custom matching HOT 3
- NullReferenceException when Deregister builder with a custom matcher HOT 2
- Verify calls to the registered end points have been called [discussion] HOT 8
- Same request but with different responses HOT 4
- Add [StringSyntax] attributes HOT 2
- Remove Newtonsoft.Json
- Async RegisterBundle() overloads
- WithResponseHeaders and WithContentHeaders overloads HOT 7
- Fix remote certificate is invalid for https HOT 3
- Response sequence HOT 1
- Add exception list for urls to ignore when ThrowOnMissingRegistration is true
- Question: Should HttpClient created from HttpClientInterceptorOptions auto-follow redirects? HOT 1
- Ignore order of query params HOT 4
- after upgrade, build error HOT 4
- Test Failures Due to Authorization Issues After Upgrading to .NET 8 and HttpClient-Interception HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from httpclient-interception.