|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Net.Test.Common; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Xunit; |
| 9 | +using Xunit.Abstractions; |
| 10 | + |
| 11 | +namespace System.Net.Http.Functional.Tests |
| 12 | +{ |
| 13 | + [ConditionalClass(typeof(SocketsHttpHandler), nameof(SocketsHttpHandler.IsSupported))] |
| 14 | + public sealed class SocketsHttpHandler_Http2ExtendedConnect_Test : HttpClientHandlerTestBase |
| 15 | + { |
| 16 | + public SocketsHttpHandler_Http2ExtendedConnect_Test(ITestOutputHelper output) : base(output) { } |
| 17 | + |
| 18 | + protected override Version UseVersion => HttpVersion.Version20; |
| 19 | + |
| 20 | + public static IEnumerable<object[]> UseSsl_MemberData() |
| 21 | + { |
| 22 | + yield return new object[] { false }; |
| 23 | + |
| 24 | + if (PlatformDetection.SupportsAlpn) |
| 25 | + { |
| 26 | + yield return new object[] { true }; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + [Theory] |
| 31 | + [MemberData(nameof(UseSsl_MemberData))] |
| 32 | + public async Task Connect_ReadWriteResponseStream(bool useSsl) |
| 33 | + { |
| 34 | + byte[] clientMessage = new byte[] { 1, 2, 3 }; |
| 35 | + byte[] serverMessage = new byte[] { 4, 5, 6, 7 }; |
| 36 | + |
| 37 | + TaskCompletionSource clientCompleted = new(TaskCreationOptions.RunContinuationsAsynchronously); |
| 38 | + |
| 39 | + await Http2LoopbackServerFactory.Singleton.CreateClientAndServerAsync(async uri => |
| 40 | + { |
| 41 | + using HttpClient client = CreateHttpClient(); |
| 42 | + |
| 43 | + HttpRequestMessage request = CreateRequest(HttpMethod.Connect, uri, UseVersion, exactVersion: true); |
| 44 | + request.Headers.Protocol = "foo"; |
| 45 | + |
| 46 | + using HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); |
| 47 | + |
| 48 | + using Stream responseStream = await response.Content.ReadAsStreamAsync(); |
| 49 | + |
| 50 | + await responseStream.WriteAsync(clientMessage); |
| 51 | + await responseStream.FlushAsync(); |
| 52 | + |
| 53 | + byte[] readBuffer = new byte[serverMessage.Length]; |
| 54 | + await responseStream.ReadExactlyAsync(readBuffer); |
| 55 | + Assert.Equal(serverMessage, readBuffer); |
| 56 | + |
| 57 | + // Receive server's EOS |
| 58 | + Assert.Equal(0, await responseStream.ReadAsync(readBuffer)); |
| 59 | + |
| 60 | + clientCompleted.SetResult(); |
| 61 | + }, |
| 62 | + async server => |
| 63 | + { |
| 64 | + await using Http2LoopbackConnection connection = await ((Http2LoopbackServer)server).EstablishConnectionAsync(new SettingsEntry { SettingId = SettingId.EnableConnect, Value = 1 }); |
| 65 | + |
| 66 | + (int streamId, _) = await connection.ReadAndParseRequestHeaderAsync(readBody: false); |
| 67 | + |
| 68 | + await connection.SendResponseHeadersAsync(streamId, endStream: false).ConfigureAwait(false); |
| 69 | + |
| 70 | + DataFrame dataFrame = await connection.ReadDataFrameAsync(); |
| 71 | + Assert.Equal(clientMessage, dataFrame.Data.ToArray()); |
| 72 | + |
| 73 | + await connection.SendResponseDataAsync(streamId, serverMessage, endStream: true); |
| 74 | + |
| 75 | + await clientCompleted.Task.WaitAsync(TestHelper.PassingTestTimeout); |
| 76 | + }, options: new GenericLoopbackOptions { UseSsl = useSsl }); |
| 77 | + } |
| 78 | + |
| 79 | + [Theory] |
| 80 | + [MemberData(nameof(UseSsl_MemberData))] |
| 81 | + public async Task Connect_ServerDoesNotSupportExtendedConnect_ClientIncludesExceptionData(bool useSsl) |
| 82 | + { |
| 83 | + TaskCompletionSource clientCompleted = new(TaskCreationOptions.RunContinuationsAsynchronously); |
| 84 | + |
| 85 | + await LoopbackServerFactory.CreateClientAndServerAsync(async uri => |
| 86 | + { |
| 87 | + using HttpClient client = CreateHttpClient(); |
| 88 | + |
| 89 | + HttpRequestMessage request = CreateRequest(HttpMethod.Connect, uri, UseVersion, exactVersion: true); |
| 90 | + request.Headers.Protocol = "foo"; |
| 91 | + |
| 92 | + HttpRequestException ex = await Assert.ThrowsAsync<HttpRequestException>(() => client.SendAsync(request)); |
| 93 | + |
| 94 | + Assert.Equal(false, ex.Data["SETTINGS_ENABLE_CONNECT_PROTOCOL"]); |
| 95 | + |
| 96 | + clientCompleted.SetResult(); |
| 97 | + }, |
| 98 | + async server => |
| 99 | + { |
| 100 | + try |
| 101 | + { |
| 102 | + await server.AcceptConnectionAsync(async connection => |
| 103 | + { |
| 104 | + await clientCompleted.Task.WaitAsync(TestHelper.PassingTestTimeout); |
| 105 | + }); |
| 106 | + } |
| 107 | + catch (Exception ex) |
| 108 | + { |
| 109 | + _output.WriteLine($"Ignoring exception {ex}"); |
| 110 | + } |
| 111 | + }, options: new GenericLoopbackOptions { UseSsl = useSsl }); |
| 112 | + } |
| 113 | + |
| 114 | + [Theory] |
| 115 | + [InlineData(true)] |
| 116 | + [InlineData(false)] |
| 117 | + public async Task Connect_Http11Endpoint_Throws(bool useSsl) |
| 118 | + { |
| 119 | + using var server = new LoopbackServer(new LoopbackServer.Options |
| 120 | + { |
| 121 | + UseSsl = useSsl |
| 122 | + }); |
| 123 | + |
| 124 | + await server.ListenAsync(); |
| 125 | + |
| 126 | + TaskCompletionSource clientCompleted = new(TaskCreationOptions.RunContinuationsAsynchronously); |
| 127 | + |
| 128 | + Task serverTask = Task.Run(async () => |
| 129 | + { |
| 130 | + try |
| 131 | + { |
| 132 | + await server.AcceptConnectionAsync(async connection => |
| 133 | + { |
| 134 | + if (!useSsl) |
| 135 | + { |
| 136 | + byte[] http2GoAwayHttp11RequiredBytes = new byte[17] { 0, 0, 8, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13 }; |
| 137 | + |
| 138 | + await connection.SendResponseAsync(http2GoAwayHttp11RequiredBytes); |
| 139 | + |
| 140 | + await clientCompleted.Task.WaitAsync(TestHelper.PassingTestTimeout); |
| 141 | + } |
| 142 | + }); |
| 143 | + } |
| 144 | + catch (Exception ex) |
| 145 | + { |
| 146 | + _output.WriteLine($"Ignoring exception {ex}"); |
| 147 | + } |
| 148 | + }); |
| 149 | + |
| 150 | + Task clientTask = Task.Run(async () => |
| 151 | + { |
| 152 | + using HttpClient client = CreateHttpClient(); |
| 153 | + |
| 154 | + HttpRequestMessage request = CreateRequest(HttpMethod.Connect, server.Address, UseVersion, exactVersion: true); |
| 155 | + request.Headers.Protocol = "foo"; |
| 156 | + |
| 157 | + Exception ex = await Assert.ThrowsAnyAsync<Exception>(() => client.SendAsync(request)); |
| 158 | + clientCompleted.SetResult(); |
| 159 | + |
| 160 | + if (useSsl) |
| 161 | + { |
| 162 | + Assert.Equal(false, ex.Data["HTTP2_ENABLED"]); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + await new[] { serverTask, clientTask }.WhenAllOrAnyFailed().WaitAsync(TestHelper.PassingTestTimeout); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments