@@ -663,7 +663,7 @@ public function testSomeRequestHeadersShouldBeRemovedWhenRedirecting()
663
663
array ($ this ->callback (function (RequestInterface $ request ) use ($ that ) {
664
664
$ that ->assertFalse ($ request ->hasHeader ('Content-Type ' ));
665
665
$ that ->assertFalse ($ request ->hasHeader ('Content-Length ' ));
666
- return true ;;
666
+ return true ;
667
667
}))
668
668
)->willReturnOnConsecutiveCalls (
669
669
Promise \resolve ($ redirectResponse ),
@@ -674,6 +674,122 @@ public function testSomeRequestHeadersShouldBeRemovedWhenRedirecting()
674
674
$ transaction ->send ($ requestWithCustomHeaders );
675
675
}
676
676
677
+ public function testRequestMethodShouldBeChangedWhenRedirectingWithSeeOther ()
678
+ {
679
+ $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
680
+
681
+ $ customHeaders = array (
682
+ 'Content-Type ' => 'text/html; charset=utf-8 ' ,
683
+ 'Content-Length ' => '111 ' ,
684
+ );
685
+
686
+ $ request = new Request ('POST ' , 'http://example.com ' , $ customHeaders );
687
+ $ sender = $ this ->makeSenderMock ();
688
+
689
+ // mock sender to resolve promise with the given $redirectResponse in
690
+ // response to the given $request
691
+ $ redirectResponse = new Response (303 , array ('Location ' => 'http://example.com/new ' ));
692
+
693
+ // mock sender to resolve promise with the given $okResponse in
694
+ // response to the given $request
695
+ $ okResponse = new Response (200 );
696
+ $ that = $ this ;
697
+ $ sender ->expects ($ this ->exactly (2 ))->method ('send ' )->withConsecutive (
698
+ array ($ this ->anything ()),
699
+ array ($ this ->callback (function (RequestInterface $ request ) use ($ that ) {
700
+ $ that ->assertEquals ('GET ' , $ request ->getMethod ());
701
+ $ that ->assertFalse ($ request ->hasHeader ('Content-Type ' ));
702
+ $ that ->assertFalse ($ request ->hasHeader ('Content-Length ' ));
703
+ return true ;
704
+ }))
705
+ )->willReturnOnConsecutiveCalls (
706
+ Promise \resolve ($ redirectResponse ),
707
+ Promise \resolve ($ okResponse )
708
+ );
709
+
710
+ $ transaction = new Transaction ($ sender , $ loop );
711
+ $ transaction ->send ($ request );
712
+ }
713
+
714
+ public function testRequestMethodAndBodyShouldNotBeChangedWhenRedirectingWith307Or308 ()
715
+ {
716
+ $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
717
+
718
+ $ customHeaders = array (
719
+ 'Content-Type ' => 'text/html; charset=utf-8 ' ,
720
+ 'Content-Length ' => '111 ' ,
721
+ );
722
+
723
+ $ request = new Request ('POST ' , 'http://example.com ' , $ customHeaders , '{"key":"value"} ' );
724
+ $ sender = $ this ->makeSenderMock ();
725
+
726
+ // mock sender to resolve promise with the given $redirectResponse in
727
+ // response to the given $request
728
+ $ redirectResponse = new Response (307 , array ('Location ' => 'http://example.com/new ' ));
729
+
730
+ // mock sender to resolve promise with the given $okResponse in
731
+ // response to the given $request
732
+ $ okResponse = new Response (200 );
733
+ $ that = $ this ;
734
+ $ sender ->expects ($ this ->exactly (2 ))->method ('send ' )->withConsecutive (
735
+ array ($ this ->anything ()),
736
+ array ($ this ->callback (function (RequestInterface $ request ) use ($ that ) {
737
+ $ that ->assertEquals ('POST ' , $ request ->getMethod ());
738
+ $ that ->assertEquals ('{"key":"value"} ' , (string )$ request ->getBody ());
739
+ $ that ->assertEquals (
740
+ array (
741
+ 'Content-Type ' => array ('text/html; charset=utf-8 ' ),
742
+ 'Content-Length ' => array ('111 ' ),
743
+ 'Host ' => array ('example.com ' )
744
+ ),
745
+ $ request ->getHeaders ()
746
+ );
747
+ return true ;
748
+ }))
749
+ )->willReturnOnConsecutiveCalls (
750
+ Promise \resolve ($ redirectResponse ),
751
+ Promise \resolve ($ okResponse )
752
+ );
753
+
754
+ $ transaction = new Transaction ($ sender , $ loop );
755
+ $ transaction ->send ($ request );
756
+ }
757
+
758
+ public function testRedirectingStreamingBodyWith307Or308ShouldThrowCantRedirectStreamException ()
759
+ {
760
+ $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
761
+
762
+ $ customHeaders = array (
763
+ 'Content-Type ' => 'text/html; charset=utf-8 ' ,
764
+ 'Content-Length ' => '111 ' ,
765
+ );
766
+
767
+ $ stream = new ThroughStream ();
768
+ $ request = new Request ('POST ' , 'http://example.com ' , $ customHeaders , new ReadableBodyStream ($ stream ));
769
+ $ sender = $ this ->makeSenderMock ();
770
+
771
+ // mock sender to resolve promise with the given $redirectResponse in
772
+ // response to the given $request
773
+ $ redirectResponse = new Response (307 , array ('Location ' => 'http://example.com/new ' ));
774
+
775
+ $ sender ->expects ($ this ->once ())->method ('send ' )->withConsecutive (
776
+ array ($ this ->anything ())
777
+ )->willReturnOnConsecutiveCalls (
778
+ Promise \resolve ($ redirectResponse )
779
+ );
780
+
781
+ $ transaction = new Transaction ($ sender , $ loop );
782
+ $ promise = $ transaction ->send ($ request );
783
+
784
+ $ exception = null ;
785
+ $ promise ->then (null , function ($ reason ) use (&$ exception ) {
786
+ $ exception = $ reason ;
787
+ });
788
+
789
+ assert ($ exception instanceof \RuntimeException);
790
+ $ this ->assertEquals ('Unable to redirect request with streaming body ' , $ exception ->getMessage ());
791
+ }
792
+
677
793
public function testCancelTransactionWillCancelRequest ()
678
794
{
679
795
$ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
0 commit comments