From ed616784189ccc8329d54e00c96d1b036e9d2f2a Mon Sep 17 00:00:00 2001 From: Andrew Hunt Date: Mon, 18 Nov 2019 16:39:01 -0500 Subject: [PATCH 1/2] Build URL with multiple instances of the same param --- lib/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Client.php b/lib/Client.php index b15595c..c114687 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -352,7 +352,7 @@ private function buildUrl($queryParams = null) { $path = '/' . implode('/', $this->path); if (isset($queryParams)) { - $path .= '?' . http_build_query($queryParams); + $path .= '?' . preg_replace('/%5B(?:\d|[1-9]\d+)%5D=/', '=', http_build_query($queryParams)); } return sprintf('%s%s%s', $this->host, $this->version ?: '', $path); From 4db779671fb5aa81377f058377c6e502483d5611 Mon Sep 17 00:00:00 2001 From: Andrew Hunt Date: Mon, 18 Nov 2019 17:04:12 -0500 Subject: [PATCH 2/2] Test coverage and docs for multiple instances of same URL arg --- USAGE.md | 17 +++++++++++++++++ lib/Client.php | 3 +++ test/unit/ClientTest.php | 16 ++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/USAGE.md b/USAGE.md index abf346a..d979639 100644 --- a/USAGE.md +++ b/USAGE.md @@ -50,6 +50,23 @@ echo $response->body(); echo $response->headers(); ``` +#### GET with array of values + +``` +$query_params = [ + 'aggregated_by' => 'month', + 'subusers' => ['one', 'two', 'three'], + 'start_date' => '2019-01-01', + 'end_date' => '2019-01-31', +]; +$request_headers = ['X-Mock: 200']; +$retryOnLimit = true; +$response = $client->subusers()->stats()->get(null, $query_params, $request_headers, $retryOnLimit); +echo $response->statusCode(); +echo $response->body(); +echo $response->headers(); +``` + ## DELETE diff --git a/lib/Client.php b/lib/Client.php index c114687..bf36535 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -346,12 +346,15 @@ public function setIsConcurrentRequest($isConcurrent) * * @param array $queryParams an array of all the query parameters * + * Nested arrays will resolve to multiple instances of the same parameter + * * @return string */ private function buildUrl($queryParams = null) { $path = '/' . implode('/', $this->path); if (isset($queryParams)) { + // Regex replaces `[0]=`, `[1]=`, etc. with `=`. $path .= '?' . preg_replace('/%5B(?:\d|[1-9]\d+)%5D=/', '=', http_build_query($queryParams)); } diff --git a/test/unit/ClientTest.php b/test/unit/ClientTest.php index 0d829a4..41f2ca8 100644 --- a/test/unit/ClientTest.php +++ b/test/unit/ClientTest.php @@ -218,6 +218,22 @@ public function testMakeRequestWithUntrustedRootCert() $client->makeRequest('GET', 'https://untrusted-root.badssl.com/'); } + public function testFormRepeatUrlArgs() + { + $client = new Client('https://localhost:4010'); + + $testParams = [ + 'thing' => 'stuff', + 'foo' => [ + 'bar', + 'bat', + 'baz', + ], + ]; + $result = $this->callMethod($client, 'buildUrl', [$testParams]); + $this->assertEquals($result, 'https://localhost:4010/?thing=stuff&foo=bar&foo=bat&foo=baz'); + } + /** * @param object $obj * @param string $name