Skip to content

Commit

Permalink
Fix default behavior for 401/403 http response codes (#429)
Browse files Browse the repository at this point in the history
* Fix default behavior for 401/403 http response codes

* Add Accept and Accept-Charset headers

* fix default user agent version

* add note
  • Loading branch information
sungam3r authored Aug 1, 2022
1 parent 758ede0 commit b4c6c82
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/GraphQL.Client/GraphQLHttpClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public class GraphQLHttpClientOptions

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

/// <summary>
/// This callback is called after successfully establishing a websocket connection but before any regular request is made.
Expand Down
4 changes: 4 additions & 0 deletions src/GraphQL.Client/GraphQLHttpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net.Http.Headers;
using System.Runtime.Serialization;
using System.Text;
using GraphQL.Client.Abstractions;
Expand Down Expand Up @@ -38,6 +39,9 @@ public virtual HttpRequestMessage ToHttpRequestMessage(GraphQLHttpClientOptions
{
Content = new StringContent(serializer.SerializeToString(this), Encoding.UTF8, options.MediaType)
};
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/graphql+json"));
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
message.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8"));

#pragma warning disable CS0618 // Type or member is obsolete
PreprocessHttpRequestMessage(message);
Expand Down
9 changes: 7 additions & 2 deletions tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Net.Http.Headers;
using FluentAssertions;
using GraphQL.Client.Abstractions;
using GraphQL.Client.Http;
Expand Down Expand Up @@ -173,9 +174,13 @@ public async void PreprocessHttpRequestMessageIsCalled()
#pragma warning restore CS0618 // Type or member is obsolete
};

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

0 comments on commit b4c6c82

Please sign in to comment.