Skip to content

Commit

Permalink
API require set HTTP User-Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Jul 21, 2017
1 parent afa343a commit bedbe58
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -35,6 +35,10 @@ anime_db_shikimori_browser:
# Prefix for API resurces
# As a default used '/api/'
prefix: '/api/'

# HTTP User-Agent
# No default value
client: 'My Custom Bot 1.0'
```

Usage
Expand Down
Expand Up @@ -32,6 +32,7 @@ public function load(array $configs, ContainerBuilder $container)
->getDefinition('anime_db.shikimori.browser')
->replaceArgument(1, $config['host'])
->replaceArgument(2, $config['prefix'])
->replaceArgument(3, $config['client'])
;
}
}
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -23,6 +23,7 @@ class Configuration implements ConfigurationInterface
* anime_db_shikimori_browser:
* host: 'https://shikimori.org'
* prefix: '/api/'
* client: 'My Custom Bot 1.0'
*
* @return TreeBuilder
*/
Expand All @@ -39,6 +40,10 @@ public function getConfigTreeBuilder()
->cannotBeEmpty()
->defaultValue('/api/')
->end()
->scalarNode('client')
->cannotBeEmpty()
->isRequired()
->end()
->end()
->end()
;
Expand Down
14 changes: 13 additions & 1 deletion src/Service/Browser.php
Expand Up @@ -25,6 +25,11 @@ class Browser
*/
private $prefix;

/**
* @var string
*/
private $app_client;

/**
* @var Client
*/
Expand All @@ -34,12 +39,14 @@ class Browser
* @param Client $client
* @param string $host
* @param string $prefix
* @param string $app_client
*/
public function __construct(Client $client, $host, $prefix)
public function __construct(Client $client, $host, $prefix, $app_client)
{
$this->client = $client;
$this->host = $host;
$this->prefix = $prefix;
$this->app_client = $app_client;
}

/**
Expand Down Expand Up @@ -106,6 +113,11 @@ public function delete($resource, array $options = [])
*/
private function request($method, $path = '', array $options = [])
{
$options['headers'] = array_merge(
['User-Agent' => $this->app_client],
isset($options['headers']) ? $options['headers'] : []
);

try {
$response = $this->client->request($method, $this->host.$this->prefix.$path, $options);
} catch (\Exception $e) {
Expand Down
18 changes: 16 additions & 2 deletions tests/DependencyInjection/AnimeDbShikimoriBrowserExtensionTest.php
Expand Up @@ -36,19 +36,26 @@ public function config()
{
return [
[
[],
[
'anime_db_shikimori_browser' => [
'client' => 'My Custom Bot 1.0',
],
],
'https://shikimori.org',
'/api/',
'My Custom Bot 1.0',
],
[
[
'anime_db_shikimori_browser' => [
'host' => 'http://shikimori.org',
'prefix' => '/api/v2/',
'client' => 'My Custom Bot 1.0',
],
],
'http://shikimori.org',
'/api/v2/',
'My Custom Bot 1.0',
],
];
}
Expand All @@ -59,8 +66,9 @@ public function config()
* @param array $config
* @param string $host
* @param string $prefix
* @param string $client
*/
public function testLoad(array $config, $host, $prefix)
public function testLoad(array $config, $host, $prefix, $client)
{
$browser = $this->getMock(Definition::class);
$browser
Expand All @@ -75,6 +83,12 @@ public function testLoad(array $config, $host, $prefix)
->with(2, $prefix)
->will($this->returnSelf())
;
$browser
->expects($this->at(2))
->method('replaceArgument')
->with(3, $client)
->will($this->returnSelf())
;

$this->container
->expects($this->once())
Expand Down
5 changes: 4 additions & 1 deletion tests/DependencyInjection/ConfigurationTest.php
Expand Up @@ -43,7 +43,7 @@ public function testConfigTree()
$children = $tree->getChildren();

$this->assertInternalType('array', $children);
$this->assertEquals(['host', 'prefix'], array_keys($children));
$this->assertEquals(['host', 'prefix', 'client'], array_keys($children));

$this->assertInstanceOf(ScalarNode::class, $children['host']);
$this->assertEquals('https://shikimori.org', $children['host']->getDefaultValue());
Expand All @@ -52,5 +52,8 @@ public function testConfigTree()
$this->assertInstanceOf(ScalarNode::class, $children['prefix']);
$this->assertEquals('/api/', $children['prefix']->getDefaultValue());
$this->assertFalse($children['prefix']->isRequired());

$this->assertInstanceOf(ScalarNode::class, $children['client']);
$this->assertTrue($children['client']->isRequired());
}
}
92 changes: 86 additions & 6 deletions tests/Service/BrowserTest.php
Expand Up @@ -27,6 +27,11 @@ class BrowserTest extends \PHPUnit_Framework_TestCase
*/
private $prefix = 'bar';

/**
* @var string
*/
private $app_client = 'My Custom Bot 1.0';

/**
* @var \PHPUnit_Framework_MockObject_MockObject|Client
*/
Expand Down Expand Up @@ -55,35 +60,110 @@ protected function setUp()
->getMock()
;

$this->browser = new Browser($this->client, $this->host, $this->prefix);
$this->browser = new Browser($this->client, $this->host, $this->prefix, $this->app_client);
}

public function requests()
{
return [
[
'GET',
['user' => 123],
[
'user' => 123,
'headers' => [
'User-Agent' => $this->app_client,
],
],
['ignored' => true],
],
[
'GET',
[
'user' => 123,
'headers' => [
'User-Agent' => 'Override User Agent',
],
],
['ignored' => true],
],
[
'POST',
[
'user' => 123,
'headers' => [
'User-Agent' => $this->app_client,
],
],
['ignored' => true],
],
[
'POST',
['user' => 123],
[
'user' => 123,
'headers' => [
'User-Agent' => 'Override User Agent',
],
],
['ignored' => true],
],
[
'PUT',
['user' => 123],
[
'user' => 123,
'headers' => [
'User-Agent' => $this->app_client,
],
],
['ignored' => true],
],
[
'PUT',
[
'user' => 123,
'headers' => [
'User-Agent' => 'Override User Agent',
],
],
['ignored' => true],
],
[
'PATCH',
[
'user' => 123,
'headers' => [
'User-Agent' => $this->app_client,
],
],
['ignored' => true],
],
[
'PATCH',
['user' => 123],
[
'user' => 123,
'headers' => [
'User-Agent' => 'Override User Agent',
],
],
['ignored' => true],
],
[
'DELETE',
[
'user' => 123,
'headers' => [
'User-Agent' => $this->app_client,
],
],
['ignored' => true],
],
[
'DELETE',
['user' => 123],
[
'user' => 123,
'headers' => [
'User-Agent' => 'Override User Agent',
],
],
['ignored' => true],
],
];
Expand Down

0 comments on commit bedbe58

Please sign in to comment.