@@ -553,7 +553,7 @@ public function testReceiveStreamAndExplicitlyCloseConnectionEvenWhenServerKeeps
553
553
$ socket ->close ();
554
554
}
555
555
556
- public function testRequestWillCreateNewConnectionForSecondRequestByDefaultEvenWhenServerKeepsConnectionOpen ()
556
+ public function testRequestWithConnectionCloseHeaderWillCreateNewConnectionForSecondRequestEvenWhenServerKeepsConnectionOpen ()
557
557
{
558
558
$ twice = $ this ->expectCallableOnce ();
559
559
$ socket = new SocketServer ('127.0.0.1:0 ' );
@@ -570,6 +570,9 @@ public function testRequestWillCreateNewConnectionForSecondRequestByDefaultEvenW
570
570
571
571
$ this ->base = str_replace ('tcp: ' , 'http: ' , $ socket ->getAddress ()) . '/ ' ;
572
572
573
+ // add `Connection: close` request header to disable HTTP keep-alive
574
+ $ this ->browser = $ this ->browser ->withHeader ('Connection ' , 'close ' );
575
+
573
576
$ response = \React \Async \await ($ this ->browser ->get ($ this ->base . 'get ' ));
574
577
assert ($ response instanceof ResponseInterface);
575
578
$ this ->assertEquals ('hello ' , (string )$ response ->getBody ());
@@ -579,12 +582,54 @@ public function testRequestWillCreateNewConnectionForSecondRequestByDefaultEvenW
579
582
$ this ->assertEquals ('hello ' , (string )$ response ->getBody ());
580
583
}
581
584
582
- public function testRequestWithoutConnectionHeaderWillReuseExistingConnectionForSecondRequest ()
585
+ public function testRequestWithHttp10WillCreateNewConnectionForSecondRequestEvenWhenServerKeepsConnectionOpen ()
586
+ {
587
+ $ twice = $ this ->expectCallableOnce ();
588
+ $ socket = new SocketServer ('127.0.0.1:0 ' );
589
+ $ socket ->on ('connection ' , function (\React \Socket \ConnectionInterface $ connection ) use ($ socket , $ twice ) {
590
+ $ connection ->on ('data ' , function () use ($ connection ) {
591
+ $ connection ->write ("HTTP/1.1 200 OK \r\nContent-Length: 5 \r\n\r\nhello " );
592
+ });
593
+
594
+ $ socket ->on ('connection ' , $ twice );
595
+ $ socket ->on ('connection ' , function () use ($ socket ) {
596
+ $ socket ->close ();
597
+ });
598
+ });
599
+
600
+ $ this ->base = str_replace ('tcp: ' , 'http: ' , $ socket ->getAddress ()) . '/ ' ;
601
+
602
+ // use HTTP/1.0 to disable HTTP keep-alive
603
+ $ this ->browser = $ this ->browser ->withProtocolVersion ('1.0 ' );
604
+
605
+ $ response = \React \Async \await ($ this ->browser ->get ($ this ->base . 'get ' ));
606
+ assert ($ response instanceof ResponseInterface);
607
+ $ this ->assertEquals ('hello ' , (string )$ response ->getBody ());
608
+
609
+ $ response = \React \Async \await ($ this ->browser ->get ($ this ->base . 'get ' ));
610
+ assert ($ response instanceof ResponseInterface);
611
+ $ this ->assertEquals ('hello ' , (string )$ response ->getBody ());
612
+ }
613
+
614
+ public function testRequestWillReuseExistingConnectionForSecondRequestByDefault ()
583
615
{
584
616
$ this ->socket ->on ('connection ' , $ this ->expectCallableOnce ());
585
617
586
- // remove default `Connection: close` request header to enable keep-alive
587
- $ this ->browser = $ this ->browser ->withoutHeader ('Connection ' );
618
+ $ response = \React \Async \await ($ this ->browser ->get ($ this ->base . 'get ' ));
619
+ assert ($ response instanceof ResponseInterface);
620
+ $ this ->assertEquals ('hello ' , (string )$ response ->getBody ());
621
+
622
+ $ response = \React \Async \await ($ this ->browser ->get ($ this ->base . 'get ' ));
623
+ assert ($ response instanceof ResponseInterface);
624
+ $ this ->assertEquals ('hello ' , (string )$ response ->getBody ());
625
+ }
626
+
627
+ public function testRequestWithHttp10AndConnectionKeepAliveHeaderWillReuseExistingConnectionForSecondRequest ()
628
+ {
629
+ $ this ->socket ->on ('connection ' , $ this ->expectCallableOnce ());
630
+
631
+ $ this ->browser = $ this ->browser ->withProtocolVersion ('1.0 ' );
632
+ $ this ->browser = $ this ->browser ->withHeader ('Connection ' , 'keep-alive ' );
588
633
589
634
$ response = \React \Async \await ($ this ->browser ->get ($ this ->base . 'get ' ));
590
635
assert ($ response instanceof ResponseInterface);
0 commit comments