7
7
using System . Net . Http ;
8
8
using System . Threading ;
9
9
using System . Threading . Tasks ;
10
+ using Microsoft . AspNetCore . Http ;
10
11
using Microsoft . AspNetCore . Http . Features ;
11
12
using Microsoft . AspNetCore . Testing ;
12
13
using Xunit ;
@@ -159,10 +160,12 @@ public async Task ContentLengthExceedsLimit_ReadSync_ThrowsImmediately()
159
160
Assert . False ( feature . IsReadOnly ) ;
160
161
Assert . Equal ( 11 , httpContext . Request . ContentLength ) ;
161
162
byte [ ] input = new byte [ 100 ] ;
162
- var ex = Assert . Throws < IOException > ( ( ) => httpContext . Request . Body . Read ( input , 0 , input . Length ) ) ;
163
+ var ex = Assert . Throws < BadHttpRequestException > ( ( ) => httpContext . Request . Body . Read ( input , 0 , input . Length ) ) ;
163
164
Assert . Equal ( "The request's Content-Length 11 is larger than the request body size limit 10." , ex . Message ) ;
164
- ex = Assert . Throws < IOException > ( ( ) => httpContext . Request . Body . Read ( input , 0 , input . Length ) ) ;
165
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
166
+ ex = Assert . Throws < BadHttpRequestException > ( ( ) => httpContext . Request . Body . Read ( input , 0 , input . Length ) ) ;
165
167
Assert . Equal ( "The request's Content-Length 11 is larger than the request body size limit 10." , ex . Message ) ;
168
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
166
169
return Task . FromResult ( 0 ) ;
167
170
} , options => options . MaxRequestBodySize = 10 ) )
168
171
{
@@ -182,10 +185,12 @@ public async Task ContentLengthExceedsLimit_ReadAsync_ThrowsImmediately()
182
185
Assert . False ( feature . IsReadOnly ) ;
183
186
Assert . Equal ( 11 , httpContext . Request . ContentLength ) ;
184
187
byte [ ] input = new byte [ 100 ] ;
185
- var ex = Assert . Throws < IOException > ( ( ) => { var t = httpContext . Request . Body . ReadAsync ( input , 0 , input . Length ) ; } ) ;
188
+ var ex = Assert . Throws < BadHttpRequestException > ( ( ) => { var t = httpContext . Request . Body . ReadAsync ( input , 0 , input . Length ) ; } ) ;
186
189
Assert . Equal ( "The request's Content-Length 11 is larger than the request body size limit 10." , ex . Message ) ;
187
- ex = Assert . Throws < IOException > ( ( ) => { var t = httpContext . Request . Body . ReadAsync ( input , 0 , input . Length ) ; } ) ;
190
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
191
+ ex = Assert . Throws < BadHttpRequestException > ( ( ) => { var t = httpContext . Request . Body . ReadAsync ( input , 0 , input . Length ) ; } ) ;
188
192
Assert . Equal ( "The request's Content-Length 11 is larger than the request body size limit 10." , ex . Message ) ;
193
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
189
194
return Task . FromResult ( 0 ) ;
190
195
} , options => options . MaxRequestBodySize = 10 ) )
191
196
{
@@ -205,10 +210,12 @@ public async Task ContentLengthExceedsLimit_ReadBeginEnd_ThrowsImmediately()
205
210
Assert . False ( feature . IsReadOnly ) ;
206
211
Assert . Equal ( 11 , httpContext . Request . ContentLength ) ;
207
212
byte [ ] input = new byte [ 100 ] ;
208
- var ex = Assert . Throws < IOException > ( ( ) => httpContext . Request . Body . BeginRead ( input , 0 , input . Length , null , null ) ) ;
213
+ var ex = Assert . Throws < BadHttpRequestException > ( ( ) => httpContext . Request . Body . BeginRead ( input , 0 , input . Length , null , null ) ) ;
209
214
Assert . Equal ( "The request's Content-Length 11 is larger than the request body size limit 10." , ex . Message ) ;
210
- ex = Assert . Throws < IOException > ( ( ) => httpContext . Request . Body . BeginRead ( input , 0 , input . Length , null , null ) ) ;
215
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
216
+ ex = Assert . Throws < BadHttpRequestException > ( ( ) => httpContext . Request . Body . BeginRead ( input , 0 , input . Length , null , null ) ) ;
211
217
Assert . Equal ( "The request's Content-Length 11 is larger than the request body size limit 10." , ex . Message ) ;
218
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
212
219
return Task . FromResult ( 0 ) ;
213
220
} , options => options . MaxRequestBodySize = 10 ) )
214
221
{
@@ -229,10 +236,12 @@ public async Task ChunkedExceedsLimit_ReadSync_ThrowsAtLimit()
229
236
Assert . False ( feature . IsReadOnly ) ;
230
237
Assert . Null ( httpContext . Request . ContentLength ) ;
231
238
byte [ ] input = new byte [ 100 ] ;
232
- var ex = Assert . Throws < IOException > ( ( ) => httpContext . Request . Body . Read ( input , 0 , input . Length ) ) ;
239
+ var ex = Assert . Throws < BadHttpRequestException > ( ( ) => httpContext . Request . Body . Read ( input , 0 , input . Length ) ) ;
233
240
Assert . Equal ( "The total number of bytes read 11 has exceeded the request body size limit 10." , ex . Message ) ;
234
- ex = Assert . Throws < IOException > ( ( ) => httpContext . Request . Body . Read ( input , 0 , input . Length ) ) ;
241
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
242
+ ex = Assert . Throws < BadHttpRequestException > ( ( ) => httpContext . Request . Body . Read ( input , 0 , input . Length ) ) ;
235
243
Assert . Equal ( "The total number of bytes read 11 has exceeded the request body size limit 10." , ex . Message ) ;
244
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
236
245
return Task . FromResult ( 0 ) ;
237
246
} , options => options . MaxRequestBodySize = 10 ) )
238
247
{
@@ -252,9 +261,11 @@ public async Task ChunkedExceedsLimit_ReadAsync_ThrowsAtLimit()
252
261
Assert . False ( feature . IsReadOnly ) ;
253
262
Assert . Null ( httpContext . Request . ContentLength ) ;
254
263
byte [ ] input = new byte [ 100 ] ;
255
- var ex = await Assert . ThrowsAsync < IOException > ( ( ) => httpContext . Request . Body . ReadAsync ( input , 0 , input . Length ) ) ;
264
+ var ex = await Assert . ThrowsAsync < BadHttpRequestException > ( ( ) => httpContext . Request . Body . ReadAsync ( input , 0 , input . Length ) ) ;
256
265
Assert . Equal ( "The total number of bytes read 11 has exceeded the request body size limit 10." , ex . Message ) ;
257
- ex = await Assert . ThrowsAsync < IOException > ( ( ) => httpContext . Request . Body . ReadAsync ( input , 0 , input . Length ) ) ;
266
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
267
+ ex = await Assert . ThrowsAsync < BadHttpRequestException > ( ( ) => httpContext . Request . Body . ReadAsync ( input , 0 , input . Length ) ) ;
268
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
258
269
Assert . Equal ( "The total number of bytes read 11 has exceeded the request body size limit 10." , ex . Message ) ;
259
270
} , options => options . MaxRequestBodySize = 10 ) )
260
271
{
@@ -275,10 +286,12 @@ public async Task ChunkedExceedsLimit_ReadBeginEnd_ThrowsAtLimit()
275
286
Assert . Null ( httpContext . Request . ContentLength ) ;
276
287
byte [ ] input = new byte [ 100 ] ;
277
288
var body = httpContext . Request . Body ;
278
- var ex = Assert . Throws < IOException > ( ( ) => body . EndRead ( body . BeginRead ( input , 0 , input . Length , null , null ) ) ) ;
289
+ var ex = Assert . Throws < BadHttpRequestException > ( ( ) => body . EndRead ( body . BeginRead ( input , 0 , input . Length , null , null ) ) ) ;
279
290
Assert . Equal ( "The total number of bytes read 11 has exceeded the request body size limit 10." , ex . Message ) ;
280
- ex = Assert . Throws < IOException > ( ( ) => body . EndRead ( body . BeginRead ( input , 0 , input . Length , null , null ) ) ) ;
291
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
292
+ ex = Assert . Throws < BadHttpRequestException > ( ( ) => body . EndRead ( body . BeginRead ( input , 0 , input . Length , null , null ) ) ) ;
281
293
Assert . Equal ( "The total number of bytes read 11 has exceeded the request body size limit 10." , ex . Message ) ;
294
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
282
295
return Task . FromResult ( 0 ) ;
283
296
} , options => options . MaxRequestBodySize = 10 ) )
284
297
{
@@ -303,8 +316,9 @@ public async Task Chunked_ReadSyncPartialBodyUnderLimit_ThrowsAfterLimit()
303
316
int read = httpContext . Request . Body . Read ( input , 0 , input . Length ) ;
304
317
Assert . Equal ( 10 , read ) ;
305
318
content . Block . Release ( ) ;
306
- var ex = Assert . Throws < IOException > ( ( ) => httpContext . Request . Body . Read ( input , 0 , input . Length ) ) ;
319
+ var ex = Assert . Throws < BadHttpRequestException > ( ( ) => httpContext . Request . Body . Read ( input , 0 , input . Length ) ) ;
307
320
Assert . Equal ( "The total number of bytes read 20 has exceeded the request body size limit 10." , ex . Message ) ;
321
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
308
322
return Task . FromResult ( 0 ) ;
309
323
} , options => options . MaxRequestBodySize = 10 ) )
310
324
{
@@ -328,8 +342,9 @@ public async Task Chunked_ReadAsyncPartialBodyUnderLimit_ThrowsAfterLimit()
328
342
int read = await httpContext . Request . Body . ReadAsync ( input , 0 , input . Length ) ;
329
343
Assert . Equal ( 10 , read ) ;
330
344
content . Block . Release ( ) ;
331
- var ex = await Assert . ThrowsAsync < IOException > ( ( ) => httpContext . Request . Body . ReadAsync ( input , 0 , input . Length ) ) ;
345
+ var ex = await Assert . ThrowsAsync < BadHttpRequestException > ( ( ) => httpContext . Request . Body . ReadAsync ( input , 0 , input . Length ) ) ;
332
346
Assert . Equal ( "The total number of bytes read 20 has exceeded the request body size limit 10." , ex . Message ) ;
347
+ Assert . Equal ( StatusCodes . Status413PayloadTooLarge , ex . StatusCode ) ;
333
348
} , options => options . MaxRequestBodySize = 10 ) )
334
349
{
335
350
string response = await SendRequestAsync ( address , content , chunked : true ) ;
0 commit comments