Skip to content

Commit 45bee28

Browse files
authored
Merge pull request #82 from swisnl/bugfix/base-uri
Do not prepend the base uri if the endpoint is already absolute
2 parents 78423d2 + 5035e43 commit 45bee28

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Client.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ protected function buildRequest(string $method, string $endpoint, $body = null,
195195
*/
196196
protected function getEndpoint(string $endpoint): string
197197
{
198+
if (strpos($endpoint, 'http://') === 0 || strpos($endpoint, 'https://') === 0) {
199+
return $endpoint;
200+
}
201+
198202
return $this->baseUri.$endpoint;
199203
}
200204

tests/ClientTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,38 @@ public function it_builds_requests_without_a_body()
234234
$this->assertInstanceOf(ResponseInterface::class, $response);
235235
$this->assertEquals('', (string) $httpClient->getLastRequest()->getBody());
236236
}
237+
238+
/**
239+
* @test
240+
*/
241+
public function it_prepends_the_base_uri_if_the_endpoint_is_relative()
242+
{
243+
$baseUri = 'http://example.com/api';
244+
$endpoint = '/test/1';
245+
246+
$httpClient = new HttpMockClient();
247+
$client = new Client($httpClient);
248+
$client->setBaseUri($baseUri);
249+
250+
$client->get($endpoint);
251+
252+
$this->assertEquals($baseUri.$endpoint, $httpClient->getLastRequest()->getUri());
253+
}
254+
255+
/**
256+
* @test
257+
*/
258+
public function it_does_not_prepend_the_base_uri_if_the_endpoint_is_already_absolute()
259+
{
260+
$baseUri = 'http://example.com/api';
261+
$endpoint = 'http://foo.bar/test/1';
262+
263+
$httpClient = new HttpMockClient();
264+
$client = new Client($httpClient);
265+
$client->setBaseUri($baseUri);
266+
267+
$client->get($endpoint);
268+
269+
$this->assertEquals($endpoint, $httpClient->getLastRequest()->getUri());
270+
}
237271
}

0 commit comments

Comments
 (0)