Comments (4)
Thanks for the question - this is possible, but there isn't an explicit example of this in our Examples
unit tests. There is a unit test elsewhere that shows this sort of thing though:
For your example, if I'm understanding it correctly, you'd want to do something like this pseudo-code:
// Create an empty list
var items = new List<MyObject>();
// Register the GET and POST requests
var options = new HttpClientInterceptorOptions();
new HttpRequestInterceptionBuilder()
.ForGet()
.ForUrl("http://localhost/items")
.WithJsonContent(items)
.RegisterWith(options);
new HttpRequestInterceptionBuilder()
.ForPost()
.ForUrl("http://localhost/items")
.WithStatus(HttpStatusCode.Created)
.RegisterWith(options);
// Your code to do the first GET and POST goes here
// Add an item to the list - the GET registration is
// using the object by reference, so any mutations to
// it will be observed by subsequent requests which
// match the registration for the builder using it.
items.Add(new MyObject());
// Your code to do the second GET goes here, which
// should return the list with the single item in it.
If you need to inspect the object that was POSTed in some way, then you could use the WithInterceptionCallback()
method to inspect the request body and mutate the list from a callback. This test shows the use of a callback:
Another possible approach is to use scopes, and change the behaviour of a registration as a test progresses, similar to this example:
Let me know if either of these answer your question, and I'll add a new example to the tests to make this behaviour a bit more discoverable.
from httpclient-interception.
Thanks for the insightful reply. I can see, I left out some - probably - valuable information:
- I am using bundles
- I am testing a request to an API that aggregates responses from other APIs - it's the latter that I am intecepting. So my tests wont be able to manipulate midway.
I created an approach where I, using another custom delegate handler would add a "X-TestIndex" header to each request with an incremental value per call. That I would then match uniquely in my bundle file with the "requestHeaders" properties. But, for some reason, I haven't figured out yet, it won't match.
from httpclient-interception.
Thanks for the clarification.
Bundles aren't able to support the same "mutations" functionality that I described as there's no code/object references to back them up, as everything is ultimately just a string from the parsed bundle file. This is sort-of by design, as they're intended to solve the simple use case where you play back the same things repeatedly/often where it's easier to define in a file rather than repeat test setup in code.
For the code-based approach, there's also the concept of "priorities" which can be mixed with custom matching to have an order of precedence so you can do fallback behaviours.
What you might be able to do is have some "defaults" in your bundle file, and then "layer on" an additional registration in code with a higher priority that can conditionally override the response configured in the bundle with a different one based on a predicate you define.
Your incrementing header approach sounds like it should work though. If you can share some code for what you're trying to do with it, I might be able to see what the problem is.
from httpclient-interception.
This issue has been automatically marked as stale because it has not had any recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
from httpclient-interception.
Related Issues (20)
- 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
- 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
- Revise documentation covering usage together with IHttpClientFactory HOT 5
- Including existing registrations in HttpRequestNotInterceptedException HOT 2
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.