From fd06f3923e1783d1848f7e22acde0bc0b8d80cc6 Mon Sep 17 00:00:00 2001 From: z38 Date: Mon, 24 Jul 2017 23:38:18 +0200 Subject: [PATCH 1/2] Pass relative path to HTTP adapters --- lib/Tmdb/Client.php | 4 ++-- lib/Tmdb/HttpClient/Adapter/GuzzleAdapter.php | 1 + lib/Tmdb/HttpClient/HttpClient.php | 9 +++------ test/Tmdb/Tests/TestCase.php | 13 ++++++++++--- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/Tmdb/Client.php b/lib/Tmdb/Client.php index fb5c038c..ca7b640e 100644 --- a/lib/Tmdb/Client.php +++ b/lib/Tmdb/Client.php @@ -169,7 +169,7 @@ public function setOptions(array $options = []) /** * Construct the http client * - * In case you are implementing your own adapter, the base url will be passed on through the $parameters array + * In case you are implementing your own adapter, the base url will be passed on through the options bag * at every call in the respective get / post methods etc. of the adapter. * * @return void @@ -334,7 +334,7 @@ protected function postResolve(array $options = []) if (!$this->options['adapter']) { $this->options['adapter'] = new GuzzleAdapter( - new \GuzzleHttp\Client(['base_url' => $this->options['base_url']]) + new \GuzzleHttp\Client() ); } diff --git a/lib/Tmdb/HttpClient/Adapter/GuzzleAdapter.php b/lib/Tmdb/HttpClient/Adapter/GuzzleAdapter.php index 466362c7..187b0210 100644 --- a/lib/Tmdb/HttpClient/Adapter/GuzzleAdapter.php +++ b/lib/Tmdb/HttpClient/Adapter/GuzzleAdapter.php @@ -104,6 +104,7 @@ public function getConfiguration(Request $request) $this->request = $request; return [ + 'base_uri' => $request->getOptions()->get('base_url'), 'headers' => $request->getHeaders()->all(), 'query' => $request->getParameters()->all() ]; diff --git a/lib/Tmdb/HttpClient/HttpClient.php b/lib/Tmdb/HttpClient/HttpClient.php index d22130e3..80a85d89 100644 --- a/lib/Tmdb/HttpClient/HttpClient.php +++ b/lib/Tmdb/HttpClient/HttpClient.php @@ -62,6 +62,8 @@ class HttpClient * The base url to built requests on top of * * @var null + * + * @deprecated since 2.1.10, to be removed in 3.0. Use `base_url` of the request options instead. */ protected $base_url = null; @@ -97,11 +99,6 @@ public function __construct( $this->processOptions(); } - private function constructSecureUrl($path) - { - return 'https://' . $this->base_url . $path; - } - /** * {@inheritDoc} */ @@ -229,7 +226,7 @@ public function setBaseUrl($url) private function send($path, $method, array $parameters = [], array $headers = [], $body = null) { $request = $this->createRequest( - $this->constructSecureUrl($path), + $path, $method, $parameters, $headers, diff --git a/test/Tmdb/Tests/TestCase.php b/test/Tmdb/Tests/TestCase.php index 4b338c8f..55785cfb 100644 --- a/test/Tmdb/Tests/TestCase.php +++ b/test/Tmdb/Tests/TestCase.php @@ -138,14 +138,14 @@ protected function getMockedHttpClient(array $methods = []) /** * Get the expected request that will deliver a response * - * @param $path + * @param string $url * @param array $parameters * @param string $method * @param array $headers * @param null $body * @return Request */ - protected function getRequest($path, $parameters = [], $method = 'GET', $headers = [], $body = null) + protected function getRequest($url, $parameters = [], $method = 'GET', $headers = [], $body = null) { if ( $method == 'POST' || @@ -161,6 +161,13 @@ protected function getRequest($path, $parameters = [], $method = 'GET', $headers $headers['Accept'] = 'application/json'; $headers['User-Agent'] = sprintf('wtfzdotnet/php-tmdb-api (v%s)', Client::VERSION); + $baseUrl = 'https://api.themoviedb.org/3/'; + if (strpos($url, $baseUrl) === 0) { + $path = substr($url, strlen($baseUrl)); + } else { + $path = $url; + } + $request = new Request( $path, $method, @@ -186,7 +193,7 @@ protected function getRequest($path, $parameters = [], $method = 'GET', $headers ], 'adapter' => $this->createMock('Tmdb\HttpClient\Adapter\AdapterInterface'), 'host' => 'api.themoviedb.org/3/', - 'base_url' => 'https://api.themoviedb.org/3/', + 'base_url' => $baseUrl, 'session_token' => null, 'event_dispatcher' => $this->eventDispatcher ])); From f6493b2fbe2b6d537ed08b12a8cec237c5edbbda Mon Sep 17 00:00:00 2001 From: Yury Buldakov Date: Sat, 9 Sep 2017 07:20:17 +0300 Subject: [PATCH 2/2] Test `secure` client option is respected --- test/Tmdb/Tests/ClientTest.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/Tmdb/Tests/ClientTest.php b/test/Tmdb/Tests/ClientTest.php index 19d848ea..11db1675 100644 --- a/test/Tmdb/Tests/ClientTest.php +++ b/test/Tmdb/Tests/ClientTest.php @@ -89,6 +89,29 @@ public function assertInstances() ); } + /** + * @test + */ + public function shouldRespectSecureClientOption() + { + $token = new ApiToken(self::API_TOKEN); + + $client = new \Tmdb\Client($token); + $options = $client->getOptions(); + $this->assertTrue(true === $options['secure']); + $this->assertTrue(false !== strpos($options['base_url'], 'https://')); + + $client = new \Tmdb\Client($token, ['secure' => true]); + $options = $client->getOptions(); + $this->assertTrue(true === $options['secure']); + $this->assertTrue(false !== strpos($options['base_url'], 'https://')); + + $client = new \Tmdb\Client($token, ['secure' => false]); + $options = $client->getOptions(); + $this->assertTrue(false === $options['secure']); + $this->assertTrue(false !== strpos($options['base_url'], 'http://')); + } + public function testShouldSwitchHttpScheme() { $token = new ApiToken(self::API_TOKEN);