-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathResponseHeaderTests.cs
314 lines (295 loc) · 14.8 KB
/
ResponseHeaderTests.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
// 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.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Primitives;
using Xunit;
namespace Microsoft.AspNetCore.Server.HttpSys
{
public class ResponseHeaderTests
{
[ConditionalFact]
public async Task ResponseHeaders_ServerSendsDefaultHeaders_Success()
{
string address;
using (Utilities.CreateHttpServer(out address, httpContext =>
{
return Task.FromResult(0);
}))
{
HttpResponseMessage response = await SendRequestAsync(address);
response.EnsureSuccessStatusCode();
Assert.Equal(2, response.Headers.Count());
Assert.False(response.Headers.TransferEncodingChunked.HasValue);
Assert.True(response.Headers.Date.HasValue);
Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers.Server.ToString());
Assert.Single(response.Content.Headers);
Assert.Equal(0, response.Content.Headers.ContentLength);
}
}
[ConditionalFact]
public async Task ResponseHeaders_ServerSendsSingleValueKnownHeaders_Success()
{
string address;
using (Utilities.CreateHttpServer(out address, httpContext =>
{
var responseInfo = httpContext.Features.Get<IHttpResponseFeature>();
var responseHeaders = responseInfo.Headers;
responseHeaders["WWW-Authenticate"] = new string[] { "custom1" };
return Task.FromResult(0);
}))
{
#pragma warning disable SYSLIB0014 // HttpClient would merge the headers no matter what
WebRequest request = WebRequest.Create(address);
#pragma warning restore SYSLIB0014
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
Assert.Equal(4, response.Headers.Count);
Assert.Null(response.Headers["Transfer-Encoding"]);
Assert.Equal(0, response.ContentLength);
Assert.NotNull(response.Headers["Date"]);
Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers["Server"]);
Assert.Equal("custom1", response.Headers["WWW-Authenticate"]);
}
}
[ConditionalFact]
public async Task ResponseHeaders_ServerSendsMultiValueKnownHeaders_Success()
{
string address;
using (Utilities.CreateHttpServer(out address, httpContext =>
{
var responseInfo = httpContext.Features.Get<IHttpResponseFeature>();
var responseHeaders = responseInfo.Headers;
responseHeaders["WWW-Authenticate"] = new string[] { "custom1, and custom2", "custom3" };
return Task.FromResult(0);
}))
{
#pragma warning disable SYSLIB0014 // HttpClient would merge the headers no matter what
WebRequest request = WebRequest.Create(address);
#pragma warning restore SYSLIB0014
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
Assert.Equal(4, response.Headers.Count);
Assert.Null(response.Headers["Transfer-Encoding"]);
Assert.Equal(0, response.ContentLength);
Assert.NotNull(response.Headers["Date"]);
Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers["Server"]);
Assert.Equal("custom1, and custom2, custom3", response.Headers["WWW-Authenticate"]);
}
}
[ConditionalFact]
public async Task ResponseHeaders_ServerSendsCustomHeaders_Success()
{
string address;
using (Utilities.CreateHttpServer(out address, httpContext =>
{
var responseInfo = httpContext.Features.Get<IHttpResponseFeature>();
var responseHeaders = responseInfo.Headers;
responseHeaders["Custom-Header1"] = new string[] { "custom1, and custom2", "custom3" };
return Task.FromResult(0);
}))
{
#pragma warning disable SYSLIB0014 // HttpClient would merge the headers no matter what
WebRequest request = WebRequest.Create(address);
#pragma warning restore SYSLIB0014
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
Assert.Equal(4, response.Headers.Count);
Assert.Null(response.Headers["Transfer-Encoding"]);
Assert.Equal(0, response.ContentLength);
Assert.NotNull(response.Headers["Date"]);
Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers["Server"]);
Assert.Equal("custom1, and custom2, custom3", response.Headers["Custom-Header1"]);
}
}
[ConditionalFact]
public async Task ResponseHeaders_ServerSendsConnectionClose_Closed()
{
string address;
using (Utilities.CreateHttpServer(out address, httpContext =>
{
var responseInfo = httpContext.Features.Get<IHttpResponseFeature>();
var responseHeaders = responseInfo.Headers;
responseHeaders["Connection"] = new string[] { "Close" };
return httpContext.Response.Body.FlushAsync(); // Http.Sys adds the Content-Length: header for us if we don't flush
}))
{
HttpResponseMessage response = await SendRequestAsync(address);
response.EnsureSuccessStatusCode();
Assert.True(response.Headers.ConnectionClose.Value);
Assert.Equal(new string[] { "close" }, response.Headers.GetValues("Connection"));
Assert.True(response.Headers.TransferEncodingChunked.HasValue);
Assert.True(response.Headers.TransferEncodingChunked);
IEnumerable<string> values;
var result = response.Content.Headers.TryGetValues("Content-Length", out values);
Assert.False(result);
}
}
[ConditionalFact]
public async Task ResponseHeaders_HTTP10Request_Gets11Close()
{
string address;
using (Utilities.CreateHttpServer(out address, httpContext =>
{
return Task.FromResult(0);
}))
{
using (HttpClient client = new HttpClient())
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, address);
request.Version = new Version(1, 0);
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Assert.Equal(new Version(1, 1), response.Version);
Assert.True(response.Headers.ConnectionClose.Value);
Assert.Equal(new string[] { "close" }, response.Headers.GetValues("Connection"));
Assert.False(response.Headers.TransferEncodingChunked.HasValue);
}
}
}
[ConditionalFact]
public async Task ResponseHeaders_HTTP10RequestWithChunkedHeader_ManualChunking()
{
string address;
using (Utilities.CreateHttpServer(out address, httpContext =>
{
var response = httpContext.Response;
var responseHeaders = response.Headers;
responseHeaders["Transfer-Encoding"] = new string[] { "chunked" };
var responseBytes = Encoding.ASCII.GetBytes("10\r\nManually Chunked\r\n0\r\n\r\n");
return response.Body.WriteAsync(responseBytes, 0, responseBytes.Length);
}))
{
using (HttpClient client = new HttpClient())
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, address);
request.Version = new Version(1, 0);
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Assert.Equal(new Version(1, 1), response.Version);
Assert.True(response.Headers.TransferEncodingChunked.HasValue);
Assert.False(response.Content.Headers.Contains("Content-Length"));
Assert.True(response.Headers.ConnectionClose.Value);
Assert.Equal(new string[] { "close" }, response.Headers.GetValues("Connection"));
Assert.Equal("Manually Chunked", await response.Content.ReadAsStringAsync());
}
}
}
[ConditionalFact]
public async Task Headers_FlushSendsHeaders_Success()
{
string address;
using (Utilities.CreateHttpServer(out address, httpContext =>
{
httpContext.Features.Get<IHttpBodyControlFeature>().AllowSynchronousIO = true;
var response = httpContext.Response;
var responseHeaders = response.Headers;
responseHeaders.Add("Custom1", new string[] { "value1a", "value1b" });
responseHeaders.Add("Custom2", new string[] { "value2a, value2b" });
var body = response.Body;
Assert.False(response.HasStarted);
body.Flush();
Assert.True(response.HasStarted);
Assert.Throws<InvalidOperationException>(() => response.StatusCode = 404);
Assert.Throws<InvalidOperationException>(() => responseHeaders.Add("Custom3", new string[] { "value3a, value3b", "value3c" }));
return Task.FromResult(0);
}))
{
HttpResponseMessage response = await SendRequestAsync(address);
response.EnsureSuccessStatusCode();
Assert.Equal(5, response.Headers.Count()); // Date, Server, Chunked
Assert.Equal(2, response.Headers.GetValues("Custom1").Count());
Assert.Equal("value1a", response.Headers.GetValues("Custom1").First());
Assert.Equal("value1b", response.Headers.GetValues("Custom1").Skip(1).First());
Assert.Single(response.Headers.GetValues("Custom2"));
Assert.Equal("value2a, value2b", response.Headers.GetValues("Custom2").First());
}
}
[ConditionalFact]
public async Task Headers_FlushAsyncSendsHeaders_Success()
{
string address;
using (Utilities.CreateHttpServer(out address, async httpContext =>
{
var response = httpContext.Response;
var responseHeaders = response.Headers;
responseHeaders.Add("Custom1", new string[] { "value1a", "value1b" });
responseHeaders.Add("Custom2", new string[] { "value2a, value2b" });
var body = response.Body;
Assert.False(response.HasStarted);
await body.FlushAsync();
Assert.True(response.HasStarted);
Assert.Throws<InvalidOperationException>(() => response.StatusCode = 404);
Assert.Throws<InvalidOperationException>(() => responseHeaders.Add("Custom3", new string[] { "value3a, value3b", "value3c" }));
}))
{
HttpResponseMessage response = await SendRequestAsync(address);
response.EnsureSuccessStatusCode();
Assert.Equal(5, response.Headers.Count()); // Date, Server, Chunked
Assert.Equal(2, response.Headers.GetValues("Custom1").Count());
Assert.Equal("value1a", response.Headers.GetValues("Custom1").First());
Assert.Equal("value1b", response.Headers.GetValues("Custom1").Skip(1).First());
Assert.Single(response.Headers.GetValues("Custom2"));
Assert.Equal("value2a, value2b", response.Headers.GetValues("Custom2").First());
}
}
[ConditionalTheory, MemberData(nameof(NullHeaderData))]
public async Task Headers_IgnoreNullHeaders(string headerName, StringValues headerValue, StringValues expectedValue)
{
string address;
using (Utilities.CreateHttpServer(out address, httpContext =>
{
var responseHeaders = httpContext.Response.Headers;
responseHeaders.Add(headerName, headerValue);
return Task.FromResult(0);
}))
{
HttpResponseMessage response = await SendRequestAsync(address);
response.EnsureSuccessStatusCode();
var headers = response.Headers;
if (expectedValue.Count == 0)
{
Assert.False(headers.Contains(headerName));
}
else
{
Assert.True(headers.Contains(headerName));
Assert.Equal(headers.GetValues(headerName), expectedValue);
}
}
}
public static TheoryData<string, StringValues, StringValues> NullHeaderData
{
get
{
var dataset = new TheoryData<string, StringValues, StringValues>();
// Unknown headers
dataset.Add("NullString", (string)null, (string)null);
dataset.Add("EmptyString", "", "");
dataset.Add("NullStringArray", new string[] { null }, "");
dataset.Add("EmptyStringArray", new string[] { "" }, "");
dataset.Add("MixedStringArray", new string[] { null, "" }, new string[] { "", "" });
// Known headers
dataset.Add("Location", (string)null, (string)null);
dataset.Add("Location", "", (string)null);
dataset.Add("Location", new string[] { null }, (string)null);
dataset.Add("Location", new string[] { "" }, (string)null);
dataset.Add("Location", new string[] { "a" }, "a");
dataset.Add("Location", new string[] { null, "" }, (string)null);
dataset.Add("Location", new string[] { null, "", "a", "b" }, new string[] { "a", "b" });
return dataset;
}
}
private async Task<HttpResponseMessage> SendRequestAsync(string uri)
{
using (HttpClient client = new HttpClient())
{
return await client.GetAsync(uri);
}
}
}
}