From 0506a231ec1a5105855f8c44489f15fefefa37b7 Mon Sep 17 00:00:00 2001 From: Romain Beuque Date: Tue, 19 Jan 2016 15:08:20 +0000 Subject: [PATCH 1/3] fix: incorrect query when passed from url and not from content Signed-off-by: Romain Beuque --- src/Api.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Api.php b/src/Api.php index a8e254f..e521cb5 100644 --- a/src/Api.php +++ b/src/Api.php @@ -223,11 +223,16 @@ class Api if (isset($content) && $method == 'GET') { - $queryString = $request->getUri()->getQuery(); + $query_string = $request->getUri()->getQuery(); - $query = false !== strpos($queryString, '&') - ? explode('&', $queryString) - : []; + $query = array(); + if ($query_string != '') { + $queries = explode('&', $query_string); + foreach($queries as $element) { + $key_value_query = explode('=', $element, 2); + $query[$key_value_query[0]] = $key_value_query[1]; + } + } $query = array_merge($query, (array)$content); $query = \GuzzleHttp\Psr7\build_query($query); From 57ba00310db69b171af38db7f503eb5f6abd1b3c Mon Sep 17 00:00:00 2001 From: Romain Beuque Date: Thu, 21 Jan 2016 18:22:06 +0000 Subject: [PATCH 2/3] Added tests for #27 + fix empty comparaison Signed-off-by: Romain Beuque --- src/Api.php | 2 +- tests/ApiTest.php | 87 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/Api.php b/src/Api.php index e521cb5..f34040b 100644 --- a/src/Api.php +++ b/src/Api.php @@ -226,7 +226,7 @@ class Api $query_string = $request->getUri()->getQuery(); $query = array(); - if ($query_string != '') { + if (!empty($query_string)) { $queries = explode('&', $query_string); foreach($queries as $element) { $key_value_query = explode('=', $element, 2); diff --git a/tests/ApiTest.php b/tests/ApiTest.php index a1375bc..f9db6d9 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -30,6 +30,7 @@ namespace Ovh\tests; use GuzzleHttp\Client; use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Psr7\Request; use Ovh\Api; /** @@ -280,4 +281,90 @@ class ApiTest extends \PHPUnit_Framework_TestCase $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); $this->assertEquals($this->consumer_key, $api->getConsumerKey()); } + + + /** + * Test GET query args + */ + public function testGetQueryArgs() + { + $handlerStack = $this->client->getConfig('handler'); + $handlerStack->push(Middleware::mapRequest(function (Request $request) { + if($request->getUri()->getPath() == "/1.0/auth/time") { + return $request; + } + + $query_string = $request->getUri()->getQuery(); + $this->assertEquals($query_string, 'applicationId=49&status=pendingValidation'); + + $request = $request->withUri($request->getUri() + ->withHost('httpbin.org') + ->withPath('/') + ->withQuery('')); + return $request; + })); + //$handlerStack->push(Middleware::mapResponse(function (Response $response) { + // return $response; + //})); + + $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); + $api->get('/me/api/credential?applicationId=49', ['status' => 'pendingValidation']); + } + + /** + * Test GET overlapping query args + */ + public function testGetOverlappingQueryArgs() + { + $handlerStack = $this->client->getConfig('handler'); + $handlerStack->push(Middleware::mapRequest(function (Request $request) { + if($request->getUri()->getPath() == "/1.0/auth/time") { + return $request; + } + + $query_string = $request->getUri()->getQuery(); + $this->assertEquals($query_string, 'applicationId=49&status=expired&test=success'); + + $request = $request->withUri($request->getUri() + ->withHost('httpbin.org') + ->withPath('/') + ->withQuery('')); + return $request; + })); + //$handlerStack->push(Middleware::mapResponse(function (Response $response) { + // return $response; + //})); + + $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); + $api->get('/me/api/credential?applicationId=49&status=pendingValidation', ['status' => 'expired', 'test' => "success"]); + } + + /** + * Test GET boolean query args + */ + public function testGetBooleanQueryArgs() + { + $handlerStack = $this->client->getConfig('handler'); + $handlerStack->push(Middleware::mapRequest(function (Request $request) { + if($request->getUri()->getPath() == "/1.0/auth/time") { + return $request; + } + + $query_string = $request->getUri()->getQuery(); + $this->assertEquals($query_string, 'dryRun=true¬DryRun=false'); + + $request = $request->withUri($request->getUri() + ->withHost('httpbin.org') + ->withPath('/') + ->withQuery('')); + return $request; + })); + //$handlerStack->push(Middleware::mapResponse(function (Response $response) { + // return $response; + //})); + + $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); + $api->get('/me/api/credential', ['dryRun' => true, 'noDryRun' => false]); + } + } From d3bd44ed6864032dbb039dcf5b4005649cc62070 Mon Sep 17 00:00:00 2001 From: Romain Beuque Date: Mon, 1 Feb 2016 10:34:34 +0000 Subject: [PATCH 3/3] fix: hook that modify Guzzle parameters serialization when true/false value Signed-off-by: Romain Beuque --- src/Api.php | 14 ++++++++++++++ tests/ApiTest.php | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Api.php b/src/Api.php index f34040b..139269d 100644 --- a/src/Api.php +++ b/src/Api.php @@ -235,6 +235,20 @@ class Api } $query = array_merge($query, (array)$content); + + // rewrite query args to properly dump true/false parameters + foreach($query as $key => $value) + { + if ($value === false) + { + $query[$key] = "false"; + } + elseif ($value === true) + { + $query[$key] = "true"; + } + } + $query = \GuzzleHttp\Psr7\build_query($query); $url = $request->getUri()->withQuery($query); diff --git a/tests/ApiTest.php b/tests/ApiTest.php index f9db6d9..7c74d34 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -364,7 +364,7 @@ class ApiTest extends \PHPUnit_Framework_TestCase //})); $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); - $api->get('/me/api/credential', ['dryRun' => true, 'noDryRun' => false]); + $api->get('/me/api/credential', ['dryRun' => true, 'notDryRun' => false]); } }