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,44 @@ 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 , 26 ) ]
251
+ [ InlineData ( "bytes = 0-6" , HttpStatusCode . PartialContent , 7 ) ]
252
+ [ InlineData ( "bytes = 17-25" , HttpStatusCode . PartialContent , 9 ) ]
253
+ [ InlineData ( "bytes = 0-50" , HttpStatusCode . PartialContent , 26 ) ]
254
+ [ InlineData ( "0-6" , HttpStatusCode . OK , 26 ) ]
255
+ [ InlineData ( "bytes = " , HttpStatusCode . OK , 26 ) ]
256
+ [ InlineData ( "bytes = 1-4, 5-11" , HttpStatusCode . OK , 26 ) ]
257
+ [ InlineData ( "bytes = 35-36" , HttpStatusCode . RequestedRangeNotSatisfiable , 26 ) ]
258
+ [ InlineData ( "bytes = -0" , HttpStatusCode . RequestedRangeNotSatisfiable , 26 ) ]
259
+ public async Task FileFromDisk_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest_WithLastModifiedAndEtag ( string rangeString , HttpStatusCode httpStatusCode , int expectedContentLength )
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 contentLength = response . Content . Headers . ContentLength ;
280
+ Assert . Equal ( expectedContentLength , contentLength ) ;
281
+
282
+ var contentDisposition = response . Content . Headers . ContentDisposition . ToString ( ) ;
283
+ Assert . NotNull ( contentDisposition ) ;
284
+ Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
285
+ }
286
+
249
287
[ Fact ]
250
288
public async Task FileFromStream_ReturnsFile ( )
251
289
{
@@ -347,11 +385,13 @@ public async Task FileFromStream_ReturnsFileWithFileName()
347
385
Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
348
386
}
349
387
350
- [ Fact ]
351
- public async Task FileFromStream_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( )
388
+ [ Theory ]
389
+ [ InlineData ( "GET" , "This is sample text from a stream" ) ]
390
+ [ InlineData ( "HEAD" , "" ) ]
391
+ public async Task FileFromStream_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( string httpMethod , string expectedBody )
352
392
{
353
393
// Arrange
354
- var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Get , "http://localhost/DownloadFiles/DownloadFromStreamWithFileName" ) ;
394
+ var httpRequestMessage = new HttpRequestMessage ( new HttpMethod ( httpMethod ) , "http://localhost/DownloadFiles/DownloadFromStreamWithFileName" ) ;
355
395
httpRequestMessage . Headers . Range = new RangeHeaderValue ( 0 , 6 ) ;
356
396
357
397
// Act
@@ -362,7 +402,7 @@ public async Task FileFromStream_ReturnsFileWithFileName_RangeProcessingNotEnabl
362
402
Assert . NotNull ( response . Content . Headers . ContentType ) ;
363
403
Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
364
404
var body = await response . Content . ReadAsStringAsync ( ) ;
365
- Assert . Equal ( "This is sample text from a stream" , body ) ;
405
+ Assert . Equal ( expectedBody , body ) ;
366
406
}
367
407
368
408
[ Fact ]
@@ -402,6 +442,44 @@ public async Task FileFromStream_ReturnsFileWithFileName_IfRangeHeaderInvalid_Ra
402
442
Assert . Equal ( "This is sample text from a stream" , body ) ;
403
443
}
404
444
445
+ [ Theory ]
446
+ [ InlineData ( "" , HttpStatusCode . OK , 33 ) ]
447
+ [ InlineData ( "bytes = 0-6" , HttpStatusCode . PartialContent , 7 ) ]
448
+ [ InlineData ( "bytes = 17-25" , HttpStatusCode . PartialContent , 9 ) ]
449
+ [ InlineData ( "bytes = 0-50" , HttpStatusCode . PartialContent , 33 ) ]
450
+ [ InlineData ( "0-6" , HttpStatusCode . OK , 33 ) ]
451
+ [ InlineData ( "bytes = " , HttpStatusCode . OK , 33 ) ]
452
+ [ InlineData ( "bytes = 1-4, 5-11" , HttpStatusCode . OK , 33 ) ]
453
+ [ InlineData ( "bytes = 35-36" , HttpStatusCode . RequestedRangeNotSatisfiable , 33 ) ]
454
+ [ InlineData ( "bytes = -0" , HttpStatusCode . RequestedRangeNotSatisfiable , 33 ) ]
455
+ public async Task FileFromStream_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest ( string rangeString , HttpStatusCode httpStatusCode , int expectedContentLength )
456
+ {
457
+ // Arrange
458
+ var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Head , "http://localhost/DownloadFiles/DownloadFromStreamWithFileName_WithEtag" ) ;
459
+ httpRequestMessage . Headers . TryAddWithoutValidation ( "Range" , rangeString ) ;
460
+ httpRequestMessage . Headers . IfRange = new RangeConditionHeaderValue ( new EntityTagHeaderValue ( "\" Etag\" " ) ) ;
461
+
462
+ // Act
463
+ var response = await Client . SendAsync ( httpRequestMessage ) ;
464
+
465
+ // Assert
466
+ Assert . Equal ( httpStatusCode , response . StatusCode ) ;
467
+
468
+ Assert . NotNull ( response . Content . Headers . ContentType ) ;
469
+ Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
470
+
471
+ var body = await response . Content . ReadAsStringAsync ( ) ;
472
+ Assert . NotNull ( body ) ;
473
+ Assert . Equal ( string . Empty , body ) ;
474
+
475
+ var contentLength = response . Content . Headers . ContentLength ;
476
+ Assert . Equal ( expectedContentLength , contentLength ) ;
477
+
478
+ var contentDisposition = response . Content . Headers . ContentDisposition . ToString ( ) ;
479
+ Assert . NotNull ( contentDisposition ) ;
480
+ Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
481
+ }
482
+
405
483
[ Fact ]
406
484
public async Task FileFromBinaryData_ReturnsFile ( )
407
485
{
@@ -506,11 +584,13 @@ public async Task FileFromBinaryData_ReturnsFileWithFileName()
506
584
Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
507
585
}
508
586
509
- [ Fact ]
510
- public async Task FileFromBinaryData_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( )
587
+ [ Theory ]
588
+ [ InlineData ( "GET" , "This is a sample text from a binary array" ) ]
589
+ [ InlineData ( "HEAD" , "" ) ]
590
+ public async Task FileFromBinaryData_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( string httpMethod , string expectedBody )
511
591
{
512
592
// Arrange
513
- var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Get , "http://localhost/DownloadFiles/DownloadFromBinaryDataWithFileName" ) ;
593
+ var httpRequestMessage = new HttpRequestMessage ( new HttpMethod ( httpMethod ) , "http://localhost/DownloadFiles/DownloadFromBinaryDataWithFileName" ) ;
514
594
httpRequestMessage . Headers . Range = new RangeHeaderValue ( 0 , 6 ) ;
515
595
516
596
// Act
@@ -521,7 +601,7 @@ public async Task FileFromBinaryData_ReturnsFileWithFileName_RangeProcessingNotE
521
601
Assert . NotNull ( response . Content . Headers . ContentType ) ;
522
602
Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
523
603
var body = await response . Content . ReadAsStringAsync ( ) ;
524
- Assert . Equal ( "This is a sample text from a binary array" , body ) ;
604
+ Assert . Equal ( expectedBody , body ) ;
525
605
}
526
606
527
607
[ Fact ]
@@ -563,6 +643,44 @@ public async Task FileFromBinaryData_ReturnsFileWithFileName_IfRangeHeaderInvali
563
643
Assert . Equal ( "This is a sample text from a binary array" , body ) ;
564
644
}
565
645
646
+ [ Theory ]
647
+ [ InlineData ( "" , HttpStatusCode . OK , 41 ) ]
648
+ [ InlineData ( "bytes = 0-6" , HttpStatusCode . PartialContent , 7 ) ]
649
+ [ InlineData ( "bytes = 17-25" , HttpStatusCode . PartialContent , 9 ) ]
650
+ [ InlineData ( "bytes = 0-50" , HttpStatusCode . PartialContent , 41 ) ]
651
+ [ InlineData ( "0-6" , HttpStatusCode . OK , 41 ) ]
652
+ [ InlineData ( "bytes = " , HttpStatusCode . OK , 41 ) ]
653
+ [ InlineData ( "bytes = 1-4, 5-11" , HttpStatusCode . OK , 41 ) ]
654
+ [ InlineData ( "bytes = 45-46" , HttpStatusCode . RequestedRangeNotSatisfiable , 41 ) ]
655
+ [ InlineData ( "bytes = -0" , HttpStatusCode . RequestedRangeNotSatisfiable , 41 ) ]
656
+ public async Task FileFromBinaryData_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest ( string rangeString , HttpStatusCode httpStatusCode , int expectedContentLength )
657
+ {
658
+ // Arrange
659
+ var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Head , "http://localhost/DownloadFiles/DownloadFromBinaryDataWithFileName_WithEtag" ) ;
660
+ httpRequestMessage . Headers . TryAddWithoutValidation ( "Range" , rangeString ) ;
661
+ httpRequestMessage . Headers . IfRange = new RangeConditionHeaderValue ( new EntityTagHeaderValue ( "\" Etag\" " ) ) ;
662
+
663
+ // Act
664
+ var response = await Client . SendAsync ( httpRequestMessage ) ;
665
+
666
+ // Assert
667
+ Assert . Equal ( httpStatusCode , response . StatusCode ) ;
668
+
669
+ Assert . NotNull ( response . Content . Headers . ContentType ) ;
670
+ Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
671
+
672
+ var body = await response . Content . ReadAsStringAsync ( ) ;
673
+ Assert . NotNull ( body ) ;
674
+ Assert . Equal ( string . Empty , body ) ;
675
+
676
+ var contentLength = response . Content . Headers . ContentLength ;
677
+ Assert . Equal ( expectedContentLength , contentLength ) ;
678
+
679
+ var contentDisposition = response . Content . Headers . ContentDisposition . ToString ( ) ;
680
+ Assert . NotNull ( contentDisposition ) ;
681
+ Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
682
+ }
683
+
566
684
[ Fact ]
567
685
public async Task FileFromEmbeddedResources_ReturnsFileWithFileName ( )
568
686
{
@@ -612,11 +730,13 @@ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_RangeRequest
612
730
Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
613
731
}
614
732
615
- [ Fact ]
616
- public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( )
733
+ [ Theory ]
734
+ [ InlineData ( "GET" , "Sample text file as embedded resource." ) ]
735
+ [ InlineData ( "HEAD" , "" ) ]
736
+ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_RangeProcessingNotEnabled_RangeRequestedIgnored ( string httpMethod , string expectedBody )
617
737
{
618
738
// Arrange
619
- var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Get , "http://localhost/EmbeddedFiles/DownloadFileWithFileName_RangeProcessingNotEnabled" ) ;
739
+ var httpRequestMessage = new HttpRequestMessage ( new HttpMethod ( httpMethod ) , "http://localhost/EmbeddedFiles/DownloadFileWithFileName_RangeProcessingNotEnabled" ) ;
620
740
httpRequestMessage . Headers . Range = new RangeHeaderValue ( 0 , 6 ) ;
621
741
622
742
// Act
@@ -627,7 +747,7 @@ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_RangeProcess
627
747
Assert . NotNull ( response . Content . Headers . ContentType ) ;
628
748
Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
629
749
var body = await response . Content . ReadAsStringAsync ( ) ;
630
- Assert . Equal ( "Sample text file as embedded resource." , body ) ;
750
+ Assert . Equal ( expectedBody , body ) ;
631
751
}
632
752
633
753
[ Fact ]
@@ -721,5 +841,43 @@ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_RangeRequest
721
841
Assert . NotNull ( contentDisposition ) ;
722
842
Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
723
843
}
844
+
845
+ [ Theory ]
846
+ [ InlineData ( "" , HttpStatusCode . OK , 38 ) ]
847
+ [ InlineData ( "bytes = 0-6" , HttpStatusCode . PartialContent , 7 ) ]
848
+ [ InlineData ( "bytes = 17-25" , HttpStatusCode . PartialContent , 9 ) ]
849
+ [ InlineData ( "bytes = 0-50" , HttpStatusCode . PartialContent , 38 ) ]
850
+ [ InlineData ( "0-6" , HttpStatusCode . OK , 38 ) ]
851
+ [ InlineData ( "bytes = " , HttpStatusCode . OK , 38 ) ]
852
+ [ InlineData ( "bytes = 1-4, 5-11" , HttpStatusCode . OK , 38 ) ]
853
+ [ InlineData ( "bytes = 45-46" , HttpStatusCode . RequestedRangeNotSatisfiable , 38 ) ]
854
+ [ InlineData ( "bytes = -0" , HttpStatusCode . RequestedRangeNotSatisfiable , 38 ) ]
855
+ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest ( string rangeString , HttpStatusCode httpStatusCode , int expectedContentLength )
856
+ {
857
+ // Arrange
858
+ var httpRequestMessage = new HttpRequestMessage ( HttpMethod . Head , "http://localhost/EmbeddedFiles/DownloadFileWithFileName" ) ;
859
+ httpRequestMessage . Headers . TryAddWithoutValidation ( "Range" , rangeString ) ;
860
+ httpRequestMessage . Headers . IfRange = new RangeConditionHeaderValue ( new EntityTagHeaderValue ( "\" Etag\" " ) ) ;
861
+
862
+ // Act
863
+ var response = await Client . SendAsync ( httpRequestMessage ) ;
864
+
865
+ // Assert
866
+ Assert . Equal ( httpStatusCode , response . StatusCode ) ;
867
+
868
+ Assert . NotNull ( response . Content . Headers . ContentType ) ;
869
+ Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
870
+
871
+ var body = await response . Content . ReadAsStringAsync ( ) ;
872
+ Assert . NotNull ( body ) ;
873
+ Assert . Equal ( string . Empty , body ) ;
874
+
875
+ var contentLength = response . Content . Headers . ContentLength ;
876
+ Assert . Equal ( expectedContentLength , contentLength ) ;
877
+
878
+ var contentDisposition = response . Content . Headers . ContentDisposition . ToString ( ) ;
879
+ Assert . NotNull ( contentDisposition ) ;
880
+ Assert . Equal ( "attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt" , contentDisposition ) ;
881
+ }
724
882
}
725
883
}
0 commit comments