diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index d35c2b79a2e7..9a2c06615aab 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -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, @@ -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; }, @@ -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. * diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 990b90b81512..0b197b095271 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -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'; + }); + } }