Skip to content

Commit e3cd734

Browse files
committed
added browser property headers to be able to change the headers with the methods withHeader and withoutHeader
1 parent cf6b150 commit e3cd734

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/Browser.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Browser
2323
private $transaction;
2424
private $baseUrl;
2525
private $protocolVersion = '1.1';
26+
private $headers = array();
2627

2728
/**
2829
* The `Browser` is responsible for sending HTTP requests to your HTTP server
@@ -725,6 +726,32 @@ public function withResponseBuffer($maximumSize)
725726
));
726727
}
727728

729+
/**
730+
* @param $header
731+
* @param $value
732+
* @return Browser
733+
*@todo documentation
734+
*
735+
*/
736+
public function withHeader($header, $value)
737+
{
738+
$browser = clone $this;
739+
$browser->headers[$header] = $value;
740+
741+
return $browser;
742+
}
743+
744+
public function withoutHeader($header)
745+
{
746+
$browser = clone $this;
747+
748+
if (array_key_exists($header, $browser->headers)) {
749+
unset($browser->headers[$header]);
750+
}
751+
752+
return $browser;
753+
}
754+
728755
/**
729756
* Changes the [options](#options) to use:
730757
*
@@ -784,7 +811,12 @@ private function requestMayBeStreaming($method, $url, array $headers = array(),
784811
}
785812

786813
return $this->transaction->send(
787-
new Request($method, $url, $headers, $body, $this->protocolVersion)
814+
new Request($method, $url, $this->mergeHeaders($headers), $body, $this->protocolVersion)
788815
);
789816
}
817+
818+
private function mergeHeaders(array $headers)
819+
{
820+
return array_merge($headers, $this->headers);
821+
}
790822
}

tests/BrowserTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,4 +503,17 @@ public function testCancelGetRequestShouldCancelUnderlyingSocketConnection()
503503
$promise = $this->browser->get('http://example.com/');
504504
$promise->cancel();
505505
}
506+
507+
public function testWithElseHeader()
508+
{
509+
$this->browser = $this->browser->withHeader('User-Agent', 'ACMC');
510+
511+
$that = $this;
512+
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) use ($that) {
513+
$that->assertEquals('ACMC', $request->getHeader('User-Agent'));
514+
return true;
515+
}))->willReturn(new Promise(function () { }));
516+
517+
$this->browser->get('http://example.com/');
518+
}
506519
}

0 commit comments

Comments
 (0)