Skip to content

Commit d19adbb

Browse files
committed
Fixed leading slash when the base url is empty
1 parent e3a4f1e commit d19adbb

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Helpers/URLHelper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public static function join(string $baseUrl, string $endpoint): string
3434
$requiresTrailingSlash = ! empty($endpoint) && $endpoint !== '/';
3535

3636
$baseEndpoint = rtrim($baseUrl, '/ ');
37-
$baseEndpoint = $requiresTrailingSlash ? $baseEndpoint . '/' : $baseEndpoint;
37+
38+
$endpointIsNotUrl = empty(filter_var($endpoint, FILTER_VALIDATE_URL));
39+
$glue = $endpointIsNotUrl ? '/' : null;
40+
41+
$baseEndpoint = $requiresTrailingSlash ? $baseEndpoint . $glue : $baseEndpoint;
3842

3943
return $baseEndpoint . $endpoint;
4044
}

tests/Unit/URLHelperTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use Sammyjo20\Saloon\Helpers\URLHelper;
4+
5+
test('the URL helper will join two URLs together', function ($baseUrl, $endpoint, $expected) {
6+
expect(URLHelper::join($baseUrl, $endpoint))->toEqual($expected);
7+
})->with([
8+
['https://google.com', '/search', 'https://google.com/search'],
9+
['https://google.com', 'search', 'https://google.com/search'],
10+
['https://google.com/', '/search', 'https://google.com/search'],
11+
['https://google.com/', 'search', 'https://google.com/search'],
12+
['https://google.com//', '//search', 'https://google.com/search'],
13+
['', 'https://google.com/search', 'https://google.com/search'],
14+
['', 'google.com/search', '/google.com/search'],
15+
]);

0 commit comments

Comments
 (0)