From bedbe58434394245f191a5854080b7be4b66b93a Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 21 Jul 2017 18:55:38 +0300 Subject: [PATCH] API require set HTTP User-Agent --- README.md | 4 + .../AnimeDbShikimoriBrowserExtension.php | 1 + src/DependencyInjection/Configuration.php | 5 + src/Service/Browser.php | 14 ++- .../AnimeDbShikimoriBrowserExtensionTest.php | 18 +++- .../DependencyInjection/ConfigurationTest.php | 5 +- tests/Service/BrowserTest.php | 92 +++++++++++++++++-- 7 files changed, 129 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9eeee58..8457528 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/DependencyInjection/AnimeDbShikimoriBrowserExtension.php b/src/DependencyInjection/AnimeDbShikimoriBrowserExtension.php index 7b2980f..f2d80f3 100644 --- a/src/DependencyInjection/AnimeDbShikimoriBrowserExtension.php +++ b/src/DependencyInjection/AnimeDbShikimoriBrowserExtension.php @@ -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']) ; } } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index d917d69..6a346c9 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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 */ @@ -39,6 +40,10 @@ public function getConfigTreeBuilder() ->cannotBeEmpty() ->defaultValue('/api/') ->end() + ->scalarNode('client') + ->cannotBeEmpty() + ->isRequired() + ->end() ->end() ->end() ; diff --git a/src/Service/Browser.php b/src/Service/Browser.php index 1676e2a..93526b8 100644 --- a/src/Service/Browser.php +++ b/src/Service/Browser.php @@ -25,6 +25,11 @@ class Browser */ private $prefix; + /** + * @var string + */ + private $app_client; + /** * @var Client */ @@ -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; } /** @@ -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) { diff --git a/tests/DependencyInjection/AnimeDbShikimoriBrowserExtensionTest.php b/tests/DependencyInjection/AnimeDbShikimoriBrowserExtensionTest.php index 01cee8a..8bd62dd 100644 --- a/tests/DependencyInjection/AnimeDbShikimoriBrowserExtensionTest.php +++ b/tests/DependencyInjection/AnimeDbShikimoriBrowserExtensionTest.php @@ -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', ], ]; } @@ -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 @@ -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()) diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index 82a1c14..f722984 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -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()); @@ -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()); } } diff --git a/tests/Service/BrowserTest.php b/tests/Service/BrowserTest.php index 3b85889..bc6b294 100644 --- a/tests/Service/BrowserTest.php +++ b/tests/Service/BrowserTest.php @@ -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 */ @@ -55,7 +60,7 @@ 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() @@ -63,27 +68,102 @@ 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], ], ];