@@ -532,21 +532,43 @@ Deno.test(
532
532
} ,
533
533
) ;
534
534
535
- Deno . test (
536
- { permissions : { net : true } } ,
537
- async function httpServerStreamResponse ( ) {
538
- const stream = new TransformStream ( ) ;
539
- const writer = stream . writable . getWriter ( ) ;
540
- writer . write ( new TextEncoder ( ) . encode ( "hello " ) ) ;
541
- writer . write ( new TextEncoder ( ) . encode ( "world" ) ) ;
542
- writer . close ( ) ;
535
+ function createStreamTest ( count : number , delay : number , action : string ) {
536
+ function doAction ( controller : ReadableStreamDefaultController , i : number ) {
537
+ if ( i == count ) {
538
+ if ( action == "Throw" ) {
539
+ controller . error ( new Error ( "Expected error!" ) ) ;
540
+ } else {
541
+ controller . close ( ) ;
542
+ }
543
+ } else {
544
+ controller . enqueue ( `a${ i } ` ) ;
543
545
544
- const listeningPromise = deferred ( ) ;
546
+ if ( delay == 0 ) {
547
+ doAction ( controller , i + 1 ) ;
548
+ } else {
549
+ setTimeout ( ( ) => doAction ( controller , i + 1 ) , delay ) ;
550
+ }
551
+ }
552
+ }
553
+
554
+ function makeStream ( count : number , delay : number ) : ReadableStream {
555
+ return new ReadableStream ( {
556
+ start ( controller ) {
557
+ if ( delay == 0 ) {
558
+ doAction ( controller , 0 ) ;
559
+ } else {
560
+ setTimeout ( ( ) => doAction ( controller , 0 ) , delay ) ;
561
+ }
562
+ } ,
563
+ } ) . pipeThrough ( new TextEncoderStream ( ) ) ;
564
+ }
565
+
566
+ Deno . test ( `httpServerStreamCount${ count } Delay${ delay } ${ action } ` , async ( ) => {
545
567
const ac = new AbortController ( ) ;
568
+ const listeningPromise = deferred ( ) ;
546
569
const server = Deno . serve ( {
547
- handler : ( request ) => {
548
- assert ( ! request . body ) ;
549
- return new Response ( stream . readable ) ;
570
+ handler : async ( request ) => {
571
+ return new Response ( makeStream ( count , delay ) ) ;
550
572
} ,
551
573
port : 4501 ,
552
574
signal : ac . signal ,
@@ -556,12 +578,34 @@ Deno.test(
556
578
557
579
await listeningPromise ;
558
580
const resp = await fetch ( "http://127.0.0.1:4501/" ) ;
559
- const respBody = await resp . text ( ) ;
560
- assertEquals ( "hello world" , respBody ) ;
581
+ const text = await resp . text ( ) ;
582
+
561
583
ac . abort ( ) ;
562
584
await server ;
563
- } ,
564
- ) ;
585
+ let expected = "" ;
586
+ if ( action == "Throw" && count < 2 && delay < 1000 ) {
587
+ // NOTE: This is specific to the current implementation. In some cases where a stream errors, we
588
+ // don't send the first packet.
589
+ expected = "" ;
590
+ } else {
591
+ for ( let i = 0 ; i < count ; i ++ ) {
592
+ expected += `a${ i } ` ;
593
+ }
594
+ }
595
+
596
+ assertEquals ( text , expected ) ;
597
+ } ) ;
598
+ }
599
+
600
+ for ( let count of [ 0 , 1 , 2 , 3 ] ) {
601
+ for ( let delay of [ 0 , 1 , 1000 ] ) {
602
+ // Creating a stream that errors in start will throw
603
+ if ( delay > 0 ) {
604
+ createStreamTest ( count , delay , "Throw" ) ;
605
+ }
606
+ createStreamTest ( count , delay , "Close" ) ;
607
+ }
608
+ }
565
609
566
610
Deno . test (
567
611
{ permissions : { net : true } } ,
@@ -1690,78 +1734,6 @@ createServerLengthTest("autoResponseWithUnknownLengthEmpty", {
1690
1734
expects_con_len : false ,
1691
1735
} ) ;
1692
1736
1693
- Deno . test (
1694
- { permissions : { net : true } } ,
1695
- async function httpServerGetChunkedResponseWithKa ( ) {
1696
- const promises = [ deferred ( ) , deferred ( ) ] ;
1697
- let reqCount = 0 ;
1698
- const listeningPromise = deferred ( ) ;
1699
- const ac = new AbortController ( ) ;
1700
-
1701
- const server = Deno . serve ( {
1702
- handler : async ( request ) => {
1703
- assertEquals ( request . method , "GET" ) ;
1704
- promises [ reqCount ] . resolve ( ) ;
1705
- reqCount ++ ;
1706
- return new Response ( reqCount <= 1 ? stream ( "foo bar baz" ) : "zar quux" ) ;
1707
- } ,
1708
- port : 4503 ,
1709
- signal : ac . signal ,
1710
- onListen : onListen ( listeningPromise ) ,
1711
- onError : createOnErrorCb ( ac ) ,
1712
- } ) ;
1713
-
1714
- await listeningPromise ;
1715
- const conn = await Deno . connect ( { port : 4503 } ) ;
1716
- const encoder = new TextEncoder ( ) ;
1717
- {
1718
- const body =
1719
- `GET / HTTP/1.1\r\nHost: example.domain\r\nConnection: keep-alive\r\n\r\n` ;
1720
- const writeResult = await conn . write ( encoder . encode ( body ) ) ;
1721
- assertEquals ( body . length , writeResult ) ;
1722
- await promises [ 0 ] ;
1723
- }
1724
-
1725
- const decoder = new TextDecoder ( ) ;
1726
- {
1727
- let msg = "" ;
1728
- while ( true ) {
1729
- try {
1730
- const buf = new Uint8Array ( 1024 ) ;
1731
- const readResult = await conn . read ( buf ) ;
1732
- assert ( readResult ) ;
1733
- msg += decoder . decode ( buf . subarray ( 0 , readResult ) ) ;
1734
- assert ( msg . endsWith ( "\r\nfoo bar baz\r\n0\r\n\r\n" ) ) ;
1735
- break ;
1736
- } catch {
1737
- continue ;
1738
- }
1739
- }
1740
- }
1741
-
1742
- // once more!
1743
- {
1744
- const body =
1745
- `GET /quux HTTP/1.1\r\nHost: example.domain\r\nConnection: close\r\n\r\n` ;
1746
- const writeResult = await conn . write ( encoder . encode ( body ) ) ;
1747
- assertEquals ( body . length , writeResult ) ;
1748
- await promises [ 1 ] ;
1749
- }
1750
- {
1751
- const buf = new Uint8Array ( 1024 ) ;
1752
- const readResult = await conn . read ( buf ) ;
1753
- assert ( readResult ) ;
1754
- const msg = decoder . decode ( buf . subarray ( 0 , readResult ) ) ;
1755
- assert ( msg . endsWith ( "zar quux" ) ) ;
1756
- }
1757
-
1758
- conn . close ( ) ;
1759
-
1760
- ac . abort ( ) ;
1761
- await server ;
1762
- } ,
1763
- ) ;
1764
-
1765
1737
Deno . test (
1766
1738
{ permissions : { net : true } } ,
1767
1739
async function httpServerPostWithContentLengthBody ( ) {
0 commit comments