Skip to content

Commit

Permalink
[7.x] Http client query string support (#31996)
Browse files Browse the repository at this point in the history
* Add support for string and array both type for query param.

This gives more control over the URL query params just the way Guzzle is designed and supports.

* Remove unwanted method. No longer needed.

* Add tests
  • Loading branch information
irazasyed authored Mar 17, 2020
1 parent a1a1dbf commit 2c8182c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 16 deletions.
18 changes: 2 additions & 16 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,10 @@ public function beforeSending($callback)
* Issue a GET request to the given URL.
*
* @param string $url
* @param array $query
* @param array|string|null $query
* @return \Illuminate\Http\Client\Response
*/
public function get(string $url, array $query = [])
public function get(string $url, $query = null)
{
return $this->send('GET', $url, [
'query' => $query,
Expand Down Expand Up @@ -468,7 +468,6 @@ public function send(string $method, string $url, array $options = [])
try {
return tap(new Response($this->buildClient()->request($method, $url, $this->mergeOptions([
'laravel_data' => $options[$this->bodyFormat] ?? [],
'query' => $this->parseQueryParams($url),
'on_stats' => function ($transferStats) {
$this->transferStats = $transferStats;
},
Expand Down Expand Up @@ -604,19 +603,6 @@ public function mergeOptions(...$options)
return array_merge_recursive($this->options, ...$options);
}

/**
* Parse the query parameters in the given URL.
*
* @param string $url
* @return array
*/
public function parseQueryParams(string $url)
{
return tap([], function (&$query) use ($url) {
parse_str(parse_url($url, PHP_URL_QUERY), $query);
});
}

/**
* Register a stub callable that will intercept requests and be able to return stub responses.
*
Expand Down
66 changes: 66 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,70 @@ public function testWithCookies()
$this->assertSame('bar', $responseCookie['Value']);
$this->assertSame('https://laravel.com', $responseCookie['Domain']);
}

public function testGetWithArrayQueryParam()
{
$this->factory->fake();

$this->factory->get('http://foo.com/get', ['foo' => 'bar']);

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/get?foo=bar';
});
}

public function testGetWithStringQueryParam()
{
$this->factory->fake();

$this->factory->get('http://foo.com/get', 'foo=bar');

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/get?foo=bar';
});
}

public function testGetWithQuery()
{
$this->factory->fake();

$this->factory->get('http://foo.com/get?foo=bar&page=1');

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/get?foo=bar&page=1';
});
}

public function testGetWithQueryWontEncode()
{
$this->factory->fake();

$this->factory->get('http://foo.com/get?foo;bar;1;5;10&page=1');

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/get?foo;bar;1;5;10&page=1';
});
}

public function testGetWithArrayQueryParamOverwrites()
{
$this->factory->fake();

$this->factory->get('http://foo.com/get?foo=bar&page=1', ['hello' => 'world']);

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/get?hello=world';
});
}

public function testGetWithArrayQueryParamEncodes()
{
$this->factory->fake();

$this->factory->get('http://foo.com/get', ['foo;bar; space test' => 'laravel']);

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/get?foo%3Bbar%3B%20space%20test=laravel';
});
}
}

0 comments on commit 2c8182c

Please sign in to comment.