From 30b901a6e645fde5fd66102e9e0c023a8bfc404a Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 14 May 2021 09:54:52 +0200 Subject: [PATCH] [HttpClient] fix adding query string to relative URLs with scoped clients --- ScopingHttpClient.php | 5 +++-- Tests/ScopingHttpClientTest.php | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ScopingHttpClient.php b/ScopingHttpClient.php index ea60fd4..3dcde64 100644 --- a/ScopingHttpClient.php +++ b/ScopingHttpClient.php @@ -71,11 +71,12 @@ public function request(string $method, string $url, array $options = []): Respo throw $e; } - $options = self::mergeDefaultOptions($options, $this->defaultOptionsByRegexp[$this->defaultRegexp], true); + $defaultOptions = $this->defaultOptionsByRegexp[$this->defaultRegexp]; + $options = self::mergeDefaultOptions($options, $defaultOptions, true); if (\is_string($options['base_uri'] ?? null)) { $options['base_uri'] = self::parseUrl($options['base_uri']); } - $url = implode('', self::resolveUrl($url, $options['base_uri'] ?? null)); + $url = implode('', self::resolveUrl($url, $options['base_uri'] ?? null, $defaultOptions['query'] ?? [])); } foreach ($this->defaultOptionsByRegexp as $regexp => $defaultOptions) { diff --git a/Tests/ScopingHttpClientTest.php b/Tests/ScopingHttpClientTest.php index 27fe23e..bfca02b 100644 --- a/Tests/ScopingHttpClientTest.php +++ b/Tests/ScopingHttpClientTest.php @@ -30,9 +30,9 @@ public function testRelativeUrl() public function testRelativeUrlWithDefaultRegexp() { $mockClient = new MockHttpClient(); - $client = new ScopingHttpClient($mockClient, ['.*' => ['base_uri' => 'http://example.com']], '.*'); + $client = new ScopingHttpClient($mockClient, ['.*' => ['base_uri' => 'http://example.com', 'query' => ['a' => 'b']]], '.*'); - $this->assertSame('http://example.com/foo', $client->request('GET', '/foo')->getInfo('url')); + $this->assertSame('http://example.com/foo?f=g&a=b', $client->request('GET', '/foo?f=g')->getInfo('url')); } /**