Skip to content

Commit d1d65df

Browse files
committed
Backwards compatibility test
1 parent 8e94ebb commit d1d65df

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/libraries/Common/tests/System/Net/Http/Http2LoopbackConnection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ private async Task ReadPrefixAsync()
100100
// The contents of what we send don't really matter, as long as it is interpreted by SocketsHttpHandler as an invalid response.
101101
await _connectionStream.WriteAsync(Encoding.ASCII.GetBytes("HTTP/2.0 400 Bad Request\r\n\r\n"));
102102
_connectionSocket.Shutdown(SocketShutdown.Send);
103+
104+
throw new Exception("HTTP/1.1 request sent to HTTP/2 connection.");
103105
}
104106
}
105107

src/libraries/System.Net.Http.WinHttpHandler/tests/FunctionalTests/BidirectionStreamingTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public BidirectionStreamingTest(ITestOutputHelper output) : base(output)
2626

2727
public static bool TestsEnabled => OsSupportsWinHttpBidirectionalStreaming && PlatformDetection.SupportsAlpn;
2828

29+
public static bool TestsBackwardsCompatibilityEnabled => !OsSupportsWinHttpBidirectionalStreaming && PlatformDetection.SupportsAlpn;
30+
2931
protected override Version UseVersion => new Version(2, 0);
3032

3133
protected static byte[] DataBytes = Encoding.ASCII.GetBytes("data");
@@ -311,6 +313,32 @@ async Task RunServer()
311313
}
312314
}
313315

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+
314342
private class StreamingContent : HttpContent
315343
{
316344
private readonly Func<Stream, Task> _writeFunc;

0 commit comments

Comments
 (0)