From 9e608af38dd19f3f669173f66ae589b45599ce9c Mon Sep 17 00:00:00 2001 From: Alexander Rose Date: Wed, 5 Feb 2020 16:01:59 +0100 Subject: [PATCH] add action to GraphQLHttpRequest which allows modificatio of the request prior to sending it --- src/GraphQL.Client.Http/GraphQLHttpClient.cs | 13 ++++++++---- src/GraphQL.Client.Http/GraphQLHttpRequest.cs | 17 +++++++++++++++ .../Helpers/WebHostHelpers.cs | 2 +- .../QueryAndMutationTests.cs | 21 +++++++++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/GraphQL.Client.Http/GraphQLHttpClient.cs b/src/GraphQL.Client.Http/GraphQLHttpClient.cs index 43705941..b9064fd6 100644 --- a/src/GraphQL.Client.Http/GraphQLHttpClient.cs +++ b/src/GraphQL.Client.Http/GraphQLHttpClient.cs @@ -103,7 +103,7 @@ public IObservable> CreateSubscriptionStream> SendHttpPostRequestAsync(GraphQLRequest request, CancellationToken cancellationToken = default) { var preprocessedRequest = await Options.PreprocessRequest(request, this); - using var httpRequestMessage = this.GenerateHttpRequestMessage(preprocessedRequest.SerializeToJson(Options)); + using var httpRequestMessage = this.GenerateHttpRequestMessage(preprocessedRequest); using var httpResponseMessage = await this.HttpClient.SendAsync(httpRequestMessage, cancellationToken); if (!httpResponseMessage.IsSuccessStatusCode) { throw new GraphQLHttpException(httpResponseMessage); @@ -113,10 +113,15 @@ private async Task> SendHttpPostRequestAsync>(Options, cancellationToken); } - private HttpRequestMessage GenerateHttpRequestMessage(string requestString) { - return new HttpRequestMessage(HttpMethod.Post, this.Options.EndPoint) { - Content = new StringContent(requestString, Encoding.UTF8, "application/json") + private HttpRequestMessage GenerateHttpRequestMessage(GraphQLRequest request) { + var message = new HttpRequestMessage(HttpMethod.Post, this.Options.EndPoint) { + Content = new StringContent(request.SerializeToJson(Options), Encoding.UTF8, "application/json") }; + + if (request is GraphQLHttpRequest httpRequest) + httpRequest.PreprocessHttpRequestMessage(message); + + return message; } private Uri GetWebSocketUri() { diff --git a/src/GraphQL.Client.Http/GraphQLHttpRequest.cs b/src/GraphQL.Client.Http/GraphQLHttpRequest.cs index 635d8da7..59cac335 100644 --- a/src/GraphQL.Client.Http/GraphQLHttpRequest.cs +++ b/src/GraphQL.Client.Http/GraphQLHttpRequest.cs @@ -1,5 +1,22 @@ +using System; +using System.Net.Http; +using System.Text.Json.Serialization; + namespace GraphQL.Client.Http { public class GraphQLHttpRequest : GraphQLRequest { + public GraphQLHttpRequest() + { + } + + public GraphQLHttpRequest(string query, object? variables = null, string? operationName = null) : base(query, variables, operationName) + { + } + + /// + /// Allows to preprocess a before it is sent, i.e. add custom headers + /// + [JsonIgnore] + public Action PreprocessHttpRequestMessage { get; set; } = message => { }; } } diff --git a/tests/GraphQL.Integration.Tests/Helpers/WebHostHelpers.cs b/tests/GraphQL.Integration.Tests/Helpers/WebHostHelpers.cs index bd737a67..8c32f558 100644 --- a/tests/GraphQL.Integration.Tests/Helpers/WebHostHelpers.cs +++ b/tests/GraphQL.Integration.Tests/Helpers/WebHostHelpers.cs @@ -50,7 +50,7 @@ public static TestServerSetup SetupTest(bool requestsViaWebsocket = fa public class TestServerSetup : IDisposable { public IWebHost Server { get; set; } - public IGraphQLClient Client { get; set; } + public GraphQLHttpClient Client { get; set; } public void Dispose() { Server?.Dispose(); diff --git a/tests/GraphQL.Integration.Tests/QueryAndMutationTests.cs b/tests/GraphQL.Integration.Tests/QueryAndMutationTests.cs index a44134e5..64ec475a 100644 --- a/tests/GraphQL.Integration.Tests/QueryAndMutationTests.cs +++ b/tests/GraphQL.Integration.Tests/QueryAndMutationTests.cs @@ -1,4 +1,6 @@ +using System.Net.Http; using GraphQL.Client; +using GraphQL.Client.Http; using GraphQL.Integration.Tests.Helpers; using GraphQL.Integration.Tests.TestData; using IntegrationTestServer; @@ -111,5 +113,24 @@ query Human($id: String!){ Assert.Equal("Han Solo", queryResponse.Data.Human.Name); } } + + [Fact] + public async void PreprocessHttpRequestMessageIsCalled() { + var callbackTester = new CallbackTester(); + var graphQLRequest = new GraphQLHttpRequest($"{{ human(id: \"1\") {{ name }} }}") { + PreprocessHttpRequestMessage = callbackTester.Callback + }; + + using (var setup = SetupTest()) { + var defaultHeaders = setup.Client.HttpClient.DefaultRequestHeaders; + var response = await setup.Client.SendQueryAsync(graphQLRequest, () => new { Human = new { Name = string.Empty } }) + .ConfigureAwait(false); + callbackTester.CallbackShouldHaveBeenInvoked(message => { + Assert.Equal(defaultHeaders, message.Headers); + }); + Assert.Null(response.Errors); + Assert.Equal("Luke", response.Data.Human.Name); + } + } } }