5
5
using System . Linq ;
6
6
using System . Net ;
7
7
using System . Net . Http ;
8
+ using System . Net . Sockets ;
8
9
using System . Text ;
9
10
using System . Threading . Tasks ;
10
11
using Microsoft . AspNetCore . Builder ;
@@ -223,8 +224,24 @@ public Task ResponseStatusCodeSetBeforeHttpContextDisposeRequestAbortedAppExcept
223
224
expectedServerStatusCode : HttpStatusCode . InternalServerError ) ;
224
225
}
225
226
226
- private static async Task ResponseStatusCodeSetBeforeHttpContextDispose ( RequestDelegate handler ,
227
- HttpStatusCode ? expectedClientStatusCode , HttpStatusCode expectedServerStatusCode )
227
+ [ Fact ]
228
+ public Task ResponseStatusCodeSetBeforeHttpContextDisposedRequestMalformed ( )
229
+ {
230
+ return ResponseStatusCodeSetBeforeHttpContextDispose (
231
+ context =>
232
+ {
233
+ return TaskCache . CompletedTask ;
234
+ } ,
235
+ expectedClientStatusCode : null ,
236
+ expectedServerStatusCode : HttpStatusCode . BadRequest ,
237
+ sendMalformedRequest : true ) ;
238
+ }
239
+
240
+ private static async Task ResponseStatusCodeSetBeforeHttpContextDispose (
241
+ RequestDelegate handler ,
242
+ HttpStatusCode ? expectedClientStatusCode ,
243
+ HttpStatusCode expectedServerStatusCode ,
244
+ bool sendMalformedRequest = false )
228
245
{
229
246
var mockHttpContextFactory = new Mock < IHttpContextFactory > ( ) ;
230
247
mockHttpContextFactory . Setup ( f => f . Create ( It . IsAny < IFeatureCollection > ( ) ) )
@@ -250,24 +267,38 @@ private static async Task ResponseStatusCodeSetBeforeHttpContextDispose(RequestD
250
267
{
251
268
host . Start ( ) ;
252
269
253
- using ( var client = new HttpClient ( ) )
270
+ if ( ! sendMalformedRequest )
254
271
{
255
- try
256
- {
257
- var response = await client . GetAsync ( $ "http://localhost:{ host . GetPort ( ) } /") ;
258
- Assert . Equal ( expectedClientStatusCode , response . StatusCode ) ;
259
- }
260
- catch
272
+ using ( var client = new HttpClient ( ) )
261
273
{
262
- if ( expectedClientStatusCode != null )
274
+ try
275
+ {
276
+ var response = await client . GetAsync ( $ "http://localhost:{ host . GetPort ( ) } /") ;
277
+ Assert . Equal ( expectedClientStatusCode , response . StatusCode ) ;
278
+ }
279
+ catch
263
280
{
264
- throw ;
281
+ if ( expectedClientStatusCode != null )
282
+ {
283
+ throw ;
284
+ }
265
285
}
266
286
}
267
287
}
288
+ else
289
+ {
290
+ using ( var socket = new Socket ( AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) )
291
+ {
292
+ socket . Connect ( new IPEndPoint ( IPAddress . Loopback , host . GetPort ( ) ) ) ;
293
+ socket . Send ( Encoding . ASCII . GetBytes (
294
+ "POST / HTTP/1.1\r \n " +
295
+ "Transfer-Encoding: chunked\r \n " +
296
+ "\r \n " +
297
+ "wrong" ) ) ;
298
+ }
299
+ }
268
300
269
301
var disposedStatusCode = await disposedTcs . Task . TimeoutAfter ( TimeSpan . FromSeconds ( 10 ) ) ;
270
-
271
302
Assert . Equal ( expectedServerStatusCode , ( HttpStatusCode ) disposedStatusCode ) ;
272
303
}
273
304
}
0 commit comments