Skip to content

Commit bc151a0

Browse files
committed
Test coverage and docs for multiple instances of same URL arg
1 parent eec9f7d commit bc151a0

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

USAGE.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ echo $response->body();
5050
echo $response->headers();
5151
```
5252

53+
#### GET with array of values
54+
55+
```
56+
$query_params = [
57+
'aggregated_by' => 'month',
58+
'subusers' => ['one', 'two', 'three'],
59+
'start_date' => '2019-01-01',
60+
'end_date' => '2019-01-31',
61+
];
62+
$request_headers = ['X-Mock: 200'];
63+
$retryOnLimit = true;
64+
$response = $client->subusers()->stats()->get(null, $query_params, $request_headers, $retryOnLimit);
65+
echo $response->statusCode();
66+
echo $response->body();
67+
echo $response->headers();
68+
```
69+
5370
<a name="delete"></a>
5471
## DELETE
5572

lib/Client.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,15 @@ public function setIsConcurrentRequest($isConcurrent)
324324
* Build the final URL to be passed
325325
*
326326
* @param array $queryParams an array of all the query parameters
327+
* Nested arrays will resolve to multiple instances of the same parameter
327328
*
328329
* @return string
329330
*/
330331
private function buildUrl($queryParams = null)
331332
{
332333
$path = '/' . implode('/', $this->path);
333334
if (isset($queryParams)) {
335+
// Regex replaces `[0]=`, `[1]=`, etc. with `=`.
334336
$path .= '?' . preg_replace('/%5B(?:\d|[1-9]\d+)%5D=/', '=', http_build_query($queryParams));
335337
}
336338
return sprintf('%s%s%s', $this->host, $this->version ?: '', $path);

test/unit/ClientTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,22 @@ public function testThrowExceptionOnInvalidCall()
204204
$client->get();
205205
}
206206

207+
public function testFormRepeatUrlArgs()
208+
{
209+
$client = new Client('https://localhost:4010');
210+
211+
$testParams = [
212+
'thing' => 'stuff',
213+
'foo' => [
214+
'bar',
215+
'bat',
216+
'baz',
217+
],
218+
];
219+
$result = $this->callMethod($client, 'buildUrl', [$testParams]);
220+
$this->assertEquals($result, 'https://localhost:4010/?thing=stuff&foo=bar&foo=bat&foo=baz');
221+
}
222+
207223
/**
208224
* @param object $obj
209225
* @param string $name

0 commit comments

Comments
 (0)