@@ -503,4 +503,101 @@ public function testCancelGetRequestShouldCancelUnderlyingSocketConnection()
503503 $ promise = $ this ->browser ->get ('http://example.com/ ' );
504504 $ promise ->cancel ();
505505 }
506+
507+ public function testWithHeaderShouldOverwriteExistingHeader ()
508+ {
509+ $ this ->browser = $ this ->browser ->withHeader ('User-Agent ' , 'ACMC ' ); //should be overwritten
510+ $ this ->browser = $ this ->browser ->withHeader ('user-agent ' , 'ABC ' ); //should be the user-agent
511+
512+ $ that = $ this ;
513+ $ this ->sender ->expects ($ this ->once ())->method ('send ' )->with ($ this ->callback (function (RequestInterface $ request ) use ($ that ) {
514+ $ that ->assertEquals (array ('ABC ' ), $ request ->getHeader ('UsEr-AgEnT ' ));
515+ return true ;
516+ }))->willReturn (new Promise (function () { }));
517+
518+ $ this ->browser ->get ('http://example.com/ ' );
519+ }
520+
521+ public function testWithHeaderShouldBeOverwrittenByExplicitHeaderInGetMethod ()
522+ {
523+ $ this ->browser = $ this ->browser ->withHeader ('User-Agent ' , 'ACMC ' );
524+
525+ $ that = $ this ;
526+ $ this ->sender ->expects ($ this ->once ())->method ('send ' )->with ($ this ->callback (function (RequestInterface $ request ) use ($ that ) {
527+ $ that ->assertEquals (array ('ABC ' ), $ request ->getHeader ('UsEr-AgEnT ' ));
528+ return true ;
529+ }))->willReturn (new Promise (function () { }));
530+
531+ $ this ->browser ->get ('http://example.com/ ' , array ('user-Agent ' => 'ABC ' )); //should win
532+ }
533+
534+ public function testWithMultipleHeadersShouldBeMergedCorrectlyWithMultipleDefaultHeaders ()
535+ {
536+ $ this ->browser = $ this ->browser ->withHeader ('User-Agent ' , 'ACMC ' );
537+ $ this ->browser = $ this ->browser ->withHeader ('User-Test ' , 'Test ' );
538+ $ this ->browser = $ this ->browser ->withHeader ('Custom-HEADER ' , 'custom ' );
539+ $ this ->browser = $ this ->browser ->withHeader ('just-a-header ' , 'header-value ' );
540+
541+ $ that = $ this ;
542+ $ this ->sender ->expects ($ this ->once ())->method ('send ' )->with ($ this ->callback (function (RequestInterface $ request ) use ($ that ) {
543+ $ expectedHeaders = array (
544+ 'Host ' => array ('example.com ' ),
545+
546+ 'User-Test ' => array ('Test ' ),
547+ 'just-a-header ' => array ('header-value ' ),
548+
549+ 'user-Agent ' => array ('ABC ' ),
550+ 'another-header ' => array ('value ' ),
551+ 'custom-header ' => array ('data ' ),
552+ );
553+
554+ $ that ->assertEquals ($ expectedHeaders , $ request ->getHeaders ());
555+ return true ;
556+ }))->willReturn (new Promise (function () { }));
557+
558+ $ headers = array (
559+ 'user-Agent ' => 'ABC ' , //should overwrite: 'User-Agent', 'ACMC'
560+ 'another-header ' => 'value ' ,
561+ 'custom-header ' => 'data ' , //should overwrite: 'Custom-header', 'custom'
562+ );
563+ $ this ->browser ->get ('http://example.com/ ' , $ headers );
564+ }
565+
566+ public function testWithoutHeaderShouldRemoveExistingHeader ()
567+ {
568+ $ this ->browser = $ this ->browser ->withHeader ('User-Agent ' , 'ACMC ' );
569+ $ this ->browser = $ this ->browser ->withoutHeader ('UsEr-AgEnT ' ); //should remove case-insensitive header
570+
571+ $ that = $ this ;
572+ $ this ->sender ->expects ($ this ->once ())->method ('send ' )->with ($ this ->callback (function (RequestInterface $ request ) use ($ that ) {
573+ $ that ->assertEquals (array (), $ request ->getHeader ('user-agent ' ));
574+ return true ;
575+ }))->willReturn (new Promise (function () { }));
576+
577+ $ this ->browser ->get ('http://example.com/ ' );
578+ }
579+
580+ public function testBrowserShouldSendDefaultUserAgentHeader ()
581+ {
582+ $ that = $ this ;
583+ $ this ->sender ->expects ($ this ->once ())->method ('send ' )->with ($ this ->callback (function (RequestInterface $ request ) use ($ that ) {
584+ $ that ->assertEquals (array (0 => 'ReactPHP/1 ' ), $ request ->getHeader ('user-agent ' ));
585+ return true ;
586+ }))->willReturn (new Promise (function () { }));
587+
588+ $ this ->browser ->get ('http://example.com/ ' );
589+ }
590+
591+ public function testBrowserShouldNotSendDefaultUserAgentHeaderIfWithoutHeaderRemovesUserAgent ()
592+ {
593+ $ this ->browser = $ this ->browser ->withoutHeader ('UsEr-AgEnT ' );
594+
595+ $ that = $ this ;
596+ $ this ->sender ->expects ($ this ->once ())->method ('send ' )->with ($ this ->callback (function (RequestInterface $ request ) use ($ that ) {
597+ $ that ->assertEquals (array (), $ request ->getHeader ('User-Agent ' ));
598+ return true ;
599+ }))->willReturn (new Promise (function () { }));
600+
601+ $ this ->browser ->get ('http://example.com/ ' );
602+ }
506603}
0 commit comments