Skip to content

Commit b4c6c82

Browse files
authored
Fix default behavior for 401/403 http response codes (#429)
* Fix default behavior for 401/403 http response codes * Add Accept and Accept-Charset headers * fix default user agent version * add note
1 parent 758ede0 commit b4c6c82

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/GraphQL.Client/GraphQLHttpClientOptions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ public class GraphQLHttpClientOptions
5252

5353
/// <summary>
5454
/// Delegate to determine if GraphQL response may be properly deserialized into <see cref="GraphQLResponse{T}"/>.
55+
/// Note that compatible to the draft graphql-over-http spec GraphQL Server MAY return 4xx status codes (401/403, etc.)
56+
/// with well-formed GraphQL response containing errors collection.
5557
/// </summary>
56-
public Func<HttpResponseMessage, bool> IsValidResponseToDeserialize { get; set; } = r => r.IsSuccessStatusCode || r.StatusCode == HttpStatusCode.BadRequest;
58+
public Func<HttpResponseMessage, bool> IsValidResponseToDeserialize { get; set; } = r =>
59+
// Why not application/json? See https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#processing-the-response
60+
r.IsSuccessStatusCode || r.StatusCode == HttpStatusCode.BadRequest || r.Content.Headers.ContentType.MediaType == "application/graphql+json";
5761

5862
/// <summary>
5963
/// This callback is called after successfully establishing a websocket connection but before any regular request is made.

src/GraphQL.Client/GraphQLHttpRequest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Net.Http.Headers;
12
using System.Runtime.Serialization;
23
using System.Text;
34
using GraphQL.Client.Abstractions;
@@ -38,6 +39,9 @@ public virtual HttpRequestMessage ToHttpRequestMessage(GraphQLHttpClientOptions
3839
{
3940
Content = new StringContent(serializer.SerializeToString(this), Encoding.UTF8, options.MediaType)
4041
};
42+
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/graphql+json"));
43+
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
44+
message.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8"));
4145

4246
#pragma warning disable CS0618 // Type or member is obsolete
4347
PreprocessHttpRequestMessage(message);

tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Net;
2+
using System.Net.Http.Headers;
23
using FluentAssertions;
34
using GraphQL.Client.Abstractions;
45
using GraphQL.Client.Http;
@@ -173,9 +174,13 @@ public async void PreprocessHttpRequestMessageIsCalled()
173174
#pragma warning restore CS0618 // Type or member is obsolete
174175
};
175176

176-
var defaultHeaders = StarWarsClient.HttpClient.DefaultRequestHeaders;
177+
var expectedHeaders = new HttpRequestMessage().Headers;
178+
expectedHeaders.UserAgent.Add(new ProductInfoHeaderValue("GraphQL.Client", typeof(GraphQLHttpClient).Assembly.GetName().Version.ToString()));
179+
expectedHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/graphql+json"));
180+
expectedHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
181+
expectedHeaders.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8"));
177182
var response = await StarWarsClient.SendQueryAsync(graphQLRequest, () => new { Human = new { Name = string.Empty } });
178-
callbackTester.Should().HaveBeenInvokedWithPayload().Which.Headers.Should().BeEquivalentTo(defaultHeaders);
183+
callbackTester.Should().HaveBeenInvokedWithPayload().Which.Headers.Should().BeEquivalentTo(expectedHeaders);
179184
Assert.Null(response.Errors);
180185
Assert.Equal("Luke", response.Data.Human.Name);
181186
}

0 commit comments

Comments
 (0)