@@ -26,6 +26,8 @@ public BidirectionStreamingTest(ITestOutputHelper output) : base(output)
26
26
27
27
public static bool TestsEnabled => OsSupportsWinHttpBidirectionalStreaming && PlatformDetection . SupportsAlpn ;
28
28
29
+ public static bool TestsBackwardsCompatibilityEnabled => ! OsSupportsWinHttpBidirectionalStreaming && PlatformDetection . SupportsAlpn ;
30
+
29
31
protected override Version UseVersion => new Version ( 2 , 0 ) ;
30
32
31
33
protected static byte [ ] DataBytes = Encoding . ASCII . GetBytes ( "data" ) ;
@@ -311,6 +313,32 @@ async Task RunServer()
311
313
}
312
314
}
313
315
316
+ [ ConditionalFact ( nameof ( TestsBackwardsCompatibilityEnabled ) ) ]
317
+ public async Task BackwardsCompatibility_DowngradeToHttp11 ( )
318
+ {
319
+ TaskCompletionSource < object > completeStreamTcs = new TaskCompletionSource < object > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
320
+
321
+ using ( Http2LoopbackServer server = Http2LoopbackServer . CreateServer ( ) )
322
+ using ( HttpClient client = CreateHttpClient ( ) )
323
+ {
324
+ HttpRequestMessage message = new HttpRequestMessage ( HttpMethod . Post , server . Address ) ;
325
+ message . Version = new Version ( 2 , 0 ) ;
326
+ message . Content = new StreamingContent ( async s =>
327
+ {
328
+ await completeStreamTcs . Task ;
329
+ } ) ;
330
+
331
+ Task < HttpResponseMessage > sendTask = client . SendAsync ( message , HttpCompletionOption . ResponseHeadersRead ) ;
332
+
333
+ // If WinHTTP doesn't support streaming a request without a length then it will fallback
334
+ // to HTTP/1.1. This is pretty weird behavior but we keep it for backwards compatibility.
335
+ Exception ex = await Assert . ThrowsAsync < Exception > ( async ( ) => await server . EstablishConnectionAsync ( ) ) ;
336
+ Assert . Equal ( "HTTP/1.1 request sent to HTTP/2 connection." , ex . Message ) ;
337
+
338
+ completeStreamTcs . SetResult ( null ) ;
339
+ }
340
+ }
341
+
314
342
private class StreamingContent : HttpContent
315
343
{
316
344
private readonly Func < Stream , Task > _writeFunc ;
0 commit comments