Open arch-imp opened 9 months ago
I'm not sure if I'm following. Any value set explcitly on HttpTest.Settings should always override the same setting set anywhere in the SUT. That's by design. But when not explicitly set on HttpTest.Settings, it should respect the settings specified in the SUT. In your code above, I confirmed that the settings specified at the start are in effect when you make the call further down. Am I missing something?
I've updated the title to hopefully help clarify.
When I run this test, the call ".WithRequestJson(requestDto)" fails. Reason being is the json serializer set on the FlurlClientCache is using camel case. Hence, the serialized json used in the body of the call is {"property":"Value"}. In the call to WithRequestJson, the serializer used to serialize requestDto is not the same as what is set earlier. As a result, WithRequestJson is using pascal case, and is comparing {"Property":"Value"} to the body of the call. Note the uppercase "Property" vs lowercase "property".
{"Property":"Value"} is not the same as {"property":"Value"}
Assertion message: Flurl.Http.Testing.HttpTestException: Expected any calls to be made with URL pattern http://test.com and body {"Property":"Value"}, but no matching calls were made.
Stack Trace:
HttpCallAssertion.Assert(Nullable1 count) HttpCallAssertion.With(Func
2 match, String descrip)
HttpCallAssertion.WithRequestBody(String bodyPattern)
HttpCallAssertion.WithRequestJson(Object body)
FlurlTests.TheTest() line 36
Got it. I probably should have ran your test before replying. :) Sounds legitimate, it's been added to the backlog and I'll look into when I get some time.
This is very similar to Issue 246 but has a slightly different configuration path. In my production code, I'm using DI to configure the FlurlClientCache with defaults. I was able to duplicate this issue when configuring the cache defaults, or directly on a FlurlClient or FlurlRequest. However, if configuring FlurlHttp.Clients.WithDefaults, it works properly.
It seems to me the issue is in the HttpTest WithRequestJson (or similar) methods that deserialize the request or response body using the settings of HttpTest rather than the settings that are currently in effect at the call level. As a result, the assertion uses a different serializer configuration than the actual call, which then fails the assertion.
As in the linked issue, I can set the JsonSerializer on the HttpTest instance, but that doesn't work if I have different serializer settings on different clients or requests at the same time. (But who would do that? :) )