public async Task UseClientCertOnHttp2_OSSupportsIt_Success() { using X509Certificate2 clientCert = Test.Common.Configuration.Certificates.GetClientCertificate(); await Http2LoopbackServer.CreateClientAndServerAsync( async address => { var handler = new WinHttpHandler(); handler.ServerCertificateValidationCallback = CustomServerCertificateValidationCallback; handler.ClientCertificates.Add(clientCert); handler.ClientCertificateOption = ClientCertificateOption.Manual; using (var client = new HttpClient(handler)) using (HttpResponseMessage response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, address) { Version = HttpVersion20.Value })) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.True(_validationCallbackHistory.WasCalled); Assert.NotEmpty(_validationCallbackHistory.CertificateChain); Assert.Equal(Test.Common.Configuration.Certificates.GetServerCertificate(), _validationCallbackHistory.CertificateChain[0]); } }, async s => { await using (Http2LoopbackConnection connection = await s.EstablishConnectionAsync().ConfigureAwait(false)) { SslStream sslStream = connection.Stream as SslStream; Assert.NotNull(sslStream); Assert.True(sslStream.IsMutuallyAuthenticated); Assert.Equal(clientCert, sslStream.RemoteCertificate); int streamId = await connection.ReadRequestHeaderAsync(); await connection.SendDefaultResponseAsync(streamId); } }, new Http2Options { ClientCertificateRequired = true }); }
public async Task HPack_HeaderEncoding(string headerName, string expectedValue, byte[] expectedEncoding) { await Http2LoopbackServer.CreateClientAndServerAsync( async uri => { using HttpClient client = CreateHttpClient(); using HttpRequestMessage request = new HttpRequestMessage(); request.Method = HttpMethod.Post; request.RequestUri = uri; request.Version = HttpVersion.Version20; request.Content = new StringContent("testing 123"); request.Headers.Add(LiteralHeaderName, LiteralHeaderValue); (await client.SendAsync(request)).Dispose(); }, async server => { Http2LoopbackConnection connection = await server.EstablishConnectionAsync(); (int streamId, HttpRequestData requestData) = await connection.ReadAndParseRequestHeaderAsync(); HttpHeaderData header = requestData.Headers.Single(x => x.Name == headerName); Assert.Equal(expectedValue, header.Value); Assert.True(expectedEncoding.AsSpan().SequenceEqual(header.Raw)); await connection.SendDefaultResponseAsync(streamId); }); }
public async Task AltSvc_ConnectionFrame_UpgradeFrom20_Success() { // [ActiveIssue("https://github.com/dotnet/runtime/issues/54050")] if (UseQuicImplementationProvider == QuicImplementationProviders.Mock) { return; } using Http2LoopbackServer firstServer = Http2LoopbackServer.CreateServer(); using Http3LoopbackServer secondServer = CreateHttp3LoopbackServer(); using HttpClient client = CreateHttpClient(HttpVersion.Version20); Task <HttpResponseMessage> firstResponseTask = client.GetAsync(firstServer.Address); Task serverTask = Task.Run(async() => { using Http2LoopbackConnection connection = await firstServer.EstablishConnectionAsync(); int streamId = await connection.ReadRequestHeaderAsync(); await connection.WriteFrameAsync(new AltSvcFrame($"https://{firstServer.Address.IdnHost}:{firstServer.Address.Port}", $"h3=\"{secondServer.Address.IdnHost}:{secondServer.Address.Port}\"", streamId: 0)); await connection.SendDefaultResponseAsync(streamId); }); await new[] { firstResponseTask, serverTask }.WhenAllOrAnyFailed(30_000); HttpResponseMessage firstResponse = firstResponseTask.Result; Assert.True(firstResponse.IsSuccessStatusCode); await AltSvc_Upgrade_Success(firstServer, secondServer, client); }
public async Task KeepAlivePingDelay_Infinite_NoKeepAlivePingIsSent() { await Http2LoopbackServer.CreateClientAndServerAsync(async uri => { SocketsHttpHandler handler = new SocketsHttpHandler() { KeepAlivePingTimeout = TimeSpan.FromSeconds(1), KeepAlivePingPolicy = HttpKeepAlivePingPolicy.Always, KeepAlivePingDelay = Timeout.InfiniteTimeSpan }; handler.SslOptions.RemoteCertificateValidationCallback = delegate { return(true); }; using HttpClient client = new HttpClient(handler); client.DefaultRequestVersion = HttpVersion.Version20; // Warmup request to create connection: await client.GetStringAsync(uri); // Actual request: await client.GetStringAsync(uri); // Let connection live until server finishes: await _serverFinished.Task.WaitAsync(TestTimeout); }, async server => { await EstablishConnectionAsync(server); // Warmup the connection. int streamId1 = await ReadRequestHeaderAsync(); await GuardConnetionWriteAsync(() => _connection.SendDefaultResponseAsync(streamId1)); Interlocked.Exchange(ref _pingCounter, 0); // reset the PING counter // Request under the test scope. int streamId2 = await ReadRequestHeaderAsync(); // Simulate inactive period: await Task.Delay(5_000); // We may have received one RTT PING in response to HEADERS, but should receive no KeepAlive PING Assert.True(_pingCounter <= 1); Interlocked.Exchange(ref _pingCounter, 0); // reset the counter // Finish the response: await GuardConnetionWriteAsync(() => _connection.SendDefaultResponseAsync(streamId2)); // Simulate inactive period: await Task.Delay(5_000); // We may have received one RTT PING in response to HEADERS, but should receive no KeepAlive PING Assert.True(_pingCounter <= 1); await TerminateLoopbackConnectionAsync(); }).WaitAsync(TestTimeout); }
public async Task AltSvc_ConnectionFrame_UpgradeFrom20_Success() { using Http2LoopbackServer firstServer = Http2LoopbackServer.CreateServer(); using Http3LoopbackServer secondServer = new Http3LoopbackServer(); using HttpClient client = CreateHttpClient(CreateHttpClientHandler(HttpVersion.Version30)); Task <HttpResponseMessage> firstResponseTask = client.GetAsync(firstServer.Address); using (Http2LoopbackConnection connection = await firstServer.EstablishConnectionAsync()) { int streamId = await connection.ReadRequestHeaderAsync(); await connection.SendDefaultResponseAsync(streamId); await connection.WriteFrameAsync(new AltSvcFrame($"https://{firstServer.Address.IdnHost}:{firstServer.Address.Port}", $"h3={secondServer.Address.IdnHost}:{secondServer.Address.Port}", 0)); } HttpResponseMessage firstResponse = await firstResponseTask; Assert.True(firstResponse.IsSuccessStatusCode); await AltSvc_Upgrade_Success(firstServer, secondServer, client); }
public async Task AltSvc_ConnectionFrame_UpgradeFrom20_Success() { using Http2LoopbackServer firstServer = Http2LoopbackServer.CreateServer(); using Http3LoopbackServer secondServer = new Http3LoopbackServer(); using HttpClient client = CreateHttpClient(); Task <HttpResponseMessage> firstResponseTask = client.GetAsync(firstServer.Address); Task serverTask = Task.Run(async() => { using Http2LoopbackConnection connection = await firstServer.EstablishConnectionAsync(); int streamId = await connection.ReadRequestHeaderAsync(); await connection.WriteFrameAsync(new AltSvcFrame($"https://{firstServer.Address.IdnHost}:{firstServer.Address.Port}", $"h3=\"{secondServer.Address.IdnHost}:{secondServer.Address.Port}\"", streamId: 0)); await connection.SendDefaultResponseAsync(streamId); }); await new[] { firstResponseTask, serverTask }.WhenAllOrAnyFailed(30_000); HttpResponseMessage firstResponse = firstResponseTask.Result; Assert.True(firstResponse.IsSuccessStatusCode); await AltSvc_Upgrade_Success(firstServer, secondServer, client); }