-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
HttpClientHandlerTest.Http3.cs
457 lines (386 loc) · 18.7 KB
/
HttpClientHandlerTest.Http3.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Quic;
using System.Net.Security;
using System.Net.Test.Common;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.Http.Functional.Tests
{
public abstract class HttpClientHandlerTest_Http3 : HttpClientHandlerTestBase
{
protected override Version UseVersion => HttpVersion.Version30;
public HttpClientHandlerTest_Http3(ITestOutputHelper output) : base(output)
{
}
[Theory]
[InlineData(10)] // 2 bytes settings value.
[InlineData(100)] // 4 bytes settings value.
[InlineData(10_000_000)] // 8 bytes settings value.
public async Task ClientSettingsReceived_Success(int headerSizeLimit)
{
using Http3LoopbackServer server = CreateHttp3LoopbackServer();
Task serverTask = Task.Run(async () =>
{
using Http3LoopbackConnection connection = (Http3LoopbackConnection)await server.EstablishGenericConnectionAsync();
(Http3LoopbackStream settingsStream, Http3LoopbackStream requestStream) = await connection.AcceptControlAndRequestStreamAsync();
using (settingsStream)
using (requestStream)
{
Assert.False(settingsStream.CanWrite, "Expected unidirectional control stream.");
long? streamType = await settingsStream.ReadIntegerAsync();
Assert.Equal(Http3LoopbackStream.ControlStream, streamType);
List<(long settingId, long settingValue)> settings = await settingsStream.ReadSettingsAsync();
(long settingId, long settingValue) = Assert.Single(settings);
Assert.Equal(Http3LoopbackStream.MaxHeaderListSize, settingId);
Assert.Equal(headerSizeLimit * 1024L, settingValue);
await requestStream.ReadRequestDataAsync();
await requestStream.SendResponseAsync();
}
});
Task clientTask = Task.Run(async () =>
{
using HttpClientHandler handler = CreateHttpClientHandler();
handler.MaxResponseHeadersLength = headerSizeLimit;
using HttpClient client = CreateHttpClient(handler);
using HttpRequestMessage request = new()
{
Method = HttpMethod.Get,
RequestUri = server.Address,
Version = HttpVersion30,
VersionPolicy = HttpVersionPolicy.RequestVersionExact
};
using HttpResponseMessage response = await client.SendAsync(request);
});
await new[] { clientTask, serverTask }.WhenAllOrAnyFailed(20_000);
}
[Theory]
[InlineData(10)]
[InlineData(100)]
[InlineData(1000)]
public async Task SendMoreThanStreamLimitRequests_Succeeds(int streamLimit)
{
using Http3LoopbackServer server = CreateHttp3LoopbackServer(new Http3Options(){ MaxBidirectionalStreams = streamLimit });
Task serverTask = Task.Run(async () =>
{
using Http3LoopbackConnection connection = (Http3LoopbackConnection)await server.EstablishGenericConnectionAsync();
for (int i = 0; i < streamLimit + 1; ++i)
{
using Http3LoopbackStream stream = await connection.AcceptRequestStreamAsync();
await stream.HandleRequestAsync();
}
});
Task clientTask = Task.Run(async () =>
{
using HttpClient client = CreateHttpClient();
for (int i = 0; i < streamLimit + 1; ++i)
{
HttpRequestMessage request = new()
{
Method = HttpMethod.Get,
RequestUri = server.Address,
Version = HttpVersion30,
VersionPolicy = HttpVersionPolicy.RequestVersionExact
};
using var response = await client.SendAsync(request).WaitAsync(TimeSpan.FromSeconds(10));
}
});
await new[] { clientTask, serverTask }.WhenAllOrAnyFailed(20_000);
}
[Theory]
[InlineData(10)]
[InlineData(100)]
[InlineData(1000)]
public async Task SendStreamLimitRequestsConcurrently_Succeeds(int streamLimit)
{
using Http3LoopbackServer server = CreateHttp3LoopbackServer(new Http3Options(){ MaxBidirectionalStreams = streamLimit });
Task serverTask = Task.Run(async () =>
{
using Http3LoopbackConnection connection = (Http3LoopbackConnection)await server.EstablishGenericConnectionAsync();
for (int i = 0; i < streamLimit; ++i)
{
using Http3LoopbackStream stream = await connection.AcceptRequestStreamAsync();
await stream.HandleRequestAsync();
}
});
Task clientTask = Task.Run(async () =>
{
using HttpClient client = CreateHttpClient();
var tasks = new Task<HttpResponseMessage>[streamLimit];
Parallel.For(0, streamLimit, i =>
{
HttpRequestMessage request = new()
{
Method = HttpMethod.Get,
RequestUri = server.Address,
Version = HttpVersion30,
VersionPolicy = HttpVersionPolicy.RequestVersionExact
};
tasks[i] = client.SendAsync(request);
});
var responses = await Task.WhenAll(tasks);
foreach (var response in responses)
{
response.Dispose();
}
});
await new[] { clientTask, serverTask }.WhenAllOrAnyFailed(20_000);
}
[Theory]
[InlineData(10)]
[InlineData(100)]
[InlineData(1000)]
public async Task SendMoreThanStreamLimitRequestsConcurrently_LastWaits(int streamLimit)
{
// This combination leads to a hang manifesting in CI only. Disabling it until there's more time to investigate.
// [ActiveIssue("https://github.com/dotnet/runtime/issues/53688")]
if (this.UseQuicImplementationProvider == QuicImplementationProviders.Mock)
{
return;
}
using Http3LoopbackServer server = CreateHttp3LoopbackServer(new Http3Options(){ MaxBidirectionalStreams = streamLimit });
var lastRequestContentStarted = new TaskCompletionSource();
Task serverTask = Task.Run(async () =>
{
// Read the first streamLimit requests, keep the streams open to make the last one wait.
using Http3LoopbackConnection connection = (Http3LoopbackConnection)await server.EstablishGenericConnectionAsync();
var streams = new Http3LoopbackStream[streamLimit];
for (int i = 0; i < streamLimit; ++i)
{
Http3LoopbackStream stream = await connection.AcceptRequestStreamAsync();
var body = await stream.ReadRequestDataAsync();
streams[i] = stream;
}
// Make the last request running independently.
var lastRequest = Task.Run(async () => {
using Http3LoopbackStream stream = await connection.AcceptRequestStreamAsync();
await stream.HandleRequestAsync();
});
// All the initial streamLimit streams are still opened so the last request cannot started yet.
Assert.False(lastRequestContentStarted.Task.IsCompleted);
// Reply to the first streamLimit requests.
for (int i = 0; i < streamLimit; ++i)
{
await streams[i].SendResponseAsync();
streams[i].Dispose();
// After the first request is fully processed, the last request should unblock and get processed.
if (i == 0)
{
await lastRequestContentStarted.Task;
}
}
await lastRequest;
});
Task clientTask = Task.Run(async () =>
{
using HttpClient client = CreateHttpClient();
// Fire out the first streamLimit requests in parallel, no waiting for the responses yet.
var countdown = new CountdownEvent(streamLimit);
var tasks = new Task<HttpResponseMessage>[streamLimit];
Parallel.For(0, streamLimit, i =>
{
HttpRequestMessage request = new()
{
Method = HttpMethod.Post,
RequestUri = server.Address,
Version = HttpVersion30,
VersionPolicy = HttpVersionPolicy.RequestVersionExact,
Content = new StreamContent(new DelegateStream(
canReadFunc: () => true,
readFunc: (buffer, offset, count) =>
{
countdown.Signal();
return 0;
}))
};
tasks[i] = client.SendAsync(request);
});
// Wait for the first streamLimit request to get started.
countdown.Wait();
// Fire out the last request, that should wait until the server fully handles at least one request.
HttpRequestMessage last = new()
{
Method = HttpMethod.Post,
RequestUri = server.Address,
Version = HttpVersion30,
VersionPolicy = HttpVersionPolicy.RequestVersionExact,
Content = new StreamContent(new DelegateStream(
canReadFunc: () => true,
readFunc: (buffer, offset, count) =>
{
lastRequestContentStarted.SetResult();
return 0;
}))
};
var lastTask = client.SendAsync(last);
// Wait for all requests to finish. Whether the last request was pending is checked on the server side.
var responses = await Task.WhenAll(tasks);
foreach (var response in responses)
{
response.Dispose();
}
await lastTask;
});
await new[] { clientTask, serverTask }.WhenAllOrAnyFailed(20_000);
}
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/53090")]
public async Task ReservedFrameType_Throws()
{
const int ReservedHttp2PriorityFrameId = 0x2;
const long UnexpectedFrameErrorCode = 0x105;
using Http3LoopbackServer server = CreateHttp3LoopbackServer();
Task serverTask = Task.Run(async () =>
{
using Http3LoopbackConnection connection = (Http3LoopbackConnection)await server.EstablishGenericConnectionAsync();
using Http3LoopbackStream stream = await connection.AcceptRequestStreamAsync();
await stream.SendFrameAsync(ReservedHttp2PriorityFrameId, new byte[8]);
QuicConnectionAbortedException ex = await Assert.ThrowsAsync<QuicConnectionAbortedException>(async () =>
{
await stream.HandleRequestAsync();
using Http3LoopbackStream stream2 = await connection.AcceptRequestStreamAsync();
});
Assert.Equal(UnexpectedFrameErrorCode, ex.ErrorCode);
});
Task clientTask = Task.Run(async () =>
{
using HttpClient client = CreateHttpClient();
using HttpRequestMessage request = new()
{
Method = HttpMethod.Get,
RequestUri = server.Address,
Version = HttpVersion30,
VersionPolicy = HttpVersionPolicy.RequestVersionExact
};
await Assert.ThrowsAsync<HttpRequestException>(async () => await client.SendAsync(request));
});
await new[] { clientTask, serverTask }.WhenAllOrAnyFailed(20_000);
}
[Fact]
public async Task ServerCertificateCustomValidationCallback_Succeeds()
{
// Mock doesn't make use of cart validation callback.
if (UseQuicImplementationProvider == QuicImplementationProviders.Mock)
{
return;
}
HttpRequestMessage? callbackRequest = null;
int invocationCount = 0;
var httpClientHandler = CreateHttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback = (request, _, _, _) =>
{
callbackRequest = request;
++invocationCount;
return true;
};
using Http3LoopbackServer server = CreateHttp3LoopbackServer();
using HttpClient client = CreateHttpClient(httpClientHandler);
Task serverTask = Task.Run(async () =>
{
using Http3LoopbackConnection connection = (Http3LoopbackConnection)await server.EstablishGenericConnectionAsync();
using Http3LoopbackStream stream = await connection.AcceptRequestStreamAsync();
await stream.HandleRequestAsync();
using Http3LoopbackStream stream2 = await connection.AcceptRequestStreamAsync();
await stream2.HandleRequestAsync();
});
var request = new HttpRequestMessage(HttpMethod.Get, server.Address);
request.Version = HttpVersion.Version30;
request.VersionPolicy = HttpVersionPolicy.RequestVersionExact;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Assert.Equal(HttpVersion.Version30, response.Version);
Assert.Same(request, callbackRequest);
Assert.Equal(1, invocationCount);
// Second request, the callback shouldn't be hit at all.
callbackRequest = null;
request = new HttpRequestMessage(HttpMethod.Get, server.Address);
request.Version = HttpVersion.Version30;
request.VersionPolicy = HttpVersionPolicy.RequestVersionExact;
response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Assert.Equal(HttpVersion.Version30, response.Version);
Assert.Null(callbackRequest);
Assert.Equal(1, invocationCount);
}
[OuterLoop]
[ConditionalTheory(nameof(IsMsQuicSupported))]
[MemberData(nameof(InteropUris))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/54726")]
public async Task Public_Interop_ExactVersion_Success(string uri)
{
if (UseQuicImplementationProvider == QuicImplementationProviders.Mock)
{
return;
}
using HttpClient client = CreateHttpClient();
using HttpRequestMessage request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(uri, UriKind.Absolute),
Version = HttpVersion.Version30,
VersionPolicy = HttpVersionPolicy.RequestVersionExact
};
using HttpResponseMessage response = await client.SendAsync(request).WaitAsync(TimeSpan.FromSeconds(20));
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(3, response.Version.Major);
}
[OuterLoop]
[ConditionalTheory(nameof(IsMsQuicSupported))]
[MemberData(nameof(InteropUris))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/54726")]
public async Task Public_Interop_Upgrade_Success(string uri)
{
if (UseQuicImplementationProvider == QuicImplementationProviders.Mock)
{
return;
}
using HttpClient client = CreateHttpClient();
// First request uses HTTP/1 or HTTP/2 and receives an Alt-Svc either by header or (with HTTP/2) by frame.
using (HttpRequestMessage requestA = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(uri, UriKind.Absolute),
Version = HttpVersion.Version30,
VersionPolicy = HttpVersionPolicy.RequestVersionOrLower
})
{
using HttpResponseMessage responseA = await client.SendAsync(requestA).WaitAsync(TimeSpan.FromSeconds(20));
Assert.Equal(HttpStatusCode.OK, responseA.StatusCode);
Assert.NotEqual(3, responseA.Version.Major);
}
// Second request uses HTTP/3.
using (HttpRequestMessage requestB = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(uri, UriKind.Absolute),
Version = HttpVersion.Version30,
VersionPolicy = HttpVersionPolicy.RequestVersionOrLower
})
{
using HttpResponseMessage responseB = await client.SendAsync(requestB).WaitAsync(TimeSpan.FromSeconds(20));
Assert.Equal(HttpStatusCode.OK, responseB.StatusCode);
Assert.NotEqual(3, responseB.Version.Major);
}
}
/// <summary>
/// These are public interop test servers for various QUIC and HTTP/3 implementations,
/// taken from https://github.com/quicwg/base-drafts/wiki/Implementations
/// </summary>
public static TheoryData<string> InteropUris() =>
new TheoryData<string>
{
{ "https://quic.rocks:4433/" }, // Chromium
{ "https://http3-test.litespeedtech.com:4433/" }, // LiteSpeed
{ "https://quic.tech:8443/" } // Cloudflare
};
}
}