| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952 |
- // Copyright (c) .NET Foundation. All rights reserved.
- // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Http.Features;
- using Microsoft.AspNetCore.Http2Cat;
- using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2;
- using Microsoft.AspNetCore.Testing;
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Logging;
- using Microsoft.Net.Http.Headers;
- using Xunit;
- namespace Microsoft.AspNetCore.Server.HttpSys.FunctionalTests
- {
- public class Http2Tests
- {
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10, SkipReason = "Http2 requires Win10")]
- public async Task EmptyResponse_200()
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
- {
- var feature = httpContext.Features.Get<IHttpUpgradeFeature>();
- Assert.False(feature.IsUpgradableRequest);
- Assert.False(httpContext.Request.CanHaveBody());
- // Default 200
- return Task.CompletedTask;
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true);
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- var dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalTheory]
- [InlineData("POST")]
- [InlineData("PUT")]
- [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10, SkipReason = "Http2 requires Win10")]
- public async Task RequestWithoutData_LengthRequired_Rejected(string method)
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
- {
- throw new NotImplementedException();
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- var headers = new[]
- {
- new KeyValuePair<string, string>(HeaderNames.Method, method),
- new KeyValuePair<string, string>(HeaderNames.Path, "/"),
- new KeyValuePair<string, string>(HeaderNames.Scheme, "https"),
- new KeyValuePair<string, string>(HeaderNames.Authority, "localhost:80"),
- };
- await h2Connection.StartStreamAsync(1, headers, endStream: true);
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- Assert.Equal("411", decodedHeaders[HeaderNames.Status]);
- });
- var dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 344);
- dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalTheory]
- [InlineData("GET")]
- [InlineData("HEAD")]
- [InlineData("PATCH")]
- [InlineData("DELETE")]
- [InlineData("CUSTOM")]
- [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10, SkipReason = "Http2 requires Win10")]
- public async Task RequestWithoutData_Success(string method)
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
- {
- Assert.True(HttpMethods.Equals(method, httpContext.Request.Method));
- Assert.False(httpContext.Request.CanHaveBody());
- Assert.Null(httpContext.Request.ContentLength);
- Assert.False(httpContext.Request.Headers.ContainsKey(HeaderNames.TransferEncoding));
- return Task.CompletedTask;
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- var headers = new[]
- {
- new KeyValuePair<string, string>(HeaderNames.Method, method),
- new KeyValuePair<string, string>(HeaderNames.Path, "/"),
- new KeyValuePair<string, string>(HeaderNames.Scheme, "https"),
- new KeyValuePair<string, string>(HeaderNames.Authority, "localhost:80"),
- };
- await h2Connection.StartStreamAsync(1, headers, endStream: true);
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- var dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalTheory]
- [InlineData("GET")]
- // [InlineData("HEAD")] Reset with code HTTP_1_1_REQUIRED
- [InlineData("POST")]
- [InlineData("PUT")]
- [InlineData("PATCH")]
- [InlineData("DELETE")]
- [InlineData("CUSTOM")]
- [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10, SkipReason = "Http2 requires Win10")]
- public async Task RequestWithDataAndContentLength_Success(string method)
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
- {
- Assert.True(HttpMethods.Equals(method, httpContext.Request.Method));
- Assert.True(httpContext.Request.CanHaveBody());
- Assert.Equal(11, httpContext.Request.ContentLength);
- Assert.False(httpContext.Request.Headers.ContainsKey(HeaderNames.TransferEncoding));
- return httpContext.Request.Body.CopyToAsync(httpContext.Response.Body);
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- var headers = new[]
- {
- new KeyValuePair<string, string>(HeaderNames.Method, method),
- new KeyValuePair<string, string>(HeaderNames.Path, "/"),
- new KeyValuePair<string, string>(HeaderNames.Scheme, "https"),
- new KeyValuePair<string, string>(HeaderNames.Authority, "localhost:80"),
- new KeyValuePair<string, string>(HeaderNames.ContentLength, "11"),
- };
- await h2Connection.StartStreamAsync(1, headers, endStream: false);
- await h2Connection.SendDataAsync(1, Encoding.UTF8.GetBytes("Hello World"), endStream: true);
- // Http.Sys no longer sends a window update here on later versions.
- if (Environment.OSVersion.Version < new Version(10, 0, 19041, 0))
- {
- var windowUpdate = await h2Connection.ReceiveFrameAsync();
- Assert.Equal(Http2FrameType.WINDOW_UPDATE, windowUpdate.Type);
- }
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- var dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 11);
- Assert.Equal("Hello World", Encoding.UTF8.GetString(dataFrame.Payload.Span));
- dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalTheory]
- [InlineData("GET")]
- // [InlineData("HEAD")] Reset with code HTTP_1_1_REQUIRED
- [InlineData("POST")]
- [InlineData("PUT")]
- [InlineData("PATCH")]
- [InlineData("DELETE")]
- [InlineData("CUSTOM")]
- [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10, SkipReason = "Http2 requires Win10")]
- public async Task RequestWithDataAndNoContentLength_Success(string method)
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
- {
- Assert.True(HttpMethods.Equals(method, httpContext.Request.Method));
- Assert.True(httpContext.Request.CanHaveBody());
- Assert.Null(httpContext.Request.ContentLength);
- // The client didn't send this header, Http.Sys added it for back compat with HTTP/1.1.
- Assert.Equal("chunked", httpContext.Request.Headers[HeaderNames.TransferEncoding]);
- return httpContext.Request.Body.CopyToAsync(httpContext.Response.Body);
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- var headers = new[]
- {
- new KeyValuePair<string, string>(HeaderNames.Method, method),
- new KeyValuePair<string, string>(HeaderNames.Path, "/"),
- new KeyValuePair<string, string>(HeaderNames.Scheme, "https"),
- new KeyValuePair<string, string>(HeaderNames.Authority, "localhost:80"),
- };
- await h2Connection.StartStreamAsync(1, headers, endStream: false);
- await h2Connection.SendDataAsync(1, Encoding.UTF8.GetBytes("Hello World"), endStream: true);
- // Http.Sys no longer sends a window update here on later versions.
- if (Environment.OSVersion.Version < new Version(10, 0, 19041, 0))
- {
- var windowUpdate = await h2Connection.ReceiveFrameAsync();
- Assert.Equal(Http2FrameType.WINDOW_UPDATE, windowUpdate.Type);
- }
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- var dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 11);
- Assert.Equal("Hello World", Encoding.UTF8.GetString(dataFrame.Payload.Span));
- dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10, SkipReason = "Http2 requires Win10")]
- public async Task ResponseWithData_Success()
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
- {
- return httpContext.Response.WriteAsync("Hello World");
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true);
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- var dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 11);
- dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact(Skip = "https://github.com/dotnet/aspnetcore/issues/17420")]
- [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10, SkipReason = "Http2 requires Win10")]
- [MaximumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10_19H1, SkipReason = "This is last version without GoAway support")]
- public async Task ConnectionClose_NoOSSupport_NoGoAway()
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
- {
- httpContext.Response.Headers[HeaderNames.Connection] = "close";
- return Task.FromResult(0);
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true);
- await h2Connection.ReceiveHeadersAsync(1, endStream: true, decodedHeaders =>
- {
- // HTTP/2 filters out the connection header
- Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- // Send and receive a second request to ensure there is no GoAway frame on the wire yet.
- await h2Connection.StartStreamAsync(3, Http2Utilities.BrowserRequestHeaders, endStream: true);
- await h2Connection.ReceiveHeadersAsync(3, endStream: true, decodedHeaders =>
- {
- // HTTP/2 filters out the connection header
- Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- await h2Connection.StopConnectionAsync(expectedLastStreamId: 1, ignoreNonGoAwayFrames: false);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10_19H2, SkipReason = "GoAway support was added in Win10_19H2.")]
- public async Task ConnectionClose_OSSupport_SendsGoAway()
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
- {
- httpContext.Response.Headers[HeaderNames.Connection] = "close";
- return Task.FromResult(0);
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true);
- var goAwayFrame = await h2Connection.ReceiveFrameAsync();
- h2Connection.VerifyGoAway(goAwayFrame, int.MaxValue, Http2ErrorCode.NO_ERROR);
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- // HTTP/2 filters out the connection header
- Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- var dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);
- // Http.Sys doesn't send a final GoAway unless we ignore the first one and send 200 additional streams.
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10_19H2, SkipReason = "GoAway support was added in Win10_19H2.")]
- public async Task ConnectionClose_AdditionalRequests_ReceivesSecondGoAway()
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
- {
- httpContext.Response.Headers[HeaderNames.Connection] = "close";
- return Task.FromResult(0);
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- var streamId = 1;
- await h2Connection.StartStreamAsync(streamId, Http2Utilities.BrowserRequestHeaders, endStream: true);
- var goAwayFrame = await h2Connection.ReceiveFrameAsync();
- h2Connection.VerifyGoAway(goAwayFrame, int.MaxValue, Http2ErrorCode.NO_ERROR);
- await h2Connection.ReceiveHeadersAsync(streamId, decodedHeaders =>
- {
- // HTTP/2 filters out the connection header
- Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- var dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, streamId, endOfStream: true, length: 0);
- // Http.Sys doesn't send a final GoAway unless we ignore the first one and send 200 additional streams.
- for (var i = 1; i < 200; i++)
- {
- streamId = 1 + (i * 2); // Odds.
- await h2Connection.StartStreamAsync(streamId, Http2Utilities.BrowserRequestHeaders, endStream: true);
- await h2Connection.ReceiveHeadersAsync(streamId, decodedHeaders =>
- {
- // HTTP/2 filters out the connection header
- Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, streamId, endOfStream: true, length: 0);
- }
- streamId = 1 + (200 * 2); // Odds.
- await h2Connection.StartStreamAsync(streamId, Http2Utilities.BrowserRequestHeaders, endStream: true);
- // Final GoAway
- goAwayFrame = await h2Connection.ReceiveFrameAsync();
- h2Connection.VerifyGoAway(goAwayFrame, streamId, Http2ErrorCode.NO_ERROR);
- // Normal response
- await h2Connection.ReceiveHeadersAsync(streamId, decodedHeaders =>
- {
- // HTTP/2 filters out the connection header
- Assert.False(decodedHeaders.ContainsKey(HeaderNames.Connection));
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, streamId, endOfStream: true, length: 0);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10, SkipReason = "Http2 requires Win10")]
- public async Task AppException_BeforeResponseHeaders_500()
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
- {
- throw new Exception("Application exception");
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true);
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- Assert.Equal("500", decodedHeaders[HeaderNames.Status]);
- });
- var dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10, SkipReason = "Http2 requires Win10")]
- [MaximumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10_20H1, SkipReason = "This is last version without custom Reset support")]
- public async Task AppException_AfterHeaders_PriorOSVersions_ResetCancel()
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, async httpContext =>
- {
- await httpContext.Response.Body.FlushAsync();
- throw new Exception("Application exception");
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true);
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- var resetFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyResetFrame(resetFrame, expectedStreamId: 1, Http2ErrorCode.CANCEL);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, "10.0.19529", SkipReason = "Custom Reset support was added in Win10_20H2.")]
- public async Task AppException_AfterHeaders_ResetInternalError()
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, async httpContext =>
- {
- await httpContext.Response.Body.FlushAsync();
- throw new Exception("Application exception");
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true);
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- var frame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyResetFrame(frame, expectedStreamId: 1, Http2ErrorCode.INTERNAL_ERROR);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact]
- public async Task Reset_Http1_NotSupported()
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
- {
- Assert.Equal("HTTP/1.1", httpContext.Request.Protocol);
- var feature = httpContext.Features.Get<IHttpResetFeature>();
- Assert.Null(feature);
- return httpContext.Response.WriteAsync("Hello World");
- });
- var handler = new HttpClientHandler();
- handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
- using HttpClient client = new HttpClient(handler);
- client.DefaultRequestVersion = HttpVersion.Version11;
- var response = await client.GetStringAsync(address);
- Assert.Equal("Hello World", response);
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10, SkipReason = "Http2 requires Win10")]
- [MaximumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10_20H1, SkipReason = "This is last version without Reset support")]
- public async Task Reset_PriorOSVersions_NotSupported()
- {
- using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
- {
- Assert.Equal("HTTP/2", httpContext.Request.Protocol);
- var feature = httpContext.Features.Get<IHttpResetFeature>();
- Assert.Null(feature);
- return httpContext.Response.WriteAsync("Hello World");
- });
- var handler = new HttpClientHandler();
- handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
- using HttpClient client = new HttpClient(handler);
- client.DefaultRequestVersion = HttpVersion.Version20;
- var response = await client.GetStringAsync(address);
- Assert.Equal("Hello World", response);
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, "10.0.19529", SkipReason = "Reset support was added in Win10_20H2.")]
- public async Task Reset_BeforeResponse_Resets()
- {
- var appResult = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
- using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
- {
- try
- {
- Assert.Equal("HTTP/2", httpContext.Request.Protocol);
- var feature = httpContext.Features.Get<IHttpResetFeature>();
- Assert.NotNull(feature);
- feature.Reset(1111); // Custom
- appResult.SetResult(0);
- }
- catch (Exception ex)
- {
- appResult.SetException(ex);
- }
- return Task.FromResult(0);
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true);
- var resetFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyResetFrame(resetFrame, expectedStreamId: 1, expectedErrorCode: (Http2ErrorCode)1111);
- // Any app errors?
- Assert.Equal(0, await appResult.Task.DefaultTimeout());
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, "10.0.19529", SkipReason = "Reset support was added in Win10_20H2.")]
- public async Task Reset_AfterResponseHeaders_Resets()
- {
- var appResult = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
- using var server = Utilities.CreateDynamicHttpsServer(out var address, async httpContext =>
- {
- try
- {
- Assert.Equal("HTTP/2", httpContext.Request.Protocol);
- var feature = httpContext.Features.Get<IHttpResetFeature>();
- Assert.NotNull(feature);
- await httpContext.Response.Body.FlushAsync();
- feature.Reset(1111); // Custom
- appResult.SetResult(0);
- }
- catch (Exception ex)
- {
- appResult.SetException(ex);
- }
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true);
- // Any app errors?
- Assert.Equal(0, await appResult.Task.DefaultTimeout());
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- var resetFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyResetFrame(resetFrame, expectedStreamId: 1, expectedErrorCode: (Http2ErrorCode)1111);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, "10.0.19529", SkipReason = "Reset support was added in Win10_20H2.")]
- public async Task Reset_DurringResponseBody_Resets()
- {
- var appResult = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
- using var server = Utilities.CreateDynamicHttpsServer(out var address, async httpContext =>
- {
- try
- {
- Assert.Equal("HTTP/2", httpContext.Request.Protocol);
- var feature = httpContext.Features.Get<IHttpResetFeature>();
- Assert.NotNull(feature);
- await httpContext.Response.WriteAsync("Hello World");
- feature.Reset(1111); // Custom
- appResult.SetResult(0);
- }
- catch (Exception ex)
- {
- appResult.SetException(ex);
- }
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true);
- // Any app errors?
- Assert.Equal(0, await appResult.Task.DefaultTimeout());
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- var dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 11);
- var resetFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyResetFrame(resetFrame, expectedStreamId: 1, expectedErrorCode: (Http2ErrorCode)1111);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, "10.0.19529", SkipReason = "Reset support was added in Win10_20H2.")]
- public async Task Reset_AfterCompleteAsync_NoReset()
- {
- var appResult = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
- using var server = Utilities.CreateDynamicHttpsServer(out var address, async httpContext =>
- {
- try
- {
- Assert.Equal("HTTP/2", httpContext.Request.Protocol);
- var feature = httpContext.Features.Get<IHttpResetFeature>();
- Assert.NotNull(feature);
- await httpContext.Response.WriteAsync("Hello World");
- await httpContext.Response.CompleteAsync();
- // The request and response are fully complete, the reset doesn't get sent.
- feature.Reset(1111);
- appResult.SetResult(0);
- }
- catch (Exception ex)
- {
- appResult.SetException(ex);
- }
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.BrowserRequestHeaders, endStream: true);
- // Any app errors?
- Assert.Equal(0, await appResult.Task.DefaultTimeout());
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- var dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: false, length: 11);
- dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, "10.0.19529", SkipReason = "Reset support was added in Win10_20H2.")]
- public async Task Reset_BeforeRequestBody_Resets()
- {
- var appResult = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
- using var server = Utilities.CreateDynamicHttpsServer(out var address, async httpContext =>
- {
- try
- {
- Assert.Equal("HTTP/2", httpContext.Request.Protocol);
- var feature = httpContext.Features.Get<IHttpResetFeature>();
- Assert.NotNull(feature);
- var readTask = httpContext.Request.Body.ReadAsync(new byte[10], 0, 10);
- feature.Reset(1111);
- await Assert.ThrowsAsync<IOException>(() => readTask);
- appResult.SetResult(0);
- }
- catch (Exception ex)
- {
- appResult.SetException(ex);
- }
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.PostRequestHeaders, endStream: false);
- // Any app errors?
- Assert.Equal(0, await appResult.Task.DefaultTimeout());
- var resetFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyResetFrame(resetFrame, expectedStreamId: 1, expectedErrorCode: (Http2ErrorCode)1111);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, "10.0.19529", SkipReason = "Reset support was added in Win10_20H2.")]
- public async Task Reset_DurringRequestBody_Resets()
- {
- var appResult = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
- using var server = Utilities.CreateDynamicHttpsServer(out var address, async httpContext =>
- {
- try
- {
- Assert.Equal("HTTP/2", httpContext.Request.Protocol);
- var feature = httpContext.Features.Get<IHttpResetFeature>();
- Assert.NotNull(feature);
- var read = await httpContext.Request.Body.ReadAsync(new byte[10], 0, 10);
- Assert.Equal(10, read);
- var readTask = httpContext.Request.Body.ReadAsync(new byte[10], 0, 10);
- feature.Reset(1111);
- await Assert.ThrowsAsync<IOException>(() => readTask);
- appResult.SetResult(0);
- }
- catch (Exception ex)
- {
- appResult.SetException(ex);
- }
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.PostRequestHeaders, endStream: false);
- await h2Connection.SendDataAsync(1, new byte[10], endStream: false);
- // Any app errors?
- Assert.Equal(0, await appResult.Task.DefaultTimeout());
- var resetFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyResetFrame(resetFrame, expectedStreamId: 1, expectedErrorCode: (Http2ErrorCode)1111);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- [ConditionalFact]
- [MinimumOSVersion(OperatingSystems.Windows, "10.0.19529", SkipReason = "Reset support was added in Win10_20H2.")]
- public async Task Reset_CompleteAsyncDurringRequestBody_Resets()
- {
- var appResult = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
- using var server = Utilities.CreateDynamicHttpsServer(out var address, async httpContext =>
- {
- try
- {
- Assert.Equal("HTTP/2", httpContext.Request.Protocol);
- var feature = httpContext.Features.Get<IHttpResetFeature>();
- Assert.NotNull(feature);
- var read = await httpContext.Request.Body.ReadAsync(new byte[10], 0, 10);
- Assert.Equal(10, read);
- var readTask = httpContext.Request.Body.ReadAsync(new byte[10], 0, 10);
- await httpContext.Response.CompleteAsync();
- feature.Reset((int)Http2ErrorCode.NO_ERROR); // GRPC does this
- await Assert.ThrowsAsync<IOException>(() => readTask);
- appResult.SetResult(0);
- }
- catch (Exception ex)
- {
- appResult.SetException(ex);
- }
- });
- await new HostBuilder()
- .UseHttp2Cat(address, async h2Connection =>
- {
- await h2Connection.InitializeConnectionAsync();
- h2Connection.Logger.LogInformation("Initialized http2 connection. Starting stream 1.");
- await h2Connection.StartStreamAsync(1, Http2Utilities.PostRequestHeaders, endStream: false);
- await h2Connection.SendDataAsync(1, new byte[10], endStream: false);
- // Any app errors?
- Assert.Equal(0, await appResult.Task.DefaultTimeout());
- await h2Connection.ReceiveHeadersAsync(1, decodedHeaders =>
- {
- Assert.Equal("200", decodedHeaders[HeaderNames.Status]);
- });
- var dataFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyDataFrame(dataFrame, 1, endOfStream: true, length: 0);
- var resetFrame = await h2Connection.ReceiveFrameAsync();
- Http2Utilities.VerifyResetFrame(resetFrame, expectedStreamId: 1, expectedErrorCode: Http2ErrorCode.NO_ERROR);
- h2Connection.Logger.LogInformation("Connection stopped.");
- })
- .Build().RunAsync();
- }
- }
- }
|