Search code examples
c#http-headershttpclient.net-7.0

.net 7 HTTP Client calls failing with Format Exception: The format of value '' is invalid


We just upgraded to .net 7, and all the http calls in our integration tests started breaking with the error:

Message: 
    System.FormatException : The format of value '' is invalid.

  Stack Trace: 
    HttpHeaderParser.ParseValue(String value, Object storeValue, Int32& index)
    HttpHeaders.ParseAndAddValue(HeaderDescriptor descriptor, HeaderStoreItemInfo info, String value)
    HttpHeaders.Add(HeaderDescriptor descriptor, String value)
    CookieContainerHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)

These tests are working fine in .net 5 and .net 6.

Here's an example test -

public async Task ShouldGetStatusOfRunFromAPI()
{
            var client = CreateClient(this.mockApiServer.Uri.ToString());

            HttpResponseMessage response = await client.GetAsync(StatusAPIForAuroraRun1ContextPath);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

I'm not explicitly setting any headers, but the error doesn't make it clear which value it is expecting that cannot be empty.

Here's the CreateClient method in the TestStartup -

protected HttpClient CreateClient(string uri)
        {
            var projectDir = Directory.GetCurrentDirectory();
            var factory = new WebApplicationFactory<Program>();
            var config = TestConfigurationHelper.LoadConfiguration();

            return factory
                .WithWebHostBuilder(builder =>
                {
                    builder.ConfigureTestServices(services =>
                    {
                        services.Configure<ClassOptions>(options => config.GetSection(ClassOptions.Name).Bind(options));

                        SomeService someService = NewService(config);
                        services.AddSingleton(someService);

                        Kubernetes kubernetesClient = NewKubernetesClient(uri);
                        services.AddSingleton<IKubernetes, Kubernetes>((svcProvider) => kubernetesClient);

                        services.AddSingleton<ISecretProvider, SecretProvider>((svcProvider) => NewSecretProvider());

                    })
                    .ConfigureAppConfiguration((context, builder) =>
                    {
                        builder.AddConfiguration(config);
                    });
                })
                .CreateClient();
        }

The error message shows it's failing while adding some header, but how do I know which header value is failing? The value for Accept is set, and I also tried setting it explicitly using -

client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

But this also failed with the same error.

The calls are failing at the line where GetAsync() is being called. These are the header values at the time of the call - enter image description here


Solution

  • Per my comment on the question, it's an issue in CookieContainerHandler: it looks like if you upgrade your Microsoft.AspNetCore.Mvc.Testing reference it will resolve it; this is what I have now in my dual published library:

    <PackageReference Condition="'$(TargetFramework)' == 'net6.0'" Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.18" />
    <PackageReference Condition="'$(TargetFramework)' == 'net7.0'" Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.0" />