1
- // Copyright (c) .NET Foundation. All rights reserved.
1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
5
using System . IO ;
6
6
using System . Threading . Tasks ;
7
+ using Microsoft . AspNetCore . Http ;
7
8
using Microsoft . AspNetCore . Http . Features ;
8
9
using Microsoft . AspNetCore . Server . Kestrel . Core ;
9
10
using Microsoft . AspNetCore . Server . Kestrel . Core . Internal . Infrastructure ;
10
11
using Microsoft . AspNetCore . Server . Kestrel . Tests ;
11
12
using Microsoft . AspNetCore . Testing ;
12
13
using Microsoft . Extensions . Logging . Testing ;
14
+ using Microsoft . Net . Http . Headers ;
13
15
using Xunit ;
14
16
15
17
namespace Microsoft . AspNetCore . Server . Kestrel . FunctionalTests
@@ -146,32 +148,43 @@ await connection.Receive("HTTP/1.1 101 Switching Protocols",
146
148
}
147
149
148
150
[ Fact ]
149
- public async Task RejectsRequestWithContentLengthAndUpgrade ( )
151
+ public async Task AcceptsRequestWithContentLengthAndUpgrade ( )
150
152
{
151
- using ( var server = new TestServer ( context => Task . CompletedTask , new TestServiceContext ( LoggerFactory ) ) )
153
+ using ( var server = new TestServer ( async context =>
154
+ {
155
+ var feature = context . Features . Get < IHttpUpgradeFeature > ( ) ;
156
+ Assert . False ( feature . IsUpgradableRequest ) ;
157
+ Assert . Equal ( 1 , context . Request . ContentLength ) ;
158
+ Assert . Equal ( 1 , await context . Request . Body . ReadAsync ( new byte [ 10 ] , 0 , 10 ) ) ;
159
+ } , new TestServiceContext ( LoggerFactory ) ) )
152
160
using ( var connection = server . CreateConnection ( ) )
153
161
{
154
162
await connection . Send ( "POST / HTTP/1.1" ,
155
163
"Host:" ,
156
164
"Content-Length: 1" ,
157
165
"Connection: Upgrade" ,
158
166
"" ,
159
- "" ) ;
167
+ "A " ) ;
160
168
161
- await connection . ReceiveForcedEnd (
162
- "HTTP/1.1 400 Bad Request" ,
163
- "Connection: close" ,
164
- $ "Date: { server . Context . DateHeaderValue } ",
165
- "Content-Length: 0" ,
166
- "" ,
167
- "" ) ;
169
+ await connection . Receive ( "HTTP/1.1 200 OK" ) ;
168
170
}
169
171
}
170
172
171
173
[ Fact ]
172
174
public async Task AcceptsRequestWithNoContentLengthAndUpgrade ( )
173
175
{
174
- using ( var server = new TestServer ( context => Task . CompletedTask , new TestServiceContext ( LoggerFactory ) ) )
176
+ using ( var server = new TestServer ( async context =>
177
+ {
178
+ var feature = context . Features . Get < IHttpUpgradeFeature > ( ) ;
179
+ Assert . True ( feature . IsUpgradableRequest ) ;
180
+
181
+ if ( HttpMethods . IsPost ( context . Request . Method ) )
182
+ {
183
+ Assert . Equal ( 0 , context . Request . ContentLength ) ;
184
+ }
185
+
186
+ Assert . Equal ( 0 , await context . Request . Body . ReadAsync ( new byte [ 10 ] , 0 , 10 ) ) ;
187
+ } , new TestServiceContext ( LoggerFactory ) ) )
175
188
{
176
189
using ( var connection = server . CreateConnection ( ) )
177
190
{
@@ -193,24 +206,30 @@ await connection.Send("POST / HTTP/1.1",
193
206
}
194
207
195
208
[ Fact ]
196
- public async Task RejectsRequestWithChunkedEncodingAndUpgrade ( )
209
+ public async Task AcceptsRequestWithChunkedEncodingAndUpgrade ( )
197
210
{
198
- using ( var server = new TestServer ( context => Task . CompletedTask , new TestServiceContext ( LoggerFactory ) ) )
211
+ using ( var server = new TestServer ( async context =>
212
+ {
213
+ var feature = context . Features . Get < IHttpUpgradeFeature > ( ) ;
214
+ Assert . Null ( context . Request . ContentLength ) ;
215
+ Assert . False ( feature . IsUpgradableRequest ) ;
216
+ Assert . True ( HttpMethods . IsPost ( context . Request . Method ) ) ;
217
+ Assert . False ( feature . IsUpgradableRequest ) ;
218
+ Assert . Equal ( "chunked" , context . Request . Headers [ HeaderNames . TransferEncoding ] ) ;
219
+ Assert . Equal ( 11 , await context . Request . Body . ReadAsync ( new byte [ 12 ] , 0 , 12 ) ) ;
220
+ } , new TestServiceContext ( LoggerFactory ) ) )
199
221
using ( var connection = server . CreateConnection ( ) )
200
222
{
201
223
await connection . Send ( "POST / HTTP/1.1" ,
202
224
"Host:" ,
203
225
"Transfer-Encoding: chunked" ,
204
226
"Connection: Upgrade" ,
205
227
"" ,
206
- "" ) ;
207
- await connection . ReceiveForcedEnd (
208
- "HTTP/1.1 400 Bad Request" ,
209
- "Connection: close" ,
210
- $ "Date: { server . Context . DateHeaderValue } ",
211
- "Content-Length: 0" ,
228
+ "B" , "Hello World" ,
229
+ "0" ,
212
230
"" ,
213
231
"" ) ;
232
+ await connection . Receive ( "HTTP/1.1 200 OK" ) ;
214
233
}
215
234
}
216
235
0 commit comments