Reputation: 536
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 -
Upvotes: 8
Views: 4319
Reputation: 83
Update 10/24/23:
Tried installing and updating the package Microsoft.AspNetCore.MVC.Testing
in my Web API project, even though it is a Web API project.. lol
I updated it to 7.0.12 and it does not work.
Here is what I added:
request.Headers.Accept.ParseAdd("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
Here is the error message. Notice that the validation has added spacing, etc.
System.FormatException: The format of value 'text/html, application/xhtml+xml, application/xml; q=0.9, image/avif, image/webp, image/apng, */*; q=0.8, application/signed-exchange; v=b3; q=0.7'
is invalid.
This is undoubtedly an issue with the CookieContainer since I am adding 14 cookies to this call resulting in an error, whereas other calls without cookies are going through fine. Any update to fix would be wonderful. Thank you
Upvotes: 0
Reputation: 86
It is an issue in "CookieContainerHandler". Please upgrade your "Microsoft.AspNetCore.MVC.Testing".
Upvotes: 5
Reputation: 17501
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" />
Upvotes: 15