forked from dotnet/yarp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpect100ContinueTests.cs
More file actions
337 lines (306 loc) · 13.8 KB
/
Expect100ContinueTests.cs
File metadata and controls
337 lines (306 loc) · 13.8 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using Xunit;
using Yarp.ReverseProxy.Common;
using Yarp.ReverseProxy.Forwarder;
namespace Yarp.ReverseProxy;
public class Expect100ContinueTests
{
// HTTP/2 over TLS is not supported on macOS due to missing ALPN support.
// See https://github.com/dotnet/runtime/issues/27727
public static bool Http2OverTlsSupported => !RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
[Theory(Skip = "Condition not met", SkipUnless = nameof(Http2OverTlsSupported))]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http1, true, 200)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http1, false, 200)]
[InlineData(HttpProtocols.Http2, HttpProtocols.Http2, true, 200)]
[InlineData(HttpProtocols.Http2, HttpProtocols.Http2, false, 200)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http1, true, 400)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http1, false, 400)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http2, true, 400)]
[InlineData(HttpProtocols.Http2, HttpProtocols.Http2, true, 400)]
public async Task PostExpect100_BodyNotUploadedIfFailed(HttpProtocols proxyProtocol, HttpProtocols destProtocol, bool useContentLength, int destResponseCode)
{
var headerTcs = new TaskCompletionSource<StringValues>(TaskCreationOptions.RunContinuationsAsynchronously);
var bodyTcs = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
var contentString = new string('a', 1024 * 1024 * 10);
var test = new TestEnvironment(
async context =>
{
if ((context.Request.Protocol == "HTTP/1.1" && destProtocol != HttpProtocols.Http1)
|| (context.Request.Protocol == "HTTP/2.0" && destProtocol != HttpProtocols.Http2))
{
headerTcs.SetException(new Exception($"Unexpected request protocol {context.Request.Protocol}"));
return;
}
else if (context.Request.Headers.TryGetValue(HeaderNames.Expect, out var expectHeader))
{
headerTcs.SetResult(expectHeader);
}
else
{
headerTcs.SetException(new Exception("Missing 'Expect' header in request"));
return;
}
if (destResponseCode == 200)
{
// 100 response code is sent automatically on reading Body.
await ReadContent(context, bodyTcs, Encoding.UTF8.GetByteCount(contentString));
}
context.Response.StatusCode = destResponseCode;
})
{
ProxyProtocol = proxyProtocol,
UseHttpsOnDestination = true,
UseHttpsOnProxy = true,
ConfigTransformer = (c, r) =>
{
c = c with
{
HttpRequest = new ForwarderRequestConfig
{
Version = destProtocol == HttpProtocols.Http2 ? HttpVersion.Version20 : HttpVersion.Version11,
}
};
return (c, r);
},
ConfigureProxy = proxyBuilder =>
{
proxyBuilder.Services.AddSingleton<IForwarderHttpClientFactory, TestForwarderHttpClientFactory>();
},
};
await test.Invoke(async uri =>
{
await ProcessHttpRequest(new Uri(uri), proxyProtocol, contentString, useContentLength, destResponseCode, false, destResponseCode == 200);
Assert.True(headerTcs.Task.IsCompleted);
var expectHeader = await headerTcs.Task;
var expectValue = Assert.Single(expectHeader);
Assert.Equal("100-continue", expectValue);
if (destResponseCode == 200)
{
Assert.True(bodyTcs.Task.IsCompleted);
var actualString = await bodyTcs.Task;
Assert.Equal(contentString, actualString);
}
else
{
Assert.False(bodyTcs.Task.IsCompleted);
}
});
}
[Theory(Skip = "Condition not met", SkipUnless = nameof(Http2OverTlsSupported))]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http1, true, true, 200)]
[InlineData(HttpProtocols.Http2, HttpProtocols.Http2, true, true, 200)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http2, true, true, 200)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http1, false, false, 400)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http1, false, true, 400)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http1, true, false, 400)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http1, true, true, 400)]
[InlineData(HttpProtocols.Http2, HttpProtocols.Http2, false, false, 400)]
[InlineData(HttpProtocols.Http2, HttpProtocols.Http2, false, true, 400)]
[InlineData(HttpProtocols.Http2, HttpProtocols.Http2, true, false, 400)]
[InlineData(HttpProtocols.Http2, HttpProtocols.Http2, true, true, 400)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http2, false, false, 400)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http2, false, true, 400)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http2, true, false, 400)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http2, true, true, 400)]
public async Task PostExpect100_ResponseWithPayload(HttpProtocols proxyProtocol, HttpProtocols destProtocol, bool useContentLength, bool cancelResponse, int responseCode)
{
var requestBodyTcs = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
var contentString = new string('a', 1024 * 1024 * 10);
var test = new TestEnvironment(
async context =>
{
await ReadContent(context, requestBodyTcs, Encoding.UTF8.GetByteCount(contentString));
context.Response.StatusCode = responseCode;
var responseBody = Encoding.UTF8.GetBytes(contentString + "Response");
if (useContentLength)
{
context.Response.Headers.ContentLength = responseBody.Length;
}
if (cancelResponse)
{
await context.Response.BodyWriter.WriteAsync(responseBody.AsMemory(0, responseBody.Length / 2));
context.Abort();
}
else
{
await context.Response.Body.WriteAsync(responseBody.AsMemory());
}
})
{
ProxyProtocol = proxyProtocol,
UseHttpsOnDestination = true,
UseHttpsOnProxy = true,
ConfigTransformer = (c, r) =>
{
c = c with
{
HttpRequest = new ForwarderRequestConfig
{
Version = destProtocol == HttpProtocols.Http2 ? HttpVersion.Version20 : HttpVersion.Version11,
}
};
return (c, r);
},
ConfigureProxy = proxyBuilder =>
{
proxyBuilder.Services.AddSingleton<IForwarderHttpClientFactory, TestForwarderHttpClientFactory>();
},
};
await test.Invoke(async uri =>
{
await ProcessHttpRequest(new Uri(uri), proxyProtocol, contentString, useContentLength, responseCode, cancelResponse, true, async response =>
{
Assert.Equal(responseCode, (int)response.StatusCode);
var actualResponse = await response.Content.ReadAsStringAsync();
Assert.Equal(contentString + "Response", actualResponse);
});
});
}
[Theory(Skip = "Condition not met", SkipUnless = nameof(Http2OverTlsSupported))]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http1, false)]
[InlineData(HttpProtocols.Http2, HttpProtocols.Http2, false)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http2, false)]
[InlineData(HttpProtocols.Http2, HttpProtocols.Http1, false)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http1, true)]
[InlineData(HttpProtocols.Http2, HttpProtocols.Http2, true)]
[InlineData(HttpProtocols.Http1, HttpProtocols.Http2, true)]
[InlineData(HttpProtocols.Http2, HttpProtocols.Http1, true)]
public async Task PostExpect100_SkipRequestBodyWithUnsuccessfulResponseCode(HttpProtocols proxyProtocol, HttpProtocols destProtocol, bool useContentLength)
{
var requestBodyTcs = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
var contentString = new string('a', 1024 * 1024 * 10);
var test = new TestEnvironment(
async context =>
{
context.Response.StatusCode = 400;
var responseBody = Encoding.UTF8.GetBytes(contentString + "Response");
if (useContentLength)
{
context.Response.Headers.ContentLength = responseBody.Length;
}
await context.Response.Body.WriteAsync(responseBody.AsMemory());
})
{
ProxyProtocol = proxyProtocol,
UseHttpsOnDestination = true,
UseHttpsOnProxy = true,
ConfigTransformer = (c, r) =>
{
c = c with
{
HttpRequest = new ForwarderRequestConfig
{
Version = destProtocol == HttpProtocols.Http2 ? HttpVersion.Version20 : HttpVersion.Version11,
}
};
return (c, r);
},
ConfigureProxy = proxyBuilder =>
{
proxyBuilder.Services.AddSingleton<IForwarderHttpClientFactory, TestForwarderHttpClientFactory>();
},
};
await test.Invoke(async uri =>
{
await ProcessHttpRequest(new Uri(uri), proxyProtocol, contentString, useContentLength, 400, cancelResponse: false, contentRead: false, async response =>
{
Assert.Equal(400, (int)response.StatusCode);
var actualResponse = await response.Content.ReadAsStringAsync();
Assert.Equal(contentString + "Response", actualResponse);
});
});
}
private static async Task ReadContent(Microsoft.AspNetCore.Http.HttpContext context, TaskCompletionSource<string> bodyTcs, int byteCount)
{
try
{
var buffer = new byte[byteCount];
var readCount = 0;
var totalReadCount = 0;
do
{
readCount = await context.Request.Body.ReadAsync(buffer, totalReadCount, buffer.Length - totalReadCount);
totalReadCount += readCount;
} while (readCount != 0);
var actualString = Encoding.UTF8.GetString(buffer);
bodyTcs.SetResult(actualString);
}
catch (Exception e)
{
bodyTcs.SetException(e);
}
}
private async Task ProcessHttpRequest(
Uri proxyHostUri,
HttpProtocols protocol,
string contentString,
bool useContentLength,
int expectedCode,
bool cancelResponse,
bool contentRead,
Func<HttpResponseMessage, Task> responseAction = null)
{
using var handler = new SocketsHttpHandler() { Expect100ContinueTimeout = TimeSpan.FromSeconds(60) };
handler.UseProxy = false;
handler.AllowAutoRedirect = false;
handler.SslOptions.RemoteCertificateValidationCallback = delegate { return true; };
using var client = new HttpClient(handler);
using var message = new HttpRequestMessage(HttpMethod.Post, proxyHostUri);
message.Version = protocol == HttpProtocols.Http2 ? HttpVersion.Version20 : HttpVersion.Version11;
message.Headers.ExpectContinue = true;
var content = Encoding.UTF8.GetBytes(contentString);
using var contentStream = new MemoryStream(content);
message.Content = new StreamContent(contentStream);
if (useContentLength)
{
message.Content.Headers.ContentLength = content.Length;
}
else
{
message.Headers.TransferEncodingChunked = true;
}
if (!cancelResponse)
{
using var response = await client.SendAsync(message);
Assert.Equal(expectedCode, (int)response.StatusCode);
if (contentRead)
{
Assert.Equal(content.Length, contentStream.Position);
}
else
{
Assert.Equal(0, contentStream.Position);
}
if (responseAction is not null)
{
await responseAction(response);
}
}
else
{
var exception = await Assert.ThrowsAsync<HttpRequestException>(() => client.SendAsync(message));
Assert.IsAssignableFrom<IOException>(exception.InnerException);
Assert.Equal(content.Length, contentStream.Position);
}
}
private class TestForwarderHttpClientFactory : ForwarderHttpClientFactory
{
protected override void ConfigureHandler(ForwarderHttpClientContext context, SocketsHttpHandler handler)
{
base.ConfigureHandler(context, handler);
handler.Expect100ContinueTimeout = TimeSpan.FromSeconds(60);
}
}
}