Skip to content

Commit

Permalink
added browser property headers to be able to change the headers with …
Browse files Browse the repository at this point in the history
…the methods withHeader and withoutHeader
  • Loading branch information
sschmitz committed Jun 24, 2022
1 parent cf6b150 commit e3cd734
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/Browser.php
Expand Up @@ -23,6 +23,7 @@ class Browser
private $transaction;
private $baseUrl;
private $protocolVersion = '1.1';
private $headers = array();

/**
* The `Browser` is responsible for sending HTTP requests to your HTTP server
Expand Down Expand Up @@ -725,6 +726,32 @@ public function withResponseBuffer($maximumSize)
));
}

/**
* @param $header
* @param $value
* @return Browser
*@todo documentation
*
*/
public function withHeader($header, $value)
{
$browser = clone $this;
$browser->headers[$header] = $value;

return $browser;
}

public function withoutHeader($header)
{
$browser = clone $this;

if (array_key_exists($header, $browser->headers)) {
unset($browser->headers[$header]);
}

return $browser;
}

/**
* Changes the [options](#options) to use:
*
Expand Down Expand Up @@ -784,7 +811,12 @@ private function requestMayBeStreaming($method, $url, array $headers = array(),
}

return $this->transaction->send(
new Request($method, $url, $headers, $body, $this->protocolVersion)
new Request($method, $url, $this->mergeHeaders($headers), $body, $this->protocolVersion)
);
}

private function mergeHeaders(array $headers)
{
return array_merge($headers, $this->headers);
}
}
13 changes: 13 additions & 0 deletions tests/BrowserTest.php
Expand Up @@ -503,4 +503,17 @@ public function testCancelGetRequestShouldCancelUnderlyingSocketConnection()
$promise = $this->browser->get('http://example.com/');
$promise->cancel();
}

public function testWithElseHeader()
{
$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('ACMC', $request->getHeader('User-Agent'));
return true;
}))->willReturn(new Promise(function () { }));

$this->browser->get('http://example.com/');
}
}

0 comments on commit e3cd734

Please sign in to comment.