11using System . Net ;
22using System . Net . Http . Headers ;
3+ using System . Net . Http . Json ;
34using System . Net . Sockets ;
45using System . Text ;
56using System . Text . Json . Nodes ;
@@ -528,4 +529,34 @@ public async Task GraphQL_Endpoint_FollowSpec_Supports_Header_Not_First()
528529 "Response text did not contain an expected Content-Type header. Received: " + responseText
529530 ) ;
530531 }
531- }
532+
533+ [ Fact ]
534+ public async Task GraphQL_Endpoint_200_On_Chunked_Data_Query ( )
535+ {
536+ // The second, real Kestrel-based WebApplication
537+ WebApplication realApp = _factory . RealApp ?? throw new InvalidOperationException ( "RealApp is null." ) ;
538+
539+ // Get the real ephemeral port
540+ IServer server = realApp . Services . GetRequiredService < IServer > ( ) ;
541+ IServerAddressesFeature addresses = server . Features . Get < IServerAddressesFeature > ( ) ! ;
542+ string address = addresses . Addresses . First ( ) ; // e.g. http://127.0.0.1:12345
543+ Uri uri = new ( address ) ;
544+
545+ HttpClient client = new ( ) ;
546+ client . BaseAddress = new Uri ( $ "http://{ uri . Host } :{ uri . Port } ") ;
547+ client . DefaultRequestHeaders . Add ( "Accept" , "*/*" ) ;
548+ // PostAsJsonAsync adds the following header:
549+ // Transfer-Encoding = chunked
550+ // but ContentLength isn't forwarded, hence it's null or zero
551+ // if PostAsJsonAsync is used with WebApplicationFactory,
552+ // i.e. the in-memory test server, with no network involved,
553+ // it works fine and the ContentLength is set correctly.
554+ // hence, the CustomWebApplicationFactory is used to ensure
555+ // network communication is used, and the ContentLength is not set.
556+ HttpResponseMessage resp = await client . PostAsJsonAsync ( "/graphql" , new { query = "{ hello }" } ) ;
557+ resp . EnsureSuccessStatusCode ( ) ;
558+
559+ string json = await resp . Content . ReadAsStringAsync ( ) ;
560+ Assert . Contains ( "\" hello\" :\" world\" " , json ) ;
561+ }
562+ }
0 commit comments