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