Skip to content

Commit

Permalink
Add default header for browser
Browse files Browse the repository at this point in the history
  • Loading branch information
gsouf committed Jul 26, 2017
1 parent fcbe58a commit 8431b74
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 26 deletions.
62 changes: 58 additions & 4 deletions src/Core/Browser/AbstractBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
use Psr\Http\Message\RequestInterface;
use Serps\Core\Psr7\RequestBuilder;
use Serps\Core\Url\UrlArchiveInterface;
use function strtoupper;
use function var_dump;

abstract class AbstractBrowser implements BrowserInterface
{

protected $defaultHeaders = [];
protected $defaultHeadersUC = [];

/**
* @inheritdoc
*/
Expand All @@ -21,6 +26,58 @@ public function sendRequest(RequestInterface $request)
return $this->getHttpClient()->sendRequest($request, $this->getProxy(), $this->getCookieJar());
}

/**
* @return null|string
*/
public function getAcceptLanguage()
{
return $this->getDefaultHeaderValue('ACCEPT-LANGUAGE');
}

/**
* @return null|string
*/
public function getUserAgent()
{
return $this->getDefaultHeaderValue('USER-AGENT');
}

/**
* Adds a default header to be sent with every request
* @param $headerName
* @param $headerValue
*/
public function setDefaultHeader($headerName, $headerValue)
{
$this->defaultHeaders[$headerName] = $headerValue;
$this->defaultHeadersUC[strtoupper($headerName)] = $headerName;
}

/**
* Check if the header name is defined as a default header
* @param $headerName
* @return bool
*/
public function hasDefaultHeader($headerName)
{
return isset($this->defaultHeadersUC[strtoupper($headerName)]);
}

/**
* Get the value of the default given default header name or null if not set
* @param $headerName
* @return bool
*/
public function getDefaultHeaderValue($headerName)
{
$headerName = strtoupper($headerName);
if (isset($this->defaultHeadersUC[$headerName])) {
return $this->defaultHeaders[$this->defaultHeadersUC[$headerName]];
} else {
return null;
}
}

/**
* @inheritdoc
*/
Expand All @@ -38,10 +95,7 @@ public function prepareRequest(RequestInterface $request)
*/
public function getDefaultHeaders()
{
return [
'User-Agent' => $this->getUserAgent(),
'Accept-Language' => $this->getAcceptLanguage()
];
return $this->defaultHeaders;
}

/**
Expand Down
26 changes: 4 additions & 22 deletions src/Core/Browser/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

class Browser extends AbstractBrowser
{
protected $acceptLanguage;
protected $userAgent;

/**
* @var null|CookieJarInterface
Expand Down Expand Up @@ -48,28 +46,12 @@ public function __construct(
) {

$this->httpClient = $httpClient;
$this->userAgent = $userAgent ? $userAgent : 'serps';
$this->acceptLanguage = $acceptLanguage ? $acceptLanguage : 'en-US,en;q=0.8';
$this->setAcceptLanguage($acceptLanguage ? $acceptLanguage : 'en-US,en;q=0.8');
$this->setUserAgent($userAgent ? $userAgent : 'serps');
$this->cookieJar = $cookieJar;
$this->proxy = $proxy;
}

/**
* @return null|string
*/
public function getAcceptLanguage()
{
return $this->acceptLanguage;
}

/**
* @return null|string
*/
public function getUserAgent()
{
return $this->userAgent;
}

/**
* @return null|CookieJarInterface
*/
Expand Down Expand Up @@ -99,15 +81,15 @@ public function getHttpClient()
*/
public function setAcceptLanguage($acceptLanguage)
{
$this->acceptLanguage = $acceptLanguage;
$this->setDefaultHeader('Accept-Language', $acceptLanguage);
}

/**
* @param null|string $userAgent
*/
public function setUserAgent($userAgent)
{
$this->userAgent = $userAgent;
$this->setDefaultHeader('User-Agent', $userAgent);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions test/suites/TDD/Core/Browser/BrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Serps\Test\Core\Browser;

use Serps\Core\Browser\AbstractBrowser;
use Serps\Core\Cookie\ArrayCookieJar;
use Serps\Core\Http\Proxy;
use Serps\Core\Http\StackingHttpClient;
Expand Down Expand Up @@ -73,6 +74,7 @@ public function testSendRequest()
////
// test request with method headers and body
$browser = new Browser($httpClient, 'foo-browser'); // Set a browser with different UA
$browser->setDefaultHeader('foo-bar', 'baz');
$httpClient->resetStack();
$request = RequestBuilder::buildRequest('http://foo.com', 'POST', ['User-Agent' => 'foobarua', 'x-foo' => 'bar'], 'a=b');

Expand All @@ -84,6 +86,7 @@ public function testSendRequest()
[
'User-Agent' => ['foo-browser'],
'Accept-Language' => ['en-US,en;q=0.8'],
'foo-bar' => ['baz'],
'x-foo' => ['bar'],
'Host' => ['foo.com']
],
Expand All @@ -93,6 +96,21 @@ public function testSendRequest()
$this->assertEquals('a=b', (string) $httpClient->getStack()[0]->request->getBody());
}

public function testDefaultHeaders()
{
/* @var \Serps\Core\Browser\AbstractBrowser $browser */
$browser = $this->getMockForAbstractClass(AbstractBrowser::class);

$this->assertFalse($browser->hasDefaultHeader('foo'));
$this->assertNull($browser->getDefaultHeaderValue('foo'));
$browser->setDefaultHeader('foo', 'bar');
$this->assertTrue($browser->hasDefaultHeader('foo'));
$this->assertTrue($browser->hasDefaultHeader('Foo'));
$this->assertEquals('bar', $browser->getDefaultHeaderValue('foo'));
$this->assertEquals('bar', $browser->getDefaultHeaderValue('Foo'));
$this->assertEquals(['foo' => 'bar'], $browser->getDefaultHeaders());
}

public function testAccessors()
{

Expand Down

0 comments on commit 8431b74

Please sign in to comment.