Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpTest WithRequestJson does not use JsonSerializer set on FlurlClientCache #813

Open
arch-imp opened this issue Mar 2, 2024 · 3 comments
Labels

Comments

@arch-imp
Copy link

arch-imp commented Mar 2, 2024

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? :) )

  [Test]
  public async Task TheTest()
  {
      var clientCache = new FlurlClientCache().WithDefaults(builder =>
          builder.WithSettings(settings =>
              settings.JsonSerializer = new DefaultJsonSerializer(new JsonSerializerOptions
              {
                  PropertyNamingPolicy = JsonNamingPolicy.CamelCase
              })
          )
      );

      using var httpTest = new HttpTest();

      var requestDto = new RequestDto
      {
          Property = "Value"
      };

      var client = clientCache.GetOrAdd("NamedClient", "http://test.com");

      await client.Request().PostJsonAsync(requestDto);

      httpTest.ShouldHaveCalled("http://test.com")
          .WithRequestJson(requestDto);
  }

  private class RequestDto
  {
      public string Property { get; set; } = null!;
  }
@arch-imp arch-imp added the bug label Mar 2, 2024
@tmenier
Copy link
Owner

tmenier commented Mar 24, 2024

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?

@arch-imp arch-imp changed the title HttpTest does not use default serializer on FlurlClientCache HttpTest does not use DefaultJsonSerializer set on FlurlClientCache Mar 26, 2024
@arch-imp arch-imp changed the title HttpTest does not use DefaultJsonSerializer set on FlurlClientCache HttpTest does not use JsonSerializer set on FlurlClientCache Mar 26, 2024
@arch-imp
Copy link
Author

arch-imp commented Mar 26, 2024

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(Func2 match, String descrip)
HttpCallAssertion.WithRequestBody(String bodyPattern)
HttpCallAssertion.WithRequestJson(Object body)
FlurlTests.TheTest() line 36

@arch-imp arch-imp changed the title HttpTest does not use JsonSerializer set on FlurlClientCache HttpTest WithRequestJson does not use JsonSerializer set on FlurlClientCache Mar 26, 2024
@tmenier
Copy link
Owner

tmenier commented Mar 26, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Backlog
Development

No branches or pull requests

2 participants