From 2f041ebb9b43a79b5cc5e10d633f12d26370dcd4 Mon Sep 17 00:00:00 2001 From: Florian Preusner Date: Sun, 11 Dec 2016 14:40:51 +0100 Subject: [PATCH] #77 Workaround/Hotfix: Cannot set headers in config (#78) --- DependencyInjection/Configuration.php | 7 +- DependencyInjection/GuzzleExtension.php | 29 +--- Middleware/RequestHeaderMiddleware.php | 119 ---------------- README.md | 6 +- Resources/config/services.xml | 2 +- .../DependencyInjection/ConfigurationTest.php | 3 + .../GuzzleExtensionTest.php | 1 - .../RequestHeaderMiddlewareTest.php | 134 ------------------ 8 files changed, 21 insertions(+), 280 deletions(-) delete mode 100644 Middleware/RequestHeaderMiddleware.php delete mode 100644 Tests/Middleware/RequestHeaderMiddlewareTest.php diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 007767d..e4a42f4 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -81,6 +81,7 @@ private function createClientsNode() ->children() ->scalarNode('base_url')->defaultValue(null)->end() + // @deprecated ->arrayNode('headers') ->prototype('scalar') ->end() @@ -88,11 +89,15 @@ private function createClientsNode() ->arrayNode('options') ->children() + ->arrayNode('headers') + ->prototype('scalar') + ->end() + ->end() ->arrayNode('auth') ->prototype('scalar') ->end() ->end() - ->arrayNode('query') + ->arrayNode('query') ->prototype('scalar') ->end() ->end() diff --git a/DependencyInjection/GuzzleExtension.php b/DependencyInjection/GuzzleExtension.php index 48df9c0..2e4d454 100644 --- a/DependencyInjection/GuzzleExtension.php +++ b/DependencyInjection/GuzzleExtension.php @@ -64,6 +64,12 @@ public function load(array $configs, ContainerBuilder $container) } } + // header hotfix/workaround #77 + // @deprecated + if (isset($options['headers'])) { + $argument['headers'] = $this->cleanUpHeaders($options['headers']); + } + $client = new Definition('%guzzle.http_client.class%'); $client->addArgument($argument); @@ -90,16 +96,11 @@ protected function createHandler(ContainerBuilder $container, $name, array $conf $log = $this->createLogMiddleware(); $container->setDefinition($logServiceName, $log); - $headerServiceName = sprintf('guzzle_bundle.middleware.request_header.%s', $name); - $requestHeader = $this->createRequestHeaderMiddleware($config['headers']); - $container->setDefinition($headerServiceName, $requestHeader); - // Event Dispatching service $eventServiceName = sprintf('guzzle_bundle.middleware.event_dispatch.%s', $name); $eventService = $this->createEventMiddleware($name); $container->setDefinition($eventServiceName, $eventService); - $headerExpression = new Expression(sprintf("service('%s').attach()", $headerServiceName)); $logExpression = new Expression(sprintf("service('%s').log()", $logServiceName)); // Create the event Dispatch Middleware $eventExpression = new Expression(sprintf("service('%s').dispatchEvent()", $eventServiceName)); @@ -136,7 +137,6 @@ protected function createHandler(ContainerBuilder $container, $name, array $conf } } - $handler->addMethodCall('push', [$headerExpression]); $handler->addMethodCall('push', [$logExpression]); // goes on the end of the stack. $handler->addMethodCall('unshift', [$eventExpression]); @@ -186,23 +186,6 @@ protected function createLogMiddleware() return $log; } - /** - * Create Middleware For Request Headers - * - * @since 2015-07 - * - * @param array $headers - * - * @return Definition - */ - protected function createRequestHeaderMiddleware(array $headers) - { - $requestHeader = new Definition('%guzzle_bundle.middleware.request_header.class%'); - $requestHeader->addArgument($this->cleanUpHeaders($headers)); - - return $requestHeader; - } - /** * Create Middleware For dispatching events * diff --git a/Middleware/RequestHeaderMiddleware.php b/Middleware/RequestHeaderMiddleware.php deleted file mode 100644 index f8d15b1..0000000 --- a/Middleware/RequestHeaderMiddleware.php +++ /dev/null @@ -1,119 +0,0 @@ -setHeaders($headers); - } - - /** - * Retrieve headers that have been set - * - * @version 1.0 - * @since 2013-10 - * - * @return array $headers - */ - public function getHeaders() - { - return $this->headers; - } - - /** - * Set headers - * - * @version 1.0 - * @since 2013-10 - * - * @param array $headers - * @return void - */ - public function setHeaders(array $headers) - { - foreach ($headers as $name => $value) { - - $this->addHeader($name, $value); - } - } - - /** - * @version 1.0 - * @since 2013-10 - * - * @param string $key - * @param string $value - * - * @return void - */ - public function addHeader($key, $value) - { - $this->headers[$key] = $value; - } - - /** - * Get specified header - * - * @version 1.0 - * @since 2013-10 - * - * @param string $key - * @return string - */ - public function getHeader($key) - { - if (array_key_exists($key, $this->headers)) { - - return $this->headers[$key]; - } - - return null; - } - - /** - * Add given headers to request - * - * @version 3.0 - * @since 2015-06 - * - * @return callable - */ - public function attach() - { - return function (callable $handler) { - - return function (RequestInterface $request, array $options) use ($handler) { - - foreach ($this->getHeaders() as $key => $value) { - - $request = $request->withHeader($key, $value); - } - - return $handler($request, $options); - }; - }; - } -} diff --git a/README.md b/README.md index 4fe7f82..38aef42 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ guzzle: api_payment: base_url: "http://api.domain.tld" - # custom headers + # custom headers (@deprecated, will be removed in v5; new: set headers in options) headers: Accept: "application/json" @@ -76,6 +76,10 @@ guzzle: auth: - acme # login - pa55w0rd # password + + headers: + Accept: "application/json" + timeout: 30 # plugin settings diff --git a/Resources/config/services.xml b/Resources/config/services.xml index e86cad1..a0c2bd8 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -11,7 +11,7 @@ EightPoints\Bundle\GuzzleBundle\DataCollector\HttpDataCollector EightPoints\Bundle\GuzzleBundle\Log\Logger EightPoints\Bundle\GuzzleBundle\Middleware\LogMiddleware - EightPoints\Bundle\GuzzleBundle\Middleware\RequestHeaderMiddleware + EightPoints\Bundle\GuzzleBundle\Middleware\EventDispatchMiddleware diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index f7cab99..9658ab1 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -26,6 +26,9 @@ public function testSingleClientConfigWithOptions() 'user', 'pass' ], + 'headers' => [ + 'Accept' => 'application/json' + ], 'query' => [ ], 'cert' => 'path/to/cert', diff --git a/Tests/DependencyInjection/GuzzleExtensionTest.php b/Tests/DependencyInjection/GuzzleExtensionTest.php index dfde12a..49db386 100644 --- a/Tests/DependencyInjection/GuzzleExtensionTest.php +++ b/Tests/DependencyInjection/GuzzleExtensionTest.php @@ -29,7 +29,6 @@ public function testGuzzleExtension() // test Services $this->assertTrue($container->hasDefinition('guzzle_bundle.middleware.log.test_api')); - $this->assertTrue($container->hasDefinition('guzzle_bundle.middleware.request_header.test_api')); $this->assertTrue($container->hasDefinition('guzzle_bundle.middleware.event_dispatch.test_api')); // test WSSE Plugin diff --git a/Tests/Middleware/RequestHeaderMiddlewareTest.php b/Tests/Middleware/RequestHeaderMiddlewareTest.php deleted file mode 100644 index 209958d..0000000 --- a/Tests/Middleware/RequestHeaderMiddlewareTest.php +++ /dev/null @@ -1,134 +0,0 @@ - 'application/json; version=2' - ]; - - $middleware = new RequestHeaderMiddleware($headers); - $returnedHeaders = $middleware->getHeaders(); - - $this->assertSame($headers, $returnedHeaders); - } - - /** - * Test Headers - * - * @version 4.5 - * @since 2016-01 - * - * @covers EightPoints\Bundle\GuzzleBundle\Middleware\RequestHeaderMiddleware::getHeaders - */ - public function testGetHeaders() - { - $headers = [ - 'Accept' => 'application/json; version=2', - 'Accept-Language' => 'de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4', - 'Cache-Control' => 'max-age=0' - ]; - - $middleware = new RequestHeaderMiddleware($headers); - $returnedHeader = $middleware->getHeaders(); - - $this->assertArrayHasKey('Accept', $returnedHeader); - $this->assertArrayHasKey('Accept-Language', $returnedHeader); - $this->assertArrayHasKey('Cache-Control', $returnedHeader); - } - - /** - * Test Headers - * - * @version 4.5 - * @since 2016-01 - * - * @covers EightPoints\Bundle\GuzzleBundle\Middleware\RequestHeaderMiddleware::getHeader - */ - public function testGetSingleHeader() - { - $headers = [ - 'Accept' => 'application/json; version=2', - 'Accept-Language' => 'de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4', - 'Cache-Control' => 'max-age=0' - ]; - - $middleware = new RequestHeaderMiddleware($headers); - - $returnedHeaderValue = $middleware->getHeader('Cache-Control'); - - $this->assertSame('max-age=0', $returnedHeaderValue); - } - - /** - * Test Headers - * - * @version 4.5 - * @since 2016-01 - * - * @covers EightPoints\Bundle\GuzzleBundle\Middleware\RequestHeaderMiddleware::getHeaders - * @covers EightPoints\Bundle\GuzzleBundle\Middleware\RequestHeaderMiddleware::addHeader - */ - public function testAddHeader() - { - $headers = [ - 'Accept' => 'application/json; version=2', - 'Accept-Language' => 'de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4', - 'Cache-Control' => 'max-age=0' - ]; - - $middleware = new RequestHeaderMiddleware($headers); - $middleware->addHeader('Connection', 'keep-alive'); - - $headers['Connection'] = 'keep-alive'; - - $returnedHeader = $middleware->getHeaders(); - - $this->assertSame($headers, $returnedHeader); - } - - /** - * Test Headers - * - * @version 4.5 - * @since 2016-01 - * - * @covers EightPoints\Bundle\GuzzleBundle\Middleware\RequestHeaderMiddleware::setHeaders - */ - public function testSetHeaders() - { - $this->markTestSkipped('needs refactoring cause "set" is actually adding headers'); - - $headers = [ - 'Accept' => 'application/json; version=2', - 'Accept-Language' => 'de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4', - 'Cache-Control' => 'max-age=0' - ]; - - $middleware = new RequestHeaderMiddleware($headers); - - $this->assertSame($headers, $middleware->getHeaders()); - - $newHeader = ['Connection' => 'keep-alive']; - $middleware->setHeaders($newHeader); - - $this->assertSame($newHeader, $middleware->getHeaders()); - } -}