Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ public static IEndpointRouteBuilder MapGraphQL<TQueryType>(
context.Response.StatusCode = StatusCodes.Status415UnsupportedMediaType;
return;
}
if (context.Request.ContentLength == null || context.Request.ContentLength == 0)
var isChunked = context.Request.Headers.TransferEncoding
.Any(h => h is not null && h.Equals("chunked", StringComparison.OrdinalIgnoreCase));

if (!isChunked && (context.Request.ContentLength == null ||
context.Request.ContentLength == 0))
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Net.Sockets;
using System.Text;
using System.Text.Json.Nodes;
Expand Down Expand Up @@ -528,4 +529,34 @@ public async Task GraphQL_Endpoint_FollowSpec_Supports_Header_Not_First()
"Response text did not contain an expected Content-Type header. Received: " + responseText
);
}
}

[Fact]
public async Task GraphQL_Endpoint_200_On_Chunked_Data_Query()
{
// The second, real Kestrel-based WebApplication
WebApplication realApp = _factory.RealApp ?? throw new InvalidOperationException("RealApp is null.");

// Get the real ephemeral port
IServer server = realApp.Services.GetRequiredService<IServer>();
IServerAddressesFeature addresses = server.Features.Get<IServerAddressesFeature>()!;
string address = addresses.Addresses.First(); // e.g. http://127.0.0.1:12345
Uri uri = new(address);

HttpClient client = new();
client.BaseAddress = new Uri($"http://{uri.Host}:{uri.Port}");
client.DefaultRequestHeaders.Add("Accept", "*/*");
// PostAsJsonAsync adds the following header:
// Transfer-Encoding = chunked
// but ContentLength isn't forwarded, hence it's null or zero
// if PostAsJsonAsync is used with WebApplicationFactory,
// i.e. the in-memory test server, with no network involved,
// it works fine and the ContentLength is set correctly.
// hence, the CustomWebApplicationFactory is used to ensure
// network communication is used, and the ContentLength is not set.
HttpResponseMessage resp = await client.PostAsJsonAsync("/graphql", new { query = "{ hello }" });
resp.EnsureSuccessStatusCode();

string json = await resp.Content.ReadAsStringAsync();
Assert.Contains("\"hello\":\"world\"", json);
}
}