Skip to content

Commit

Permalink
Fixed Content-Type selection in HTTP Transport. (#5878)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler authored and michaelstaib committed Feb 21, 2023
1 parent 6015f20 commit 66f3f9a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ protected virtual void OnWriteResponseHeaders(
if (resultKind is ResultKind.Single &&
start.Kind is AllApplication or All)
{
possibleFormat = _defaultFormat;
return _defaultFormat;
}

if (resultKind is ResultKind.Single &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,32 @@ public GraphQLOverHttpSpecTests(TestServerFactory serverFactory)
[InlineData("*/*", Legacy, ContentType.Json)]
[InlineData("application/*", Latest, ContentType.GraphQLResponse)]
[InlineData("application/*", Legacy, ContentType.Json)]
[InlineData("application/json, */*", Latest, ContentType.GraphQLResponse)]
[InlineData("application/json, */*", Legacy, ContentType.Json)]
[InlineData("application/json, application/*", Latest, ContentType.GraphQLResponse)]
[InlineData("application/json, application/*", Legacy, ContentType.Json)]
[InlineData("application/json, text/plain, */*", Latest, ContentType.GraphQLResponse)]
[InlineData("application/json, text/plain, */*", Legacy, ContentType.Json)]
[InlineData(ContentType.Json, Latest, ContentType.Json)]
[InlineData(ContentType.Json, Legacy, ContentType.Json)]
[InlineData(ContentType.GraphQLResponse, Latest, ContentType.GraphQLResponse)]
[InlineData(ContentType.GraphQLResponse, Legacy, ContentType.GraphQLResponse)]
[InlineData("application/graphql-response+json; charset=utf-8, multipart/mixed; charset=utf-8",
Latest, ContentType.GraphQLResponse)]
[InlineData("application/graphql-response+json; charset=utf-8, multipart/mixed; charset=utf-8",
Legacy, ContentType.GraphQLResponse)]
[InlineData("application/graphql-response+json, multipart/mixed", Latest, ContentType.GraphQLResponse)]
[InlineData("application/graphql-response+json, multipart/mixed", Legacy, ContentType.GraphQLResponse)]
[InlineData("multipart/mixed,application/graphql-response+json", Latest, ContentType.GraphQLResponse)]
[InlineData("multipart/mixed,application/graphql-response+json", Legacy, ContentType.GraphQLResponse)]
[InlineData("text/event-stream, multipart/mixed,application/json, application/graphql-response+json",
Latest, ContentType.GraphQLResponse)]
[InlineData("text/event-stream, multipart/mixed,application/json, application/graphql-response+json",
Legacy, ContentType.GraphQLResponse)]
[InlineData("application/graphql-response+json; charset=utf-8, application/json; charset=utf-8",
Latest, ContentType.GraphQLResponse)]
[InlineData("application/graphql-response+json; charset=utf-8, application/json; charset=utf-8",
Legacy, ContentType.GraphQLResponse)]
public async Task SingleResult_Success(string? acceptHeader, HttpTransportVersion transportVersion,
string expectedContentType)
{
Expand Down Expand Up @@ -59,8 +81,10 @@ public async Task SingleResult_Success(string? acceptHeader, HttpTransportVersio
@"{""data"":{""__typename"":""Query""}}");
}

[Fact]
public async Task SingleResult_MultipartMixedAcceptHeader()
[Theory]
[InlineData("multipart/mixed")]
[InlineData("multipart/*")]
public async Task SingleResult_MultipartAcceptHeader(string acceptHeader)
{
// arrange
var server = CreateStarWarsServer();
Expand All @@ -73,7 +97,7 @@ public async Task SingleResult_MultipartMixedAcceptHeader()
new ClientQueryRequest { Query = "{ __typename }" }),
Headers =
{
{ "Accept", $"{ContentType.Types.MultiPart}/{ContentType.SubTypes.Mixed}" }
{ "Accept", acceptHeader }
}
};

Expand All @@ -98,42 +122,6 @@ public async Task SingleResult_MultipartMixedAcceptHeader()
");
}

[Theory]
[InlineData("application/graphql-response+json; charset=utf-8, multipart/mixed; charset=utf-8")]
[InlineData("application/graphql-response+json, multipart/mixed")]
[InlineData("multipart/mixed,application/graphql-response+json")]
[InlineData("text/event-stream, multipart/mixed,application/json, application/graphql-response+json")]
[InlineData("application/graphql-response+json; charset=utf-8, application/json; charset=utf-8")]
public async Task SingleResult_ApplicationGraphQLIsPreferred(string acceptHeader)
{
// arrange
var server = CreateStarWarsServer();
var client = server.CreateClient();

// act
using var request = new HttpRequestMessage(HttpMethod.Post, _url)
{
Content = JsonContent.Create(
new ClientQueryRequest { Query = "{ __typename }" })
};

request.Headers.TryAddWithoutValidation("Accept", acceptHeader);

using var response = await client.SendAsync(request);

// assert
Snapshot
.Create()
.Add(response)
.MatchInline(
@"Headers:
Content-Type: application/graphql-response+json; charset=utf-8
-------------------------->
Status Code: OK
-------------------------->
{""data"":{""__typename"":""Query""}}");
}

[Theory]
[InlineData(null, Latest, BadRequest, ContentType.GraphQLResponse)]
[InlineData(null, Legacy, OK, ContentType.Json)]
Expand Down

0 comments on commit 66f3f9a

Please sign in to comment.