1
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
- using System ;
5
4
using System . Net ;
6
5
using System . Net . Http ;
7
6
using System . Net . Http . Headers ;
8
7
using System . Threading . Tasks ;
9
- using Microsoft . AspNetCore . Mvc . Infrastructure ;
10
8
using Microsoft . AspNetCore . Testing . xunit ;
11
9
using Xunit ;
12
10
@@ -189,11 +187,13 @@ public async Task FileFromDisk_ReturnsFileWithFileName()
189
187
Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
190
188
}
191
189
192
- [ Fact ]
193
- public async Task FileFromDisk_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( )
190
+ [ Theory ]
191
+ [ InlineData ( "GET" , "This is a sample text file" ) ]
192
+ [ InlineData ( "HEAD" , "" ) ]
193
+ public async Task FileFromDisk_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( string httpMethod , string expectedBody )
194
194
{
195
195
// Arrange
196
- var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Get , "http://localhost/DownloadFiles/DownloadFromDiskWithFileName" ) ;
196
+ var httpRequestMessage = new HttpRequestMessage ( new HttpMethod ( httpMethod ) , "http://localhost/DownloadFiles/DownloadFromDiskWithFileName" ) ;
197
197
httpRequestMessage . Headers . Range = new RangeHeaderValue ( 0 , 6 ) ;
198
198
199
199
// Act
@@ -204,7 +204,7 @@ public async Task FileFromDisk_ReturnsFileWithFileName_RangeProcessingNotEnabled
204
204
Assert . NotNull ( response . Content . Headers . ContentType ) ;
205
205
Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
206
206
var body = await response . Content . ReadAsStringAsync ( ) ;
207
- Assert . Equal ( "This is a sample text file" , body ) ;
207
+ Assert . Equal ( expectedBody , body ) ;
208
208
}
209
209
210
210
[ Fact ]
@@ -246,6 +246,41 @@ public async Task FileFromDisk_ReturnsFileWithFileName_IfRangeHeaderInvalid_Rang
246
246
Assert . Equal ( "This is a sample text file" , body ) ;
247
247
}
248
248
249
+ [ Theory ]
250
+ [ InlineData ( "" , HttpStatusCode . OK ) ]
251
+ [ InlineData ( "bytes = 0-6" , HttpStatusCode . PartialContent ) ]
252
+ [ InlineData ( "bytes = 17-25" , HttpStatusCode . PartialContent ) ]
253
+ [ InlineData ( "bytes = 0-50" , HttpStatusCode . PartialContent ) ]
254
+ [ InlineData ( "0-6" , HttpStatusCode . OK ) ]
255
+ [ InlineData ( "bytes = " , HttpStatusCode . OK ) ]
256
+ [ InlineData ( "bytes = 1-4, 5-11" , HttpStatusCode . OK ) ]
257
+ [ InlineData ( "bytes = 35-36" , HttpStatusCode . RequestedRangeNotSatisfiable ) ]
258
+ [ InlineData ( "bytes = -0" , HttpStatusCode . RequestedRangeNotSatisfiable ) ]
259
+ public async Task FileFromDisk_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest_WithLastModifiedAndEtag ( string rangeString , HttpStatusCode httpStatusCode )
260
+ {
261
+ // Arrange
262
+ var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Head , "http://localhost/DownloadFiles/DownloadFromDiskWithFileName_WithLastModifiedAndEtag" ) ;
263
+ httpRequestMessage . Headers . TryAddWithoutValidation ( "Range" , rangeString ) ;
264
+ httpRequestMessage . Headers . IfRange = new RangeConditionHeaderValue ( new EntityTagHeaderValue ( "\" Etag\" " ) ) ;
265
+
266
+ // Act
267
+ var response = await Client . SendAsync ( httpRequestMessage ) ;
268
+
269
+ // Assert
270
+ Assert . Equal ( httpStatusCode , response . StatusCode ) ;
271
+
272
+ Assert . NotNull ( response . Content . Headers . ContentType ) ;
273
+ Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
274
+
275
+ var body = await response . Content . ReadAsStringAsync ( ) ;
276
+ Assert . NotNull ( body ) ;
277
+ Assert . Equal ( string . Empty , body ) ;
278
+
279
+ var contentDisposition = response . Content . Headers . ContentDisposition . ToString ( ) ;
280
+ Assert . NotNull ( contentDisposition ) ;
281
+ Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
282
+ }
283
+
249
284
[ Fact ]
250
285
public async Task FileFromStream_ReturnsFile ( )
251
286
{
@@ -347,11 +382,13 @@ public async Task FileFromStream_ReturnsFileWithFileName()
347
382
Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
348
383
}
349
384
350
- [ Fact ]
351
- public async Task FileFromStream_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( )
385
+ [ Theory ]
386
+ [ InlineData ( "GET" , "This is sample text from a stream" ) ]
387
+ [ InlineData ( "HEAD" , "" ) ]
388
+ public async Task FileFromStream_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( string httpMethod , string expectedBody )
352
389
{
353
390
// Arrange
354
- var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Get , "http://localhost/DownloadFiles/DownloadFromStreamWithFileName" ) ;
391
+ var httpRequestMessage = new HttpRequestMessage ( new HttpMethod ( httpMethod ) , "http://localhost/DownloadFiles/DownloadFromStreamWithFileName" ) ;
355
392
httpRequestMessage . Headers . Range = new RangeHeaderValue ( 0 , 6 ) ;
356
393
357
394
// Act
@@ -362,7 +399,7 @@ public async Task FileFromStream_ReturnsFileWithFileName_RangeProcessingNotEnabl
362
399
Assert . NotNull ( response . Content . Headers . ContentType ) ;
363
400
Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
364
401
var body = await response . Content . ReadAsStringAsync ( ) ;
365
- Assert . Equal ( "This is sample text from a stream" , body ) ;
402
+ Assert . Equal ( expectedBody , body ) ;
366
403
}
367
404
368
405
[ Fact ]
@@ -402,6 +439,41 @@ public async Task FileFromStream_ReturnsFileWithFileName_IfRangeHeaderInvalid_Ra
402
439
Assert . Equal ( "This is sample text from a stream" , body ) ;
403
440
}
404
441
442
+ [ Theory ]
443
+ [ InlineData ( "" , HttpStatusCode . OK ) ]
444
+ [ InlineData ( "bytes = 0-6" , HttpStatusCode . PartialContent ) ]
445
+ [ InlineData ( "bytes = 17-25" , HttpStatusCode . PartialContent ) ]
446
+ [ InlineData ( "bytes = 0-50" , HttpStatusCode . PartialContent ) ]
447
+ [ InlineData ( "0-6" , HttpStatusCode . OK ) ]
448
+ [ InlineData ( "bytes = " , HttpStatusCode . OK ) ]
449
+ [ InlineData ( "bytes = 1-4, 5-11" , HttpStatusCode . OK ) ]
450
+ [ InlineData ( "bytes = 35-36" , HttpStatusCode . RequestedRangeNotSatisfiable ) ]
451
+ [ InlineData ( "bytes = -0" , HttpStatusCode . RequestedRangeNotSatisfiable ) ]
452
+ public async Task FileFromStream_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest ( string rangeString , HttpStatusCode httpStatusCode )
453
+ {
454
+ // Arrange
455
+ var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Head , "http://localhost/DownloadFiles/DownloadFromStreamWithFileName_WithEtag" ) ;
456
+ httpRequestMessage . Headers . TryAddWithoutValidation ( "Range" , rangeString ) ;
457
+ httpRequestMessage . Headers . IfRange = new RangeConditionHeaderValue ( new EntityTagHeaderValue ( "\" Etag\" " ) ) ;
458
+
459
+ // Act
460
+ var response = await Client . SendAsync ( httpRequestMessage ) ;
461
+
462
+ // Assert
463
+ Assert . Equal ( httpStatusCode , response . StatusCode ) ;
464
+
465
+ Assert . NotNull ( response . Content . Headers . ContentType ) ;
466
+ Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
467
+
468
+ var body = await response . Content . ReadAsStringAsync ( ) ;
469
+ Assert . NotNull ( body ) ;
470
+ Assert . Equal ( string . Empty , body ) ;
471
+
472
+ var contentDisposition = response . Content . Headers . ContentDisposition . ToString ( ) ;
473
+ Assert . NotNull ( contentDisposition ) ;
474
+ Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
475
+ }
476
+
405
477
[ Fact ]
406
478
public async Task FileFromBinaryData_ReturnsFile ( )
407
479
{
@@ -506,11 +578,13 @@ public async Task FileFromBinaryData_ReturnsFileWithFileName()
506
578
Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
507
579
}
508
580
509
- [ Fact ]
510
- public async Task FileFromBinaryData_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( )
581
+ [ Theory ]
582
+ [ InlineData ( "GET" , "This is a sample text from a binary array" ) ]
583
+ [ InlineData ( "HEAD" , "" ) ]
584
+ public async Task FileFromBinaryData_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( string httpMethod , string expectedBody )
511
585
{
512
586
// Arrange
513
- var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Get , "http://localhost/DownloadFiles/DownloadFromBinaryDataWithFileName" ) ;
587
+ var httpRequestMessage = new HttpRequestMessage ( new HttpMethod ( httpMethod ) , "http://localhost/DownloadFiles/DownloadFromBinaryDataWithFileName" ) ;
514
588
httpRequestMessage . Headers . Range = new RangeHeaderValue ( 0 , 6 ) ;
515
589
516
590
// Act
@@ -521,7 +595,7 @@ public async Task FileFromBinaryData_ReturnsFileWithFileName_RangeProcessingNotE
521
595
Assert . NotNull ( response . Content . Headers . ContentType ) ;
522
596
Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
523
597
var body = await response . Content . ReadAsStringAsync ( ) ;
524
- Assert . Equal ( "This is a sample text from a binary array" , body ) ;
598
+ Assert . Equal ( expectedBody , body ) ;
525
599
}
526
600
527
601
[ Fact ]
@@ -563,6 +637,41 @@ public async Task FileFromBinaryData_ReturnsFileWithFileName_IfRangeHeaderInvali
563
637
Assert . Equal ( "This is a sample text from a binary array" , body ) ;
564
638
}
565
639
640
+ [ Theory ]
641
+ [ InlineData ( "" , HttpStatusCode . OK ) ]
642
+ [ InlineData ( "bytes = 0-6" , HttpStatusCode . PartialContent ) ]
643
+ [ InlineData ( "bytes = 17-25" , HttpStatusCode . PartialContent ) ]
644
+ [ InlineData ( "bytes = 0-50" , HttpStatusCode . PartialContent ) ]
645
+ [ InlineData ( "0-6" , HttpStatusCode . OK ) ]
646
+ [ InlineData ( "bytes = " , HttpStatusCode . OK ) ]
647
+ [ InlineData ( "bytes = 1-4, 5-11" , HttpStatusCode . OK ) ]
648
+ [ InlineData ( "bytes = 45-46" , HttpStatusCode . RequestedRangeNotSatisfiable ) ]
649
+ [ InlineData ( "bytes = -0" , HttpStatusCode . RequestedRangeNotSatisfiable ) ]
650
+ public async Task FileFromBinaryData_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest ( string rangeString , HttpStatusCode httpStatusCode )
651
+ {
652
+ // Arrange
653
+ var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Head , "http://localhost/DownloadFiles/DownloadFromBinaryDataWithFileName_WithEtag" ) ;
654
+ httpRequestMessage . Headers . TryAddWithoutValidation ( "Range" , rangeString ) ;
655
+ httpRequestMessage . Headers . IfRange = new RangeConditionHeaderValue ( new EntityTagHeaderValue ( "\" Etag\" " ) ) ;
656
+
657
+ // Act
658
+ var response = await Client . SendAsync ( httpRequestMessage ) ;
659
+
660
+ // Assert
661
+ Assert . Equal ( httpStatusCode , response . StatusCode ) ;
662
+
663
+ Assert . NotNull ( response . Content . Headers . ContentType ) ;
664
+ Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
665
+
666
+ var body = await response . Content . ReadAsStringAsync ( ) ;
667
+ Assert . NotNull ( body ) ;
668
+ Assert . Equal ( string . Empty , body ) ;
669
+
670
+ var contentDisposition = response . Content . Headers . ContentDisposition . ToString ( ) ;
671
+ Assert . NotNull ( contentDisposition ) ;
672
+ Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
673
+ }
674
+
566
675
[ Fact ]
567
676
public async Task FileFromEmbeddedResources_ReturnsFileWithFileName ( )
568
677
{
@@ -612,11 +721,13 @@ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_RangeRequest
612
721
Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
613
722
}
614
723
615
- [ Fact ]
616
- public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( )
724
+ [ Theory ]
725
+ [ InlineData ( "GET" , "Sample text file as embedded resource." ) ]
726
+ [ InlineData ( "HEAD" , "" ) ]
727
+ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( string httpMethod , string expectedBody )
617
728
{
618
729
// Arrange
619
- var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Get , "http://localhost/EmbeddedFiles/DownloadFileWithFileName_RangeProcessingNotEnabled" ) ;
730
+ var httpRequestMessage = new HttpRequestMessage ( new HttpMethod ( httpMethod ) , "http://localhost/EmbeddedFiles/DownloadFileWithFileName_RangeProcessingNotEnabled" ) ;
620
731
httpRequestMessage . Headers . Range = new RangeHeaderValue ( 0 , 6 ) ;
621
732
622
733
// Act
@@ -627,7 +738,7 @@ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_RangeProcess
627
738
Assert . NotNull ( response . Content . Headers . ContentType ) ;
628
739
Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
629
740
var body = await response . Content . ReadAsStringAsync ( ) ;
630
- Assert . Equal ( "Sample text file as embedded resource." , body ) ;
741
+ Assert . Equal ( expectedBody , body ) ;
631
742
}
632
743
633
744
[ Fact ]
@@ -721,5 +832,40 @@ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_RangeRequest
721
832
Assert . NotNull ( contentDisposition ) ;
722
833
Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
723
834
}
835
+
836
+ [ Theory ]
837
+ [ InlineData ( "" , HttpStatusCode . OK ) ]
838
+ [ InlineData ( "bytes = 0-6" , HttpStatusCode . PartialContent ) ]
839
+ [ InlineData ( "bytes = 17-25" , HttpStatusCode . PartialContent ) ]
840
+ [ InlineData ( "bytes = 0-50" , HttpStatusCode . PartialContent ) ]
841
+ [ InlineData ( "0-6" , HttpStatusCode . OK ) ]
842
+ [ InlineData ( "bytes = " , HttpStatusCode . OK ) ]
843
+ [ InlineData ( "bytes = 1-4, 5-11" , HttpStatusCode . OK ) ]
844
+ [ InlineData ( "bytes = 45-46" , HttpStatusCode . RequestedRangeNotSatisfiable ) ]
845
+ [ InlineData ( "bytes = -0" , HttpStatusCode . RequestedRangeNotSatisfiable ) ]
846
+ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest ( string rangeString , HttpStatusCode httpStatusCode )
847
+ {
848
+ // Arrange
849
+ var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Head , "http://localhost/EmbeddedFiles/DownloadFileWithFileName" ) ;
850
+ httpRequestMessage . Headers . TryAddWithoutValidation ( "Range" , rangeString ) ;
851
+ httpRequestMessage . Headers . IfRange = new RangeConditionHeaderValue ( new EntityTagHeaderValue ( "\" Etag\" " ) ) ;
852
+
853
+ // Act
854
+ var response = await Client . SendAsync ( httpRequestMessage ) ;
855
+
856
+ // Assert
857
+ Assert . Equal ( httpStatusCode , response . StatusCode ) ;
858
+
859
+ Assert . NotNull ( response . Content . Headers . ContentType ) ;
860
+ Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
861
+
862
+ var body = await response . Content . ReadAsStringAsync ( ) ;
863
+ Assert . NotNull ( body ) ;
864
+ Assert . Equal ( string . Empty , body ) ;
865
+
866
+ var contentDisposition = response . Content . Headers . ContentDisposition . ToString ( ) ;
867
+ Assert . NotNull ( contentDisposition ) ;
868
+ Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
869
+ }
724
870
}
725
871
}
0 commit comments