1
0
mirror of https://github.com/ovh/php-ovh.git synced 2023-11-05 03:20:26 +01:00

Merge pull request #27 from Rbeuque74/fixMultipleQueryString

fix: incorrect query when passed from url and not from content
This commit is contained in:
Vincent Cassé 2016-02-10 14:16:38 +01:00
commit 25a97501a1
2 changed files with 110 additions and 4 deletions

View File

@ -223,13 +223,32 @@ class Api
if (isset($content) && $method == 'GET') { if (isset($content) && $method == 'GET') {
$queryString = $request->getUri()->getQuery(); $query_string = $request->getUri()->getQuery();
$query = false !== strpos($queryString, '&') $query = array();
? explode('&', $queryString) if (!empty($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 = 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); $query = \GuzzleHttp\Psr7\build_query($query);
$url = $request->getUri()->withQuery($query); $url = $request->getUri()->withQuery($query);

View File

@ -30,6 +30,7 @@ namespace Ovh\tests;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Middleware; use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use Ovh\Api; 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); $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client);
$this->assertEquals($this->consumer_key, $api->getConsumerKey()); $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&notDryRun=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, 'notDryRun' => false]);
}
} }