|
| 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; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Linq; |
| 8 | +using System.Net; |
| 9 | +using System.Net.Http; |
| 10 | +using System.Net.Quic; |
| 11 | +using System.Text; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using Microsoft.AspNetCore.Http; |
| 14 | +using Microsoft.AspNetCore.Server.IIS.FunctionalTests; |
| 15 | +using Microsoft.AspNetCore.Server.IntegrationTesting.Common; |
| 16 | +using Microsoft.AspNetCore.Server.IntegrationTesting.IIS; |
| 17 | +using Microsoft.AspNetCore.Testing; |
| 18 | +using Microsoft.Extensions.Hosting; |
| 19 | +using Microsoft.Extensions.Logging; |
| 20 | +using Microsoft.Net.Http.Headers; |
| 21 | +using Microsoft.Win32; |
| 22 | +using Xunit; |
| 23 | + |
| 24 | +namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests |
| 25 | +{ |
| 26 | + [MsQuicSupported] |
| 27 | + [HttpSysHttp3Supported] |
| 28 | + [Collection(IISHttpsTestSiteCollection.Name)] |
| 29 | + public class Http3Tests |
| 30 | + { |
| 31 | + public Http3Tests(IISTestSiteFixture fixture) |
| 32 | + { |
| 33 | + var port = TestPortHelper.GetNextSSLPort(); |
| 34 | + fixture.DeploymentParameters.ApplicationBaseUriHint = $"https://localhost:{port}/"; |
| 35 | + fixture.DeploymentParameters.AddHttpsToServerConfig(); |
| 36 | + fixture.DeploymentParameters.SetWindowsAuth(false); |
| 37 | + Fixture = fixture; |
| 38 | + } |
| 39 | + |
| 40 | + public IISTestSiteFixture Fixture { get; } |
| 41 | + |
| 42 | + [ConditionalFact] |
| 43 | + public async Task Http3_Direct() |
| 44 | + { |
| 45 | + using var client = SetUpClient(); |
| 46 | + client.DefaultRequestVersion = HttpVersion.Version30; |
| 47 | + client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact; |
| 48 | + var response = await client.GetAsync(Fixture.Client.BaseAddress.ToString() + "Http3_Direct"); |
| 49 | + |
| 50 | + response.EnsureSuccessStatusCode(); |
| 51 | + Assert.Equal(HttpVersion.Version30, response.Version); |
| 52 | + Assert.Equal("HTTP/3", await response.Content.ReadAsStringAsync()); |
| 53 | + } |
| 54 | + |
| 55 | + [ConditionalFact] |
| 56 | + public async Task Http3_AltSvcHeader_UpgradeFromHttp1() |
| 57 | + { |
| 58 | + var address = Fixture.Client.BaseAddress.ToString() + "Http3_AltSvcHeader_UpgradeFromHttp1"; |
| 59 | + |
| 60 | + var altsvc = $@"h3="":{new Uri(address).Port}"""; |
| 61 | + using var client = SetUpClient(); |
| 62 | + client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher; |
| 63 | +
|
| 64 | + // First request is HTTP/1.1, gets an alt-svc response |
| 65 | + var request = new HttpRequestMessage(HttpMethod.Get, address); |
| 66 | + request.Version = HttpVersion.Version11; |
| 67 | + request.VersionPolicy = HttpVersionPolicy.RequestVersionExact; |
| 68 | + var response1 = await client.SendAsync(request); |
| 69 | + response1.EnsureSuccessStatusCode(); |
| 70 | + Assert.Equal("HTTP/1.1", await response1.Content.ReadAsStringAsync()); |
| 71 | + Assert.Equal(altsvc, response1.Headers.GetValues(HeaderNames.AltSvc).SingleOrDefault()); |
| 72 | + |
| 73 | + // Second request is HTTP/3 |
| 74 | + var response3 = await client.GetAsync(address); |
| 75 | + Assert.Equal(HttpVersion.Version30, response3.Version); |
| 76 | + Assert.Equal("HTTP/3", await response3.Content.ReadAsStringAsync()); |
| 77 | + } |
| 78 | + |
| 79 | + [ConditionalFact] |
| 80 | + public async Task Http3_AltSvcHeader_UpgradeFromHttp2() |
| 81 | + { |
| 82 | + var address = Fixture.Client.BaseAddress.ToString() + "Http3_AltSvcHeader_UpgradeFromHttp2"; |
| 83 | + |
| 84 | + var altsvc = $@"h3="":{new Uri(address).Port}"""; |
| 85 | + using var client = SetUpClient(); |
| 86 | + client.DefaultRequestVersion = HttpVersion.Version20; |
| 87 | + client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher; |
| 88 | +
|
| 89 | + // First request is HTTP/2, gets an alt-svc response |
| 90 | + var response2 = await client.GetAsync(address); |
| 91 | + response2.EnsureSuccessStatusCode(); |
| 92 | + Assert.Equal(altsvc, response2.Headers.GetValues(HeaderNames.AltSvc).SingleOrDefault()); |
| 93 | + Assert.Equal("HTTP/2", await response2.Content.ReadAsStringAsync()); |
| 94 | + |
| 95 | + // Second request is HTTP/3 |
| 96 | + var response3 = await client.GetStringAsync(address); |
| 97 | + Assert.Equal("HTTP/3", response3); |
| 98 | + } |
| 99 | + |
| 100 | + [ConditionalFact] |
| 101 | + public async Task Http3_ResponseTrailers() |
| 102 | + { |
| 103 | + var address = Fixture.Client.BaseAddress.ToString() + "Http3_ResponseTrailers"; |
| 104 | + using var client = SetUpClient(); |
| 105 | + client.DefaultRequestVersion = HttpVersion.Version30; |
| 106 | + client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact; |
| 107 | + var response = await client.GetAsync(address); |
| 108 | + response.EnsureSuccessStatusCode(); |
| 109 | + var result = await response.Content.ReadAsStringAsync(); |
| 110 | + Assert.Equal("HTTP/3", result); |
| 111 | + Assert.Equal("value", response.TrailingHeaders.GetValues("custom").SingleOrDefault()); |
| 112 | + } |
| 113 | + |
| 114 | + [ConditionalFact] |
| 115 | + public async Task Http3_ResetBeforeHeaders() |
| 116 | + { |
| 117 | + var address = Fixture.Client.BaseAddress.ToString() + "Http3_ResetBeforeHeaders"; |
| 118 | + using var client = SetUpClient(); |
| 119 | + client.DefaultRequestVersion = HttpVersion.Version30; |
| 120 | + client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact; |
| 121 | + var ex = await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync(address)); |
| 122 | + var qex = Assert.IsType<QuicStreamAbortedException>(ex.InnerException); |
| 123 | + Assert.Equal(0x010b, qex.ErrorCode); |
| 124 | + } |
| 125 | + |
| 126 | + [ConditionalFact] |
| 127 | + public async Task Http3_ResetAfterHeaders() |
| 128 | + { |
| 129 | + var address = Fixture.Client.BaseAddress.ToString() + "Http3_ResetAfterHeaders"; |
| 130 | + using var client = SetUpClient(); |
| 131 | + client.DefaultRequestVersion = HttpVersion.Version30; |
| 132 | + client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact; |
| 133 | + var response = await client.GetAsync(address, HttpCompletionOption.ResponseHeadersRead); |
| 134 | + await client.GetAsync(Fixture.Client.BaseAddress.ToString() + "Http3_ResetAfterHeaders_SetResult"); |
| 135 | + response.EnsureSuccessStatusCode(); |
| 136 | + var ex = await Assert.ThrowsAsync<HttpRequestException>(() => response.Content.ReadAsStringAsync()); |
| 137 | + var qex = Assert.IsType<QuicStreamAbortedException>(ex.InnerException?.InnerException?.InnerException); |
| 138 | + Assert.Equal(0x010c, qex.ErrorCode); // H3_REQUEST_CANCELLED |
| 139 | + } |
| 140 | + |
| 141 | + [ConditionalFact] |
| 142 | + public async Task Http3_AppExceptionAfterHeaders_InternalError() |
| 143 | + { |
| 144 | + var address = Fixture.Client.BaseAddress.ToString() + "Http3_AppExceptionAfterHeaders_InternalError"; |
| 145 | + using var client = SetUpClient(); |
| 146 | + client.DefaultRequestVersion = HttpVersion.Version30; |
| 147 | + client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact; |
| 148 | + |
| 149 | + var response = await client.GetAsync(address, HttpCompletionOption.ResponseHeadersRead); |
| 150 | + await client.GetAsync(Fixture.Client.BaseAddress.ToString() + "Http3_AppExceptionAfterHeaders_InternalError_SetResult"); |
| 151 | + response.EnsureSuccessStatusCode(); |
| 152 | + var ex = await Assert.ThrowsAsync<HttpRequestException>(() => response.Content.ReadAsStringAsync()); |
| 153 | + var qex = Assert.IsType<QuicStreamAbortedException>(ex.InnerException?.InnerException?.InnerException); |
| 154 | + Assert.Equal(0x0102, qex.ErrorCode); // H3_INTERNAL_ERROR |
| 155 | + } |
| 156 | + |
| 157 | + [ConditionalFact] |
| 158 | + public async Task Http3_Abort_Cancel() |
| 159 | + { |
| 160 | + var address = Fixture.Client.BaseAddress.ToString() + "Http3_Abort_Cancel"; |
| 161 | + using var client = SetUpClient(); |
| 162 | + client.DefaultRequestVersion = HttpVersion.Version30; |
| 163 | + client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact; |
| 164 | + |
| 165 | + var ex = await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync(address)); |
| 166 | + var qex = Assert.IsType<QuicStreamAbortedException>(ex.InnerException); |
| 167 | + Assert.Equal(0x010c, qex.ErrorCode); // H3_REQUEST_CANCELLED |
| 168 | + } |
| 169 | + |
| 170 | + private HttpClient SetUpClient() |
| 171 | + { |
| 172 | + var handler = new HttpClientHandler(); |
| 173 | + // Needed on CI, the IIS Express cert we use isn't trusted there. |
| 174 | + handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; |
| 175 | + return new HttpClient(handler); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments