@@ -421,21 +421,61 @@ await connection.Receive(
421
421
}
422
422
}
423
423
424
- [ Theory ]
425
- [ InlineData ( false ) ]
426
- [ InlineData ( true ) ]
427
- public async Task ChunksWithGetMemoryBeforeFirstFlushStillFlushes ( bool start )
424
+ [ Fact ]
425
+ public async Task ChunksWithGetMemoryAfterStartAsyncBeforeFirstFlushStillFlushes ( )
428
426
{
429
427
var testContext = new TestServiceContext ( LoggerFactory ) ;
430
428
431
429
using ( var server = new TestServer ( async httpContext =>
432
430
{
433
431
var response = httpContext . Response ;
434
432
435
- if ( start )
433
+ await response . StartAsync ( ) ;
434
+ var memory = response . BodyPipe . GetMemory ( ) ;
435
+ var fisrtPartOfResponse = Encoding . ASCII . GetBytes ( "Hello " ) ;
436
+ fisrtPartOfResponse . CopyTo ( memory ) ;
437
+ response . BodyPipe . Advance ( 6 ) ;
438
+
439
+ memory = response . BodyPipe . GetMemory ( ) ;
440
+ var secondPartOfResponse = Encoding . ASCII . GetBytes ( "World!" ) ;
441
+ secondPartOfResponse . CopyTo ( memory ) ;
442
+ response . BodyPipe . Advance ( 6 ) ;
443
+
444
+ await response . BodyPipe . FlushAsync ( ) ;
445
+ } , testContext ) )
446
+ {
447
+ using ( var connection = server . CreateConnection ( ) )
436
448
{
437
- await response . StartAsync ( ) ;
449
+ await connection . Send (
450
+ "GET / HTTP/1.1" ,
451
+ "Host: " ,
452
+ "" ,
453
+ "" ) ;
454
+ await connection . Receive (
455
+ "HTTP/1.1 200 OK" ,
456
+ $ "Date: { testContext . DateHeaderValue } ",
457
+ "Transfer-Encoding: chunked" ,
458
+ "" ,
459
+ "c" ,
460
+ "Hello World!" ,
461
+ "0" ,
462
+ "" ,
463
+ "" ) ;
438
464
}
465
+
466
+ await server . StopAsync ( ) ;
467
+ }
468
+ }
469
+
470
+ [ Fact ]
471
+ public async Task ChunksWithGetMemoryBeforeFirstFlushStillFlushes ( )
472
+ {
473
+ var testContext = new TestServiceContext ( LoggerFactory ) ;
474
+
475
+ using ( var server = new TestServer ( async httpContext =>
476
+ {
477
+ var response = httpContext . Response ;
478
+
439
479
var memory = response . BodyPipe . GetMemory ( ) ;
440
480
var fisrtPartOfResponse = Encoding . ASCII . GetBytes ( "Hello " ) ;
441
481
fisrtPartOfResponse . CopyTo ( memory ) ;
@@ -472,10 +512,8 @@ await connection.Receive(
472
512
}
473
513
}
474
514
475
- [ Theory ]
476
- [ InlineData ( false ) ]
477
- [ InlineData ( true ) ]
478
- public async Task ChunksWithGetMemoryLargeWriteBeforeFirstFlush ( bool start )
515
+ [ Fact ]
516
+ public async Task ChunksWithGetMemoryLargeWriteBeforeFirstFlush ( )
479
517
{
480
518
var length = new IntAsRef ( ) ;
481
519
var semaphore = new SemaphoreSlim ( initialCount : 0 ) ;
@@ -484,10 +522,6 @@ public async Task ChunksWithGetMemoryLargeWriteBeforeFirstFlush(bool start)
484
522
using ( var server = new TestServer ( async httpContext =>
485
523
{
486
524
var response = httpContext . Response ;
487
- if ( start )
488
- {
489
- await response . StartAsync ( ) ;
490
- }
491
525
492
526
var memory = response . BodyPipe . GetMemory ( ) ;
493
527
length . Value = memory . Length ;
@@ -534,10 +568,8 @@ await connection.Receive(
534
568
}
535
569
}
536
570
537
- [ Theory ]
538
- [ InlineData ( false ) ]
539
- [ InlineData ( true ) ]
540
- public async Task ChunksWithGetMemoryWithInitialFlushWorks ( bool start )
571
+ [ Fact ]
572
+ public async Task ChunksWithGetMemoryAndStartAsyncWithInitialFlushWorks ( )
541
573
{
542
574
var length = new IntAsRef ( ) ;
543
575
var semaphore = new SemaphoreSlim ( initialCount : 0 ) ;
@@ -547,10 +579,7 @@ public async Task ChunksWithGetMemoryWithInitialFlushWorks(bool start)
547
579
{
548
580
var response = httpContext . Response ;
549
581
550
- if ( start )
551
- {
552
- await response . StartAsync ( ) ;
553
- }
582
+ await response . StartAsync ( ) ;
554
583
555
584
await response . BodyPipe . FlushAsync ( ) ;
556
585
@@ -599,22 +628,76 @@ await connection.Receive(
599
628
}
600
629
}
601
630
602
- [ Theory ]
603
- [ InlineData ( false ) ]
604
- [ InlineData ( true ) ]
605
- public async Task ChunkGetMemoryMultipleAdvance ( bool start )
631
+ [ Fact ]
632
+ public async Task ChunksWithGetMemoryBeforeFlushEdgeCase ( )
606
633
{
634
+ var length = new IntAsRef ( ) ;
635
+ var semaphore = new SemaphoreSlim ( initialCount : 0 ) ;
607
636
var testContext = new TestServiceContext ( LoggerFactory ) ;
608
637
609
638
using ( var server = new TestServer ( async httpContext =>
610
639
{
611
640
var response = httpContext . Response ;
612
641
613
- if ( start )
642
+ await response . StartAsync ( ) ;
643
+
644
+ var memory = response . BodyPipe . GetMemory ( ) ;
645
+ length . Value = memory . Length - 1 ;
646
+ semaphore . Release ( ) ;
647
+
648
+ var fisrtPartOfResponse = Encoding . ASCII . GetBytes ( new string ( 'a' , length . Value ) ) ;
649
+ fisrtPartOfResponse . CopyTo ( memory ) ;
650
+ response . BodyPipe . Advance ( length . Value ) ;
651
+
652
+ var secondMemory = response . BodyPipe . GetMemory ( 6 ) ;
653
+
654
+ var secondPartOfResponse = Encoding . ASCII . GetBytes ( "World!" ) ;
655
+ secondPartOfResponse . CopyTo ( secondMemory ) ;
656
+ response . BodyPipe . Advance ( 6 ) ;
657
+
658
+ await response . BodyPipe . FlushAsync ( ) ;
659
+ } , testContext ) )
660
+ {
661
+ using ( var connection = server . CreateConnection ( ) )
614
662
{
615
- await response . StartAsync ( ) ;
663
+ await connection . Send (
664
+ "GET / HTTP/1.1" ,
665
+ "Host: " ,
666
+ "" ,
667
+ "" ) ;
668
+
669
+ // Wait for length to be set
670
+ await semaphore . WaitAsync ( ) ;
671
+
672
+ await connection . Receive (
673
+ "HTTP/1.1 200 OK" ,
674
+ $ "Date: { testContext . DateHeaderValue } ",
675
+ "Transfer-Encoding: chunked" ,
676
+ "" ,
677
+ length . Value . ToString ( "x" ) ,
678
+ new string ( 'a' , length . Value ) ,
679
+ "6" ,
680
+ "World!" ,
681
+ "0" ,
682
+ "" ,
683
+ "" ) ;
616
684
}
617
685
686
+ await server . StopAsync ( ) ;
687
+ }
688
+ }
689
+
690
+ [ Fact ]
691
+ public async Task ChunkGetMemoryMultipleAdvance ( )
692
+ {
693
+ var testContext = new TestServiceContext ( LoggerFactory ) ;
694
+
695
+ using ( var server = new TestServer ( async httpContext =>
696
+ {
697
+ var response = httpContext . Response ;
698
+
699
+ await response . StartAsync ( ) ;
700
+
618
701
var memory = response . BodyPipe . GetMemory ( 4096 ) ;
619
702
var fisrtPartOfResponse = Encoding . ASCII . GetBytes ( "Hello " ) ;
620
703
fisrtPartOfResponse . CopyTo ( memory ) ;
@@ -648,20 +731,16 @@ await connection.Receive(
648
731
}
649
732
}
650
733
651
- [ Theory ]
652
- [ InlineData ( false ) ]
653
- [ InlineData ( true ) ]
654
- public async Task ChunkGetSpanMultipleAdvance ( bool start )
734
+ [ Fact ]
735
+ public async Task ChunkGetSpanMultipleAdvance ( )
655
736
{
656
737
var testContext = new TestServiceContext ( LoggerFactory ) ;
657
738
658
739
using ( var server = new TestServer ( async httpContext =>
659
740
{
660
741
var response = httpContext . Response ;
661
- if ( start )
662
- {
663
- await response . StartAsync ( ) ;
664
- }
742
+ await response . StartAsync ( ) ;
743
+
665
744
// To avoid using span in an async method
666
745
void NonAsyncMethod ( )
667
746
{
@@ -863,27 +942,21 @@ await connection.Receive(
863
942
}
864
943
865
944
[ Theory ]
866
- [ InlineData ( 15 , false ) ]
867
- [ InlineData ( 255 , false ) ]
868
- [ InlineData ( 15 , true ) ]
869
- [ InlineData ( 255 , true ) ]
870
- public async Task ChunkGetMemoryWithSmallerSizesWork ( int writeSize , bool start )
945
+ [ InlineData ( 15 ) ]
946
+ [ InlineData ( 255 ) ]
947
+ public async Task ChunkGetMemoryWithoutStartWithSmallerSizesWork ( int writeSize )
871
948
{
872
949
var testContext = new TestServiceContext ( LoggerFactory ) ;
873
950
874
951
using ( var server = new TestServer ( async httpContext =>
875
952
{
876
953
var response = httpContext . Response ;
877
954
878
- if ( start )
879
- {
880
- await response . StartAsync ( ) ;
881
- }
882
-
883
955
var memory = response . BodyPipe . GetMemory ( 4096 ) ;
884
956
var fisrtPartOfResponse = Encoding . ASCII . GetBytes ( new string ( 'a' , writeSize ) ) ;
885
957
fisrtPartOfResponse . CopyTo ( memory ) ;
886
958
response . BodyPipe . Advance ( writeSize ) ;
959
+ await response . BodyPipe . FlushAsync ( ) ;
887
960
} , testContext ) )
888
961
{
889
962
using ( var connection = server . CreateConnection ( ) )
@@ -910,19 +983,53 @@ await connection.Receive(
910
983
}
911
984
912
985
[ Theory ]
913
- [ InlineData ( false ) ]
914
- [ InlineData ( true ) ]
915
- public async Task ChunkedWithBothPipeAndStreamWorks ( bool start )
986
+ [ InlineData ( 15 ) ]
987
+ [ InlineData ( 255 ) ]
988
+ public async Task ChunkGetMemoryWithStartWithSmallerSizesWork ( int writeSize )
916
989
{
990
+ var testContext = new TestServiceContext ( LoggerFactory ) ;
991
+
917
992
using ( var server = new TestServer ( async httpContext =>
918
993
{
919
994
var response = httpContext . Response ;
920
995
921
- if ( start )
996
+ var memory = response . BodyPipe . GetMemory ( 4096 ) ;
997
+ var fisrtPartOfResponse = Encoding . ASCII . GetBytes ( new string ( 'a' , writeSize ) ) ;
998
+ fisrtPartOfResponse . CopyTo ( memory ) ;
999
+ response . BodyPipe . Advance ( writeSize ) ;
1000
+ await response . BodyPipe . FlushAsync ( ) ;
1001
+ } , testContext ) )
1002
+ {
1003
+ using ( var connection = server . CreateConnection ( ) )
922
1004
{
923
- await response . StartAsync ( ) ;
1005
+ await connection . Send (
1006
+ "GET / HTTP/1.1" ,
1007
+ "Host: " ,
1008
+ "" ,
1009
+ "" ) ;
1010
+ await connection . Receive (
1011
+ "HTTP/1.1 200 OK" ,
1012
+ $ "Date: { testContext . DateHeaderValue } ",
1013
+ "Transfer-Encoding: chunked" ,
1014
+ "" ,
1015
+ writeSize . ToString ( "X" ) . ToLower ( ) ,
1016
+ new string ( 'a' , writeSize ) ,
1017
+ "0" ,
1018
+ "" ,
1019
+ "" ) ;
924
1020
}
925
1021
1022
+ await server . StopAsync ( ) ;
1023
+ }
1024
+ }
1025
+
1026
+ [ Fact ]
1027
+ public async Task ChunkedWithBothPipeAndStreamWorks ( )
1028
+ {
1029
+ using ( var server = new TestServer ( async httpContext =>
1030
+ {
1031
+ var response = httpContext . Response ;
1032
+
926
1033
var memory = response . BodyPipe . GetMemory ( 4096 ) ;
927
1034
var fisrtPartOfResponse = Encoding . ASCII . GetBytes ( "hello," ) ;
928
1035
fisrtPartOfResponse . CopyTo ( memory ) ;
0 commit comments