|
| 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.Net.Test.Common; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Xunit; |
| 7 | +using Xunit.Abstractions; |
| 8 | + |
| 9 | +namespace System.Net.Http.Functional.Tests |
| 10 | +{ |
| 11 | + [ConditionalClass(typeof(SocketsHttpHandler), nameof(SocketsHttpHandler.IsSupported))] |
| 12 | + public sealed class SocketsHttpHandler_Http1KeepAlive_Test : HttpClientHandlerTestBase |
| 13 | + { |
| 14 | + public SocketsHttpHandler_Http1KeepAlive_Test(ITestOutputHelper output) : base(output) { } |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public async Task Http10Response_ConnectionIsReusedFor10And11() |
| 18 | + { |
| 19 | + await LoopbackServer.CreateClientAndServerAsync(async uri => |
| 20 | + { |
| 21 | + using HttpClient client = CreateHttpClient(); |
| 22 | + |
| 23 | + await client.SendAsync(CreateRequest(HttpMethod.Get, uri, HttpVersion.Version10, exactVersion: true)); |
| 24 | + await client.SendAsync(CreateRequest(HttpMethod.Get, uri, HttpVersion.Version11, exactVersion: true)); |
| 25 | + await client.SendAsync(CreateRequest(HttpMethod.Get, uri, HttpVersion.Version10, exactVersion: true)); |
| 26 | + }, |
| 27 | + server => server.AcceptConnectionAsync(async connection => |
| 28 | + { |
| 29 | + HttpRequestData request = await connection.ReadRequestDataAsync(); |
| 30 | + Assert.Equal(0, request.Version.Minor); |
| 31 | + await connection.WriteStringAsync("HTTP/1.0 200 OK\r\nContent-Length: 1\r\n\r\n1"); |
| 32 | + connection.CompleteRequestProcessing(); |
| 33 | + |
| 34 | + request = await connection.ReadRequestDataAsync(); |
| 35 | + Assert.Equal(1, request.Version.Minor); |
| 36 | + await connection.WriteStringAsync("HTTP/1.0 200 OK\r\nContent-Length: 1\r\n\r\n2"); |
| 37 | + connection.CompleteRequestProcessing(); |
| 38 | + |
| 39 | + request = await connection.ReadRequestDataAsync(); |
| 40 | + Assert.Equal(0, request.Version.Minor); |
| 41 | + await connection.WriteStringAsync("HTTP/1.0 200 OK\r\nContent-Length: 1\r\n\r\n3"); |
| 42 | + })); |
| 43 | + } |
| 44 | + |
| 45 | + [OuterLoop("Uses Task.Delay")] |
| 46 | + [Fact] |
| 47 | + public async Task Http10ResponseWithKeepAliveTimeout_ConnectionRecycledAfterTimeout() |
| 48 | + { |
| 49 | + await LoopbackServer.CreateClientAndServerAsync(async uri => |
| 50 | + { |
| 51 | + using HttpClient client = CreateHttpClient(); |
| 52 | + |
| 53 | + await client.GetAsync(uri); |
| 54 | + |
| 55 | + await Task.Delay(2000); |
| 56 | + await client.GetAsync(uri); |
| 57 | + }, |
| 58 | + async server => |
| 59 | + { |
| 60 | + await server.AcceptConnectionAsync(async connection => |
| 61 | + { |
| 62 | + await connection.ReadRequestDataAsync(); |
| 63 | + await connection.WriteStringAsync("HTTP/1.0 200 OK\r\nKeep-Alive: timeout=1\r\nContent-Length: 1\r\n\r\n1"); |
| 64 | + connection.CompleteRequestProcessing(); |
| 65 | + |
| 66 | + await Assert.ThrowsAnyAsync<Exception>(() => connection.ReadRequestDataAsync()); |
| 67 | + }); |
| 68 | + |
| 69 | + await server.AcceptConnectionSendResponseAndCloseAsync(); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + [Theory] |
| 74 | + [InlineData("timeout=1000", true)] |
| 75 | + [InlineData("timeout=30", true)] |
| 76 | + [InlineData("timeout=0", false)] |
| 77 | + [InlineData("foo, bar=baz, timeout=30", true)] |
| 78 | + [InlineData("foo, bar=baz, timeout=0", false)] |
| 79 | + [InlineData("timeout=-1", true)] |
| 80 | + [InlineData("timeout=abc", true)] |
| 81 | + [InlineData("max=1", true)] |
| 82 | + [InlineData("max=0", false)] |
| 83 | + [InlineData("max=-1", true)] |
| 84 | + [InlineData("max=abc", true)] |
| 85 | + [InlineData("timeout=30, max=1", true)] |
| 86 | + [InlineData("timeout=30, max=0", false)] |
| 87 | + [InlineData("timeout=0, max=1", false)] |
| 88 | + [InlineData("timeout=0, max=0", false)] |
| 89 | + public async Task Http10ResponseWithKeepAlive_ConnectionNotReusedForShortTimeoutOrMax0(string keepAlive, bool shouldReuseConnection) |
| 90 | + { |
| 91 | + await LoopbackServer.CreateClientAndServerAsync(async uri => |
| 92 | + { |
| 93 | + using HttpClient client = CreateHttpClient(); |
| 94 | + |
| 95 | + await client.GetAsync(uri); |
| 96 | + await client.GetAsync(uri); |
| 97 | + }, |
| 98 | + async server => |
| 99 | + { |
| 100 | + await server.AcceptConnectionAsync(async connection => |
| 101 | + { |
| 102 | + await connection.ReadRequestDataAsync(); |
| 103 | + await connection.WriteStringAsync($"HTTP/1.0 200 OK\r\nKeep-Alive: {keepAlive}\r\nContent-Length: 1\r\n\r\n1"); |
| 104 | + connection.CompleteRequestProcessing(); |
| 105 | + |
| 106 | + if (shouldReuseConnection) |
| 107 | + { |
| 108 | + await connection.HandleRequestAsync(); |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + await Assert.ThrowsAnyAsync<Exception>(() => connection.ReadRequestDataAsync()); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + if (!shouldReuseConnection) |
| 117 | + { |
| 118 | + await server.AcceptConnectionSendResponseAndCloseAsync(); |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + [Theory] |
| 124 | + [InlineData("timeout=1")] |
| 125 | + [InlineData("timeout=0")] |
| 126 | + [InlineData("max=1")] |
| 127 | + [InlineData("max=0")] |
| 128 | + public async Task Http11ResponseWithKeepAlive_KeepAliveIsIgnored(string keepAlive) |
| 129 | + { |
| 130 | + await LoopbackServer.CreateClientAndServerAsync(async uri => |
| 131 | + { |
| 132 | + using HttpClient client = CreateHttpClient(); |
| 133 | + |
| 134 | + await client.GetAsync(uri); |
| 135 | + await client.GetAsync(uri); |
| 136 | + }, |
| 137 | + async server => |
| 138 | + { |
| 139 | + await server.AcceptConnectionAsync(async connection => |
| 140 | + { |
| 141 | + await connection.ReadRequestDataAsync(); |
| 142 | + await connection.WriteStringAsync($"HTTP/1.1 200 OK\r\nKeep-Alive: {keepAlive}\r\nContent-Length: 1\r\n\r\n1"); |
| 143 | + connection.CompleteRequestProcessing(); |
| 144 | + |
| 145 | + await connection.HandleRequestAsync(); |
| 146 | + }); |
| 147 | + }); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments