From 6a3893ed07bf3b53b6162ed4a79174e1c06873f6 Mon Sep 17 00:00:00 2001 From: Sunghwan Bang Date: Wed, 1 Nov 2023 02:38:38 +0900 Subject: [PATCH] Add default http request version and policy (#6641) --- .../DefaultGraphQLHttpClient.cs | 4 +++ .../GraphQLHttpClientConfigurationTests.cs | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/HotChocolate/AspNetCore/test/Transport.Http.Tests/GraphQLHttpClientConfigurationTests.cs diff --git a/src/HotChocolate/AspNetCore/src/Transport.Http/DefaultGraphQLHttpClient.cs b/src/HotChocolate/AspNetCore/src/Transport.Http/DefaultGraphQLHttpClient.cs index ec629ef8ada..44202b00784 100644 --- a/src/HotChocolate/AspNetCore/src/Transport.Http/DefaultGraphQLHttpClient.cs +++ b/src/HotChocolate/AspNetCore/src/Transport.Http/DefaultGraphQLHttpClient.cs @@ -103,6 +103,10 @@ private async Task ExecuteInternalAsync( // DO NOT move the writer out of this method. using var arrayWriter = new ArrayWriter(); using var requestMessage = CreateRequestMessage(arrayWriter, request, requestUri); +#if NET5_0_OR_GREATER + requestMessage.Version = _http.DefaultRequestVersion; + requestMessage.VersionPolicy = _http.DefaultVersionPolicy; +#endif var responseMessage = await _http.SendAsync(requestMessage, ResponseHeadersRead, ct).ConfigureAwait(false); return new GraphQLHttpResponse(responseMessage); } diff --git a/src/HotChocolate/AspNetCore/test/Transport.Http.Tests/GraphQLHttpClientConfigurationTests.cs b/src/HotChocolate/AspNetCore/test/Transport.Http.Tests/GraphQLHttpClientConfigurationTests.cs new file mode 100644 index 00000000000..a12cd23f40a --- /dev/null +++ b/src/HotChocolate/AspNetCore/test/Transport.Http.Tests/GraphQLHttpClientConfigurationTests.cs @@ -0,0 +1,35 @@ +using System.Net; +using static HotChocolate.AspNetCore.Tests.Utilities.TestServerExtensions; + +namespace HotChocolate.Transport.Http.Tests; + +public class GraphQLHttpClientConfigurationTests +{ + [Fact] + public async Task DefaultRequestVersion() + { + var httpClient = new HttpClient(new TestHttpMessageHandler( + request => + { + Assert.Equal(HttpVersion.Version20, request.Version); + Assert.Equal(HttpVersionPolicy.RequestVersionOrHigher, request.VersionPolicy); + return new(); + } + )) + { + DefaultRequestVersion = HttpVersion.Version20, + DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher, + }; + + var client = GraphQLHttpClient.Create(httpClient, true); + await client.SendAsync(new("{ __typename }", new(CreateUrl(default))), default); + } + + class TestHttpMessageHandler(Func sender) : HttpMessageHandler + { + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + return Task.FromResult(sender.Invoke(request)); + } + } +}