@@ -25,9 +25,17 @@ class StreamingServerTest extends TestCase
2525     */ 
2626    public  function  setUpConnectionMockAndSocket ()
2727    {
28-         $ this  ->connection  = $ this  ->getMockBuilder ('React\Socket\Connection ' )
28+         $ this  ->connection  = $ this  ->mockConnection ();
29+ 
30+         $ this  ->socket  = new  SocketServerStub ();
31+     }
32+ 
33+ 
34+     private  function  mockConnection (array  $ additionalMethods  = null )
35+     {
36+         $ connection  = $ this  ->getMockBuilder ('React\Socket\Connection ' )
2937            ->disableOriginalConstructor ()
30-             ->setMethods (
38+             ->setMethods (array_merge ( 
3139                array (
3240                    'write ' ,
3341                    'end ' ,
@@ -39,14 +47,15 @@ public function setUpConnectionMockAndSocket()
3947                    'getRemoteAddress ' ,
4048                    'getLocalAddress ' ,
4149                    'pipe ' 
42-                 )
43-             )
50+                 ),
51+                 (is_array ($ additionalMethods ) ? $ additionalMethods  : array ())
52+             ))
4453            ->getMock ();
4554
46-         $ this -> connection ->method ('isWritable ' )->willReturn (true );
47-         $ this -> connection ->method ('isReadable ' )->willReturn (true );
55+         $ connection ->method ('isWritable ' )->willReturn (true );
56+         $ connection ->method ('isReadable ' )->willReturn (true );
4857
49-         $ this -> socket  =  new   SocketServerStub () ;
58+         return   $ connection  ;
5059    }
5160
5261    public  function  testRequestEventWillNotBeEmittedForIncompleteHeaders ()
@@ -3245,6 +3254,25 @@ public function testNewConnectionWillInvokeParserTwiceAfterInvokingRequestHandle
32453254        $ this  ->assertCount (1 , $ this  ->connection ->listeners ('close ' ));
32463255    }
32473256
3257+     public  function  testCompletingARequestWillRemoveConnectionOnCloseListener ()
3258+     {
3259+         $ connection  = $ this  ->mockConnection (array ('removeListener ' ));
3260+ 
3261+         $ request  = new  ServerRequest ('GET ' , 'http://localhost/ ' );
3262+ 
3263+         $ server  = new  StreamingServer (Loop::get (), function  () {
3264+             return  \React \Promise \resolve (new  Response ());
3265+         });
3266+ 
3267+         $ server ->listen ($ this  ->socket );
3268+         $ this  ->socket ->emit ('connection ' , array ($ connection ));
3269+ 
3270+         $ connection ->expects ($ this  ->once ())->method ('removeListener ' );
3271+ 
3272+         // pretend parser just finished parsing 
3273+         $ server ->handleRequest ($ connection , $ request );
3274+     }
3275+ 
32483276    private  function  createGetRequest ()
32493277    {
32503278        $ data  = "GET / HTTP/1.1 \r\n" ;
0 commit comments