@@ -677,7 +677,39 @@ function ($data) use (&$buffer) {
677677 $ this ->assertStringEndsWith ("\r\n\r\n0 \r\n\r\n" , $ buffer );
678678 }
679679
680- public function testStreamAlreadyClosedWillSendEmptyBodyPlainHttp10 ()
680+ public function testResponseStreamEndingWillSendEmptyBodyChunkedEncoded ()
681+ {
682+ $ stream = new ThroughStream ();
683+
684+ $ server = new Server ($ this ->socket , function (ServerRequestInterface $ request ) use ($ stream ) {
685+ return new Response (200 , array (), $ stream );
686+ });
687+
688+ $ buffer = '' ;
689+
690+ $ this ->connection
691+ ->expects ($ this ->any ())
692+ ->method ('write ' )
693+ ->will (
694+ $ this ->returnCallback (
695+ function ($ data ) use (&$ buffer ) {
696+ $ buffer .= $ data ;
697+ }
698+ )
699+ );
700+
701+ $ this ->socket ->emit ('connection ' , array ($ this ->connection ));
702+
703+ $ data = "GET / HTTP/1.1 \r\nHost: localhost \r\n\r\n" ;
704+ $ this ->connection ->emit ('data ' , array ($ data ));
705+
706+ $ stream ->end ();
707+
708+ $ this ->assertStringStartsWith ("HTTP/1.1 200 OK \r\n" , $ buffer );
709+ $ this ->assertStringEndsWith ("\r\n\r\n0 \r\n\r\n" , $ buffer );
710+ }
711+
712+ public function testResponseStreamAlreadyClosedWillSendEmptyBodyPlainHttp10 ()
681713 {
682714 $ stream = new ThroughStream ();
683715 $ stream ->close ();
@@ -706,7 +738,73 @@ function ($data) use (&$buffer) {
706738
707739 $ this ->assertStringStartsWith ("HTTP/1.0 200 OK \r\n" , $ buffer );
708740 $ this ->assertStringEndsWith ("\r\n\r\n" , $ buffer );
709- }
741+ }
742+
743+ public function testResponseStreamWillBeClosedIfConnectionIsAlreadyClosed ()
744+ {
745+ $ stream = new ThroughStream ();
746+ $ stream ->on ('close ' , $ this ->expectCallableOnce ());
747+
748+ $ server = new Server ($ this ->socket , function (ServerRequestInterface $ request ) use ($ stream ) {
749+ return new Response (200 , array (), $ stream );
750+ });
751+
752+ $ buffer = '' ;
753+
754+ $ this ->connection
755+ ->expects ($ this ->any ())
756+ ->method ('write ' )
757+ ->will (
758+ $ this ->returnCallback (
759+ function ($ data ) use (&$ buffer ) {
760+ $ buffer .= $ data ;
761+ }
762+ )
763+ );
764+
765+ $ this ->connection = $ this ->getMockBuilder ('React\Socket\Connection ' )
766+ ->disableOriginalConstructor ()
767+ ->setMethods (
768+ array (
769+ 'write ' ,
770+ 'end ' ,
771+ 'close ' ,
772+ 'pause ' ,
773+ 'resume ' ,
774+ 'isReadable ' ,
775+ 'isWritable ' ,
776+ 'getRemoteAddress ' ,
777+ 'getLocalAddress ' ,
778+ 'pipe '
779+ )
780+ )
781+ ->getMock ();
782+
783+ $ this ->connection ->expects ($ this ->once ())->method ('isWritable ' )->willReturn (false );
784+ $ this ->connection ->expects ($ this ->never ())->method ('write ' );
785+ $ this ->connection ->expects ($ this ->never ())->method ('write ' );
786+
787+ $ this ->socket ->emit ('connection ' , array ($ this ->connection ));
788+
789+ $ data = $ this ->createGetRequest ();
790+ $ this ->connection ->emit ('data ' , array ($ data ));
791+ }
792+
793+ public function testResponseStreamWillBeClosedIfConnectionEmitsCloseEvent ()
794+ {
795+ $ stream = new ThroughStream ();
796+ $ stream ->on ('close ' , $ this ->expectCallableOnce ());
797+
798+ $ server = new Server ($ this ->socket , function (ServerRequestInterface $ request ) use ($ stream ) {
799+ return new Response (200 , array (), $ stream );
800+ });
801+
802+ $ this ->socket ->emit ('connection ' , array ($ this ->connection ));
803+
804+ $ data = $ this ->createGetRequest ();
805+ $ this ->connection ->emit ('data ' , array ($ data ));
806+ $ this ->connection ->emit ('close ' );
807+ }
710808
711809 public function testResponseContainsSameRequestProtocolVersionAndChunkedBodyForHttp11 ()
712810 {
0 commit comments