Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 166
Support for default request headers#461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -23,6 +23,9 @@ class Browser | ||
| private $transaction; | ||
| private $baseUrl; | ||
| private $protocolVersion = '1.1'; | ||
| private $defaultHeaders = array( | ||
| 'User-Agent' => 'ReactPHP/1' | ||
| ); | ||
| /** | ||
| * The `Browser` is responsible for sending HTTP requests to your HTTP server | ||
| @@ -725,6 +728,62 @@ public function withResponseBuffer($maximumSize) | ||
| )); | ||
| } | ||
| /** | ||
| * Add a request header for all following requests. | ||
| * | ||
| * ```php | ||
| * $browser = $browser->withHeader('User-Agent', 'ACME'); | ||
| * | ||
| * $browser->get($url)->then(…); | ||
| * ``` | ||
| * | ||
| * Note that the new header will overwrite any headers previously set with | ||
| * the same name (case-insensitive). Following requests will use these headers | ||
| * by default unless they are explicitly set for any requests. | ||
| * | ||
| * @param string $header | ||
| * @param string $value | ||
| * @return Browser | ||
| */ | ||
| public function withHeader($header, $value) | ||
| { | ||
| $browser = $this->withoutHeader($header); | ||
| $browser->defaultHeaders[$header] = $value; | ||
| return $browser; | ||
| } | ||
| /** | ||
| * Remove any default request headers previously set via | ||
| * the [`withHeader()` method](#withheader). | ||
| * | ||
| * ```php | ||
| * $browser = $browser->withoutHeader('User-Agent'); | ||
| * | ||
| * $browser->get($url)->then(…); | ||
| * ``` | ||
| * | ||
| * Note that this method only affects the headers which were set with the | ||
| * method `withHeader(string $header, string $value): Browser` | ||
| * | ||
| * @param string $header | ||
| * @return Browser | ||
| */ | ||
| public function withoutHeader($header) | ||
| { | ||
| $browser = clone $this; | ||
| /** @var string|int $key */ | ||
| foreach (\array_keys($browser->defaultHeaders) as $key) { | ||
| if (\strcasecmp($key, $header) === 0) { | ||
| unset($browser->defaultHeaders[$key]); | ||
| break; | ||
| } | ||
| } | ||
clue marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return $browser; | ||
| } | ||
| /** | ||
| * Changes the [options](#options) to use: | ||
| * | ||
| @@ -783,6 +842,19 @@ private function requestMayBeStreaming($method, $url, array $headers = array(), | ||
| $body = new ReadableBodyStream($body); | ||
| } | ||
| foreach ($this->defaultHeaders as $key => $value) { | ||
| $explicitHeaderExists = false; | ||
| foreach (\array_keys($headers) as $headerKey) { | ||
| if (\strcasecmp($headerKey, $key) === 0) { | ||
| $explicitHeaderExists = true; | ||
51imyyy marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| break; | ||
| } | ||
| } | ||
| if (!$explicitHeaderExists) { | ||
| $headers[$key] = $value; | ||
| } | ||
| } | ||
| return $this->transaction->send( | ||
| new Request($method, $url, $headers, $body, $this->protocolVersion) | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -503,4 +503,101 @@ public function testCancelGetRequestShouldCancelUnderlyingSocketConnection() | ||
| $promise = $this->browser->get('http://example.com/'); | ||
| $promise->cancel(); | ||
| } | ||
| public function testWithHeaderShouldOverwriteExistingHeader() | ||
| { | ||
| $this->browser = $this->browser->withHeader('User-Agent', 'ACMC'); //should be overwritten | ||
| $this->browser = $this->browser->withHeader('user-agent', 'ABC'); //should be the user-agent | ||
| $that = $this; | ||
| $this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) use ($that) { | ||
| $that->assertEquals(array('ABC'), $request->getHeader('UsEr-AgEnT')); | ||
| return true; | ||
| }))->willReturn(new Promise(function () { })); | ||
| $this->browser->get('http://example.com/'); | ||
| } | ||
| public function testWithHeaderShouldBeOverwrittenByExplicitHeaderInGetMethod() | ||
| { | ||
| $this->browser = $this->browser->withHeader('User-Agent', 'ACMC'); | ||
| $that = $this; | ||
| $this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) use ($that) { | ||
| $that->assertEquals(array('ABC'), $request->getHeader('UsEr-AgEnT')); | ||
| return true; | ||
| }))->willReturn(new Promise(function () { })); | ||
| $this->browser->get('http://example.com/', array('user-Agent' => 'ABC')); //should win | ||
| } | ||
| public function testWithMultipleHeadersShouldBeMergedCorrectlyWithMultipleDefaultHeaders() | ||
clue marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| { | ||
| $this->browser = $this->browser->withHeader('User-Agent', 'ACMC'); | ||
| $this->browser = $this->browser->withHeader('User-Test', 'Test'); | ||
| $this->browser = $this->browser->withHeader('Custom-HEADER', 'custom'); | ||
| $this->browser = $this->browser->withHeader('just-a-header', 'header-value'); | ||
| $that = $this; | ||
| $this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) use ($that) { | ||
| $expectedHeaders = array( | ||
| 'Host' => array('example.com'), | ||
| 'User-Test' => array('Test'), | ||
| 'just-a-header' => array('header-value'), | ||
| 'user-Agent' => array('ABC'), | ||
| 'another-header' => array('value'), | ||
| 'custom-header' => array('data'), | ||
| ); | ||
| $that->assertEquals($expectedHeaders, $request->getHeaders()); | ||
| return true; | ||
| }))->willReturn(new Promise(function () { })); | ||
| $headers = array( | ||
| 'user-Agent' => 'ABC', //should overwrite: 'User-Agent', 'ACMC' | ||
| 'another-header' => 'value', | ||
| 'custom-header' => 'data', //should overwrite: 'Custom-header', 'custom' | ||
| ); | ||
| $this->browser->get('http://example.com/', $headers); | ||
| } | ||
| public function testWithoutHeaderShouldRemoveExistingHeader() | ||
| { | ||
| $this->browser = $this->browser->withHeader('User-Agent', 'ACMC'); | ||
| $this->browser = $this->browser->withoutHeader('UsEr-AgEnT'); //should remove case-insensitive header | ||
| $that = $this; | ||
| $this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) use ($that) { | ||
| $that->assertEquals(array(), $request->getHeader('user-agent')); | ||
| return true; | ||
| }))->willReturn(new Promise(function () { })); | ||
| $this->browser->get('http://example.com/'); | ||
| } | ||
| public function testBrowserShouldSendDefaultUserAgentHeader() | ||
| { | ||
| $that = $this; | ||
| $this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) use ($that) { | ||
| $that->assertEquals(array(0 => 'ReactPHP/1'), $request->getHeader('user-agent')); | ||
| return true; | ||
| }))->willReturn(new Promise(function () { })); | ||
| $this->browser->get('http://example.com/'); | ||
| } | ||
| public function testBrowserShouldNotSendDefaultUserAgentHeaderIfWithoutHeaderRemovesUserAgent() | ||
| { | ||
| $this->browser = $this->browser->withoutHeader('UsEr-AgEnT'); | ||
| $that = $this; | ||
| $this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) use ($that) { | ||
| $that->assertEquals(array(), $request->getHeader('User-Agent')); | ||
| return true; | ||
| }))->willReturn(new Promise(function () { })); | ||
| $this->browser->get('http://example.com/'); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.