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

[10.x] Add withQueryParameters to the HTTP client #47297

Merged
merged 3 commits into from
Jun 22, 2023
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
15 changes: 15 additions & 0 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,21 @@ public function bodyFormat(string $format)
});
}

/**
* Set the given query parameters in the request URI.
*
* @param array $parameters
* @return $this
*/
public function withQueryParameters(array $parameters)
{
return tap($this, function () use ($parameters) {
$this->options = array_merge_recursive($this->options, [
'query' => $parameters,
]);
});
}

/**
* Specify the request's content type.
*
Expand Down
58 changes: 58 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,64 @@ public function testWithCookies()
$this->assertSame('https://laravel.com', $responseCookie['Domain']);
}

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

$this->factory->withQueryParameters(
['foo' => 'bar']
)->get('https://laravel.com');

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

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

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

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'https://laravel.com?foo%5B0%5D=bar&foo%5B1%5D=baz';
});
}

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

$this->factory->withQueryParameters(
['foo' => 'bar']
)->get('https://laravel.com', [
'baz' => 'qux',
]);

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'https://laravel.com?foo=bar&baz=qux';
});
}

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

$this->factory->withQueryParameters([
'foo' => 'bar',
'baz' => 'baz',
])->get('https://laravel.com', [
// Override the previously set value
'baz' => 'qux',
]);

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'https://laravel.com?foo=bar&baz=qux';
});
}

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