Skip to content

Commit

Permalink
Fixed transport compatibility issues (#5410)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Sep 19, 2022
1 parent fef8097 commit 3762806
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/HotChocolate/AspNetCore/src/AspNetCore/ContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static class ContentType
private const string _boundary = "boundary=\"-\"";
public const string GraphQL = $"{Types.Application}/{SubTypes.GraphQL};{_utf8}";
public const string Json = $"{Types.Application}/{SubTypes.Json};{_utf8}";
public const string MultiPartMixed = $"{Types.MultiPart}/{SubTypes.Mixed};{_utf8};{_boundary}";
public const string MultiPartMixed = $"{Types.MultiPart}/{SubTypes.Mixed};{_boundary}";
public const string GraphQLResponse = $"{Types.Application}/{SubTypes.GraphQLResponse};{_utf8}";
public const string EventStream = $"{Types.Text}/{SubTypes.EventStream};{_utf8}";

Expand Down
13 changes: 12 additions & 1 deletion src/HotChocolate/AspNetCore/src/AspNetCore/HeaderUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static AcceptHeaderResult GetAcceptHeader(HttpRequest request)
return new AcceptHeaderResult(Array.Empty<AcceptMediaType>());
}

string[] innerArray;

if (count == 1)
{
var headerValue = value[0]!;
Expand All @@ -51,10 +53,19 @@ public static AcceptHeaderResult GetAcceptHeader(HttpRequest request)
return new AcceptHeaderResult(new[] { parsedValue });
}

// note: this is a workaround for now. we need to parse this properly.
if (headerValue.IndexOf(',', StringComparison.Ordinal) != -1)
{
innerArray = headerValue.Split(',');
goto MULTI_VALUES;
}

return new AcceptHeaderResult(headerValue);
}

string[] innerArray = value!;
innerArray = value!;

MULTI_VALUES:
ref var searchSpace = ref MemoryMarshal.GetReference(innerArray.AsSpan());
var parsedValues = new AcceptMediaType[innerArray.Length];
var p = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public async Task Legacy_With_Stream_1()
.Add(response)
.MatchInline(
@"Headers:
Content-Type: multipart/mixed; boundary=""-""; charset=utf-8
Content-Type: multipart/mixed; boundary=""-""
-------------------------->
Status Code: OK
-------------------------->
Expand Down Expand Up @@ -321,7 +321,7 @@ public async Task New_Query_No_Streams_3()
.Add(response)
.MatchInline(
@"Headers:
Content-Type: multipart/mixed; boundary=""-""; charset=utf-8
Content-Type: multipart/mixed; boundary=""-""
-------------------------->
Status Code: OK
-------------------------->
Expand Down Expand Up @@ -710,6 +710,50 @@ public async Task New_Query_No_Streams_12()
@"is supported."",""extensions"":{""code"":""HC0063""}}]}");
}

/// <summary>
/// This request specifies the application/graphql-response+json; charset=utf-8,
/// multipart/mixed; charset=utf-8 content types as accept header value.
/// expected response content-type: application/graphql-response+json
/// expected status code: 200
/// </summary>
[Fact]
public async Task New_Query_No_Streams_13()
{
// 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",
"application/graphql-response+json; charset=utf-8, multipart/mixed; charset=utf-8");

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

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

/// <summary>
/// This request specifies the application/graphql-response+json and
/// the multipart/mixed content type as accept header value.
Expand Down Expand Up @@ -752,7 +796,7 @@ public async Task New_Query_With_Streams_1()
.Add(response)
.MatchInline(
@"Headers:
Content-Type: multipart/mixed; boundary=""-""; charset=utf-8
Content-Type: multipart/mixed; boundary=""-""
-------------------------->
Status Code: OK
-------------------------->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"ContentType": "multipart/mixed; boundary=\"-\"; charset=utf-8",
"ContentType": "multipart/mixed; boundary=\"-\"",
"StatusCode": "OK",
"Content": "\r\n---\r\nContent-Type: application/json; charset=utf-8\r\n\r\n{\"data\":{\"hero\":{\"name\":\"R2-D2\"}},\"hasNext\":true}\r\n---\r\nContent-Type: application/json; charset=utf-8\r\n\r\n{\"incremental\":[{\"data\":{\"id\":\"2001\"},\"label\":\"my_id\",\"path\":[\"hero\"]}],\"hasNext\":true}\r\n---\r\nContent-Type: application/json; charset=utf-8\r\n\r\n{\"incremental\":[{\"data\":{\"wait\":true},\"path\":[]}],\"hasNext\":false}\r\n-----\r\n"
}

0 comments on commit 3762806

Please sign in to comment.