Skip to content

Commit

Permalink
Browser constructor now accepts an existing Client instance
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Apr 10, 2019
1 parent 76138c2 commit 3f85676
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ class Browser extends SearchContext
/**
* Class constructor.
*
* @param RequestHandler $handler A handler to serve the requests.
* @param MessageListener|null $listener An optional message listener to use.
* @param Client $httpClient The HTTP client.
* @param bool $setUserAgent Whether to set the User-Agent header. Defaults to true.
*/
public function __construct(RequestHandler $handler, MessageListener $listener = null)
public function __construct(Client $httpClient, bool $setUserAgent = true)
{
$this->httpClient = new Client($handler, null, $listener);
$this->httpClient->setHeaders([
'User-Agent' => 'Brick/Browser'
]);

$this->httpClient = $httpClient;
$this->history = new History();

if ($setUserAgent) {
$httpClient->addHeaders([
'User-Agent' => 'Brick/Browser v0.2'
]);
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ public function getLastResponse() : Response
/**
* Sets the headers to send with each request.
*
* All existing headers will be replaced.
*
* @param array $headers An associative array of headers.
*
* @return static This Client instance.
Expand All @@ -243,6 +245,22 @@ public function setHeaders(array $headers) : Client
return $this;
}

/**
* Adds headers to send with each request.
*
* Existing headers with the same name will be overwritten.
*
* @param array $headers An associative array of headers.
*
* @return static This Client instance.
*/
public function addHeaders(array $headers) : Client
{
$this->headers = $headers + $this->headers;

return $this;
}

/**
* Sets whether to automatically follow redirects.
*
Expand Down
3 changes: 2 additions & 1 deletion tests/BrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Brick\Browser\Browser;
use Brick\Browser\By;
use Brick\Browser\Client\Client;
use Brick\Browser\Tests\Mock\ApplicationMock;

use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -49,7 +50,7 @@ private function assertUrl(string $url, Browser $browser) : void
public function testBrowser() : void
{
$application = new ApplicationMock();
$browser = new Browser($application);
$browser = new Browser(new Client($application));

$browser->open('http://example.com/');

Expand Down

0 comments on commit 3f85676

Please sign in to comment.