@@ -406,7 +406,7 @@ public function testReceivingStreamingBodyWillResolveWithBufferedResponseByDefau
406
406
$ this ->assertEquals ('hello world ' , (string )$ response ->getBody ());
407
407
}
408
408
409
- public function testReceivingStreamingBodyWithSizeExceedingMaximumResponseBufferWillRejectAndCloseResponseStream ()
409
+ public function testReceivingStreamingBodyWithContentLengthExceedingMaximumResponseBufferWillRejectAndCloseResponseStreamImmediately ()
410
410
{
411
411
$ stream = new ThroughStream ();
412
412
$ stream ->on ('close ' , $ this ->expectCallableOnce ());
@@ -419,11 +419,87 @@ public function testReceivingStreamingBodyWithSizeExceedingMaximumResponseBuffer
419
419
$ sender = $ this ->makeSenderMock ();
420
420
$ sender ->expects ($ this ->once ())->method ('send ' )->with ($ this ->equalTo ($ request ))->willReturn (Promise \resolve ($ response ));
421
421
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
+
422
486
$ transaction = new Transaction ($ sender , Loop::get ());
423
487
$ promise = $ transaction ->send ($ request );
424
488
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 ());
427
503
}
428
504
429
505
public function testCancelBufferingResponseWillCloseStreamAndReject ()
@@ -446,8 +522,16 @@ public function testCancelBufferingResponseWillCloseStreamAndReject()
446
522
$ deferred ->resolve ($ response );
447
523
$ promise ->cancel ();
448
524
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 ());
451
535
}
452
536
453
537
public function testReceivingStreamingBodyWillResolveWithStreamingResponseIfStreamingIsEnabled ()
0 commit comments