Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Http client query string support #31996

Merged
merged 3 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to be able to get query params using a method. 🤔 should I do it? @taylorotwell

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized it can be done with $request->data()['foo'] == 'bar'. My bad.

});
}
}