@@ -406,7 +406,7 @@ public function testReceivingStreamingBodyWillResolveWithBufferedResponseByDefau
406406 $ this ->assertEquals ('hello world ' , (string )$ response ->getBody ());
407407 }
408408
409- public function testReceivingStreamingBodyWithSizeExceedingMaximumResponseBufferWillRejectAndCloseResponseStream ()
409+ public function testReceivingStreamingBodyWithContentLengthExceedingMaximumResponseBufferWillRejectAndCloseResponseStreamImmediately ()
410410 {
411411 $ stream = new ThroughStream ();
412412 $ stream ->on ('close ' , $ this ->expectCallableOnce ());
@@ -419,11 +419,87 @@ public function testReceivingStreamingBodyWithSizeExceedingMaximumResponseBuffer
419419 $ sender = $ this ->makeSenderMock ();
420420 $ sender ->expects ($ this ->once ())->method ('send ' )->with ($ this ->equalTo ($ request ))->willReturn (Promise \resolve ($ response ));
421421
422+ $ transaction = new Transaction ($ sender , Loop::get ());
423+
424+ $ promise = $ transaction ->send ($ request );
425+
426+ $ exception = null ;
427+ $ promise ->then (null , function ($ e ) use (&$ exception ) {
428+ $ exception = $ e ;
429+ });
430+
431+ $ this ->assertFalse ($ stream ->isWritable ());
432+
433+ assert ($ exception instanceof \OverflowException);
434+ $ this ->assertInstanceOf ('OverflowException ' , $ exception );
435+ $ this ->assertEquals ('Response body size of 100000000 bytes exceeds maximum of 16777216 bytes ' , $ exception ->getMessage ());
436+ $ this ->assertEquals (defined ('SOCKET_EMSGSIZE ' ) ? \SOCKET_EMSGSIZE : 90 , $ exception ->getCode ());
437+ $ this ->assertNull ($ exception ->getPrevious ());
438+ }
439+
440+ public function testReceivingStreamingBodyWithContentsExceedingMaximumResponseBufferWillRejectAndCloseResponseStreamWhenBufferExceedsLimit ()
441+ {
442+ $ stream = new ThroughStream ();
443+ $ stream ->on ('close ' , $ this ->expectCallableOnce ());
444+
445+ $ request = $ this ->getMockBuilder ('Psr\Http\Message\RequestInterface ' )->getMock ();
446+
447+ $ response = new Response (200 , array (), new ReadableBodyStream ($ stream ));
448+
449+ // mock sender to resolve promise with the given $response in response to the given $request
450+ $ sender = $ this ->makeSenderMock ();
451+ $ sender ->expects ($ this ->once ())->method ('send ' )->with ($ this ->equalTo ($ request ))->willReturn (Promise \resolve ($ response ));
452+
453+ $ transaction = new Transaction ($ sender , Loop::get ());
454+ $ transaction = $ transaction ->withOptions (array ('maximumSize ' => 10 ));
455+ $ promise = $ transaction ->send ($ request );
456+
457+ $ exception = null ;
458+ $ promise ->then (null , function ($ e ) use (&$ exception ) {
459+ $ exception = $ e ;
460+ });
461+
462+ $ this ->assertTrue ($ stream ->isWritable ());
463+ $ stream ->write ('hello wörld ' );
464+ $ this ->assertFalse ($ stream ->isWritable ());
465+
466+ assert ($ exception instanceof \OverflowException);
467+ $ this ->assertInstanceOf ('OverflowException ' , $ exception );
468+ $ this ->assertEquals ('Response body size exceeds maximum of 10 bytes ' , $ exception ->getMessage ());
469+ $ this ->assertEquals (defined ('SOCKET_EMSGSIZE ' ) ? \SOCKET_EMSGSIZE : 90 , $ exception ->getCode ());
470+ $ this ->assertNull ($ exception ->getPrevious ());
471+ }
472+
473+ public function testReceivingStreamingBodyWillRejectWhenStreamEmitsError ()
474+ {
475+ $ stream = new ThroughStream (function ($ data ) {
476+ throw new \UnexpectedValueException ('Unexpected ' . $ data , 42 );
477+ });
478+
479+ $ request = $ this ->getMockBuilder ('Psr\Http\Message\RequestInterface ' )->getMock ();
480+ $ response = new Response (200 , array (), new ReadableBodyStream ($ stream ));
481+
482+ // mock sender to resolve promise with the given $response in response to the given $request
483+ $ sender = $ this ->makeSenderMock ();
484+ $ sender ->expects ($ this ->once ())->method ('send ' )->with ($ this ->equalTo ($ request ))->willReturn (Promise \resolve ($ response ));
485+
422486 $ transaction = new Transaction ($ sender , Loop::get ());
423487 $ promise = $ transaction ->send ($ request );
424488
425- $ this ->setExpectedException ('OverflowException ' );
426- \React \Async \await (\React \Promise \Timer \timeout ($ promise , 0.001 ));
489+ $ exception = null ;
490+ $ promise ->then (null , function ($ e ) use (&$ exception ) {
491+ $ exception = $ e ;
492+ });
493+
494+ $ this ->assertTrue ($ stream ->isWritable ());
495+ $ stream ->write ('Foo ' );
496+ $ this ->assertFalse ($ stream ->isWritable ());
497+
498+ assert ($ exception instanceof \RuntimeException);
499+ $ this ->assertInstanceOf ('RuntimeException ' , $ exception );
500+ $ this ->assertEquals ('Error while buffering response body: Unexpected Foo ' , $ exception ->getMessage ());
501+ $ this ->assertEquals (42 , $ exception ->getCode ());
502+ $ this ->assertInstanceOf ('UnexpectedValueException ' , $ exception ->getPrevious ());
427503 }
428504
429505 public function testCancelBufferingResponseWillCloseStreamAndReject ()
@@ -446,8 +522,16 @@ public function testCancelBufferingResponseWillCloseStreamAndReject()
446522 $ deferred ->resolve ($ response );
447523 $ promise ->cancel ();
448524
449- $ this ->setExpectedException ('RuntimeException ' );
450- \React \Async \await (\React \Promise \Timer \timeout ($ promise , 0.001 ));
525+ $ exception = null ;
526+ $ promise ->then (null , function ($ e ) use (&$ exception ) {
527+ $ exception = $ e ;
528+ });
529+
530+ assert ($ exception instanceof \RuntimeException);
531+ $ this ->assertInstanceOf ('RuntimeException ' , $ exception );
532+ $ this ->assertEquals ('Cancelled buffering response body ' , $ exception ->getMessage ());
533+ $ this ->assertEquals (0 , $ exception ->getCode ());
534+ $ this ->assertNull ($ exception ->getPrevious ());
451535 }
452536
453537 public function testReceivingStreamingBodyWillResolveWithStreamingResponseIfStreamingIsEnabled ()
0 commit comments