Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 8bb2b85

Browse files
author
Cesar Blum Silveira
committed
Move request target processing tests into their own class.
1 parent efa1890 commit 8bb2b85

File tree

3 files changed

+131
-198
lines changed

3 files changed

+131
-198
lines changed

test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/RequestTests.cs

Lines changed: 0 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
using System;
55
using System.Globalization;
66
using System.Net.Http;
7-
using System.Net.Sockets;
8-
using System.Text;
97
using System.Threading.Tasks;
108
using Microsoft.AspNetCore.Builder;
119
using Microsoft.AspNetCore.Hosting;
1210
using Microsoft.AspNetCore.Http;
13-
using Microsoft.AspNetCore.Http.Features;
1411
using Microsoft.AspNetCore.Testing.xunit;
1512
using Newtonsoft.Json;
1613
using Newtonsoft.Json.Linq;
@@ -159,166 +156,6 @@ public async Task DoesNotHangOnConnectionCloseRequest()
159156
}
160157
}
161158

162-
[Fact]
163-
public void RequestPathIsNormalized()
164-
{
165-
var builder = new WebHostBuilder()
166-
.UseKestrel()
167-
.UseUrls($"http://127.0.0.1:0/\u0041\u030A")
168-
.Configure(app =>
169-
{
170-
app.Run(async context =>
171-
{
172-
var connection = context.Connection;
173-
Assert.Equal("/\u00C5", context.Request.PathBase.Value);
174-
Assert.Equal("/B/\u00C5", context.Request.Path.Value);
175-
await context.Response.WriteAsync("hello, world");
176-
});
177-
});
178-
179-
using (var host = builder.Build())
180-
{
181-
host.Start();
182-
183-
using (var socket = TestConnection.CreateConnectedLoopbackSocket(host.GetPort()))
184-
{
185-
socket.Send(Encoding.ASCII.GetBytes("GET /%41%CC%8A/A/../B/%41%CC%8A HTTP/1.1\r\n\r\n"));
186-
socket.Shutdown(SocketShutdown.Send);
187-
188-
var response = new StringBuilder();
189-
var buffer = new byte[4096];
190-
while (true)
191-
{
192-
var length = socket.Receive(buffer);
193-
if (length == 0)
194-
{
195-
break;
196-
}
197-
198-
response.Append(Encoding.ASCII.GetString(buffer, 0, length));
199-
}
200-
201-
Assert.StartsWith("HTTP/1.1 200 OK", response.ToString());
202-
}
203-
}
204-
}
205-
206-
[Theory]
207-
[InlineData("/")]
208-
[InlineData("/.")]
209-
[InlineData("/..")]
210-
[InlineData("/./.")]
211-
[InlineData("/./..")]
212-
[InlineData("/../.")]
213-
[InlineData("/../..")]
214-
[InlineData("/path")]
215-
[InlineData("/path?foo=1&bar=2")]
216-
[InlineData("/hello%20world")]
217-
[InlineData("/hello%20world?foo=1&bar=2")]
218-
[InlineData("/base/path")]
219-
[InlineData("/base/path?foo=1&bar=2")]
220-
[InlineData("/base/hello%20world")]
221-
[InlineData("/base/hello%20world?foo=1&bar=2")]
222-
public void RequestFeatureContainsRawTarget(string requestTarget)
223-
{
224-
var builder = new WebHostBuilder()
225-
.UseKestrel()
226-
.UseUrls($"http://127.0.0.1:0/base")
227-
.Configure(app =>
228-
{
229-
app.Run(async context =>
230-
{
231-
var connection = context.Connection;
232-
Assert.Equal(requestTarget, context.Features.Get<IHttpRequestFeature>().RawTarget);
233-
await context.Response.WriteAsync("hello, world");
234-
});
235-
});
236-
237-
using (var host = builder.Build())
238-
{
239-
host.Start();
240-
241-
using (var socket = TestConnection.CreateConnectedLoopbackSocket(host.GetPort()))
242-
{
243-
socket.Send(Encoding.ASCII.GetBytes($"GET {requestTarget} HTTP/1.1\r\n\r\n"));
244-
socket.Shutdown(SocketShutdown.Send);
245-
246-
var response = new StringBuilder();
247-
var buffer = new byte[4096];
248-
while (true)
249-
{
250-
var length = socket.Receive(buffer);
251-
if (length == 0)
252-
{
253-
break;
254-
}
255-
256-
response.Append(Encoding.ASCII.GetString(buffer, 0, length));
257-
}
258-
259-
Assert.StartsWith("HTTP/1.1 200 OK", response.ToString());
260-
}
261-
}
262-
}
263-
264-
[Theory]
265-
[InlineData("*")]
266-
[InlineData("*/?arg=value")]
267-
[InlineData("*?arg=value")]
268-
[InlineData("DoesNotStartWith/")]
269-
[InlineData("DoesNotStartWith/?arg=value")]
270-
[InlineData("DoesNotStartWithSlash?arg=value")]
271-
[InlineData("./")]
272-
[InlineData("../")]
273-
[InlineData("../.")]
274-
[InlineData(".././")]
275-
[InlineData("../..")]
276-
[InlineData("../../")]
277-
public void NonPathRequestTargetSetInRawTarget(string requestTarget)
278-
{
279-
var builder = new WebHostBuilder()
280-
.UseKestrel()
281-
.UseUrls($"http://127.0.0.1:0/")
282-
.Configure(app =>
283-
{
284-
app.Run(async context =>
285-
{
286-
var connection = context.Connection;
287-
Assert.Equal(requestTarget, context.Features.Get<IHttpRequestFeature>().RawTarget);
288-
Assert.Empty(context.Request.Path.Value);
289-
Assert.Empty(context.Request.PathBase.Value);
290-
Assert.Empty(context.Request.QueryString.Value);
291-
await context.Response.WriteAsync("hello, world");
292-
});
293-
});
294-
295-
using (var host = builder.Build())
296-
{
297-
host.Start();
298-
299-
using (var socket = TestConnection.CreateConnectedLoopbackSocket(host.GetPort()))
300-
{
301-
socket.Send(Encoding.ASCII.GetBytes($"GET {requestTarget} HTTP/1.1\r\n\r\n"));
302-
socket.Shutdown(SocketShutdown.Send);
303-
304-
var response = new StringBuilder();
305-
var buffer = new byte[4096];
306-
while (true)
307-
{
308-
var length = socket.Receive(buffer);
309-
if (length == 0)
310-
{
311-
break;
312-
}
313-
314-
response.Append(Encoding.ASCII.GetString(buffer, 0, length));
315-
}
316-
317-
Assert.StartsWith("HTTP/1.1 200 OK", response.ToString());
318-
}
319-
}
320-
}
321-
322159
private async Task TestRemoteIPAddress(string registerAddress, string requestAddress, string expectAddress)
323160
{
324161
var builder = new WebHostBuilder()

test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestConnection.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Http.Features;
7+
using Xunit;
8+
9+
namespace Microsoft.AspNetCore.Server.KestrelTests
10+
{
11+
public class RequestTargetProcessingTests
12+
{
13+
[Fact]
14+
public async Task RequestPathIsNormalized()
15+
{
16+
var testContext = new TestServiceContext();
17+
18+
using (var server = new TestServer(async context =>
19+
{
20+
Assert.Equal("/\u00C5", context.Request.PathBase.Value);
21+
Assert.Equal("/B/\u00C5", context.Request.Path.Value);
22+
23+
context.Response.Headers["Content-Length"] = new[] { "11" };
24+
await context.Response.WriteAsync("Hello World");
25+
}, testContext, "http://127.0.0.1/\u0041\u030A"))
26+
{
27+
using (var connection = server.CreateConnection())
28+
{
29+
await connection.SendEnd(
30+
"GET /%41%CC%8A/A/../B/%41%CC%8A HTTP/1.0",
31+
"",
32+
"");
33+
await connection.ReceiveEnd(
34+
"HTTP/1.1 200 OK",
35+
$"Date: {testContext.DateHeaderValue}",
36+
"Content-Length: 11",
37+
"",
38+
"Hello World");
39+
}
40+
}
41+
}
42+
43+
[Theory]
44+
[InlineData("/")]
45+
[InlineData("/.")]
46+
[InlineData("/..")]
47+
[InlineData("/./.")]
48+
[InlineData("/./..")]
49+
[InlineData("/../.")]
50+
[InlineData("/../..")]
51+
[InlineData("/path")]
52+
[InlineData("/path?foo=1&bar=2")]
53+
[InlineData("/hello%20world")]
54+
[InlineData("/hello%20world?foo=1&bar=2")]
55+
[InlineData("/base/path")]
56+
[InlineData("/base/path?foo=1&bar=2")]
57+
[InlineData("/base/hello%20world")]
58+
[InlineData("/base/hello%20world?foo=1&bar=2")]
59+
public async Task RequestFeatureContainsRawTarget(string requestTarget)
60+
{
61+
var testContext = new TestServiceContext();
62+
63+
using (var server = new TestServer(async context =>
64+
{
65+
Assert.Equal(requestTarget, context.Features.Get<IHttpRequestFeature>().RawTarget);
66+
67+
context.Response.Headers["Content-Length"] = new[] { "11" };
68+
await context.Response.WriteAsync("Hello World");
69+
}, testContext))
70+
{
71+
using (var connection = server.CreateConnection())
72+
{
73+
await connection.SendEnd(
74+
$"GET {requestTarget} HTTP/1.0",
75+
"",
76+
"");
77+
await connection.ReceiveEnd(
78+
"HTTP/1.1 200 OK",
79+
$"Date: {testContext.DateHeaderValue}",
80+
"Content-Length: 11",
81+
"",
82+
"Hello World");
83+
}
84+
}
85+
}
86+
87+
[Theory]
88+
[InlineData("*")]
89+
[InlineData("*/?arg=value")]
90+
[InlineData("*?arg=value")]
91+
[InlineData("DoesNotStartWith/")]
92+
[InlineData("DoesNotStartWith/?arg=value")]
93+
[InlineData("DoesNotStartWithSlash?arg=value")]
94+
[InlineData("./")]
95+
[InlineData("../")]
96+
[InlineData("../.")]
97+
[InlineData(".././")]
98+
[InlineData("../..")]
99+
[InlineData("../../")]
100+
public async Task NonPathRequestTargetSetInRawTarget(string requestTarget)
101+
{
102+
var testContext = new TestServiceContext();
103+
104+
using (var server = new TestServer(async context =>
105+
{
106+
Assert.Equal(requestTarget, context.Features.Get<IHttpRequestFeature>().RawTarget);
107+
Assert.Empty(context.Request.Path.Value);
108+
Assert.Empty(context.Request.PathBase.Value);
109+
Assert.Empty(context.Request.QueryString.Value);
110+
111+
context.Response.Headers["Content-Length"] = new[] { "11" };
112+
await context.Response.WriteAsync("Hello World");
113+
}, testContext))
114+
{
115+
using (var connection = server.CreateConnection())
116+
{
117+
await connection.SendEnd(
118+
$"GET {requestTarget} HTTP/1.0",
119+
"",
120+
"");
121+
await connection.ReceiveEnd(
122+
"HTTP/1.1 200 OK",
123+
$"Date: {testContext.DateHeaderValue}",
124+
"Content-Length: 11",
125+
"",
126+
"Hello World");
127+
}
128+
}
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)