-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prior to this change, the api call to invite would fail because the relative path would collapse. This change ensures the baseUri and call path are absolute/relative when needed so the calls get executed as expected. See https://symfony.com/doc/7.2/reference/configuration/framework.html#reference-http-client-base-uri
- Loading branch information
Showing
3 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/Surfnet/ServiceProviderDashboard/Infrastructure/Invite/InviteHttpClientUrlSanitizer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2024 SURFnet B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Surfnet\ServiceProviderDashboard\Infrastructure\Invite; | ||
|
||
class InviteHttpClientUrlSanitizer | ||
{ | ||
|
||
public static function buildBaseUri(string $path, string $host): string | ||
{ | ||
$path = self::ensureRelativePath($path); | ||
$path = self::ensureEndsWithSingleSlash($path); | ||
|
||
$host = self::ensureEndsWithSingleSlash($host); | ||
|
||
return $host . $path; | ||
} | ||
|
||
/** | ||
* Ensure the relative path does not start with a slash. | ||
* The client basePath needs to end with a slash or the part after the host will get silently removed. | ||
* And the paths in the post/delete methods need to be relative or the path in the basePath will not get used. | ||
* | ||
* See https://symfony.com/doc/7.2/reference/configuration/framework.html#base-uri | ||
* @param string $path | ||
* @return string | ||
*/ | ||
public static function ensureRelativePath(string $path): string | ||
{ | ||
return ltrim($path, '/'); | ||
} | ||
|
||
private static function ensureEndsWithSingleSlash(string $string): string | ||
{ | ||
return rtrim($string, '/') . '/'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2024 SURFnet B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Surfnet\ServiceProviderDashboard\Tests\Unit\Infrastructure\Invite; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Surfnet\ServiceProviderDashboard\Infrastructure\Invite\InviteHttpClientUrlSanitizer; | ||
|
||
class InviteHttpClientTest extends TestCase | ||
{ | ||
|
||
public function handlesBaseUriCorrectly(): void | ||
{ | ||
$baseUri = InviteHttpClientUrlSanitizer::buildBaseUri('/api/external/v1', 'http://localhost'); | ||
self::assertSame('http://localhost/api/external/v1/', $baseUri); | ||
} | ||
|
||
public function ensuresRelativePathCorrectly(): void | ||
{ | ||
$relativePath = InviteHttpClientUrlSanitizer::ensureRelativePath('/internal/delete'); | ||
self::assertSame('internal/delete', $relativePath); | ||
} | ||
|
||
public function handlesEmptyPathInBaseUri(): void | ||
{ | ||
$baseUri = InviteHttpClientUrlSanitizer::buildBaseUri('', 'http://localhost'); | ||
self::assertSame('http://localhost/', $baseUri); | ||
} | ||
|
||
public function handlesEmptyHostInBaseUri(): void | ||
{ | ||
$baseUri = InviteHttpClientUrlSanitizer::buildBaseUri('/api/external/v1', ''); | ||
self::assertSame('/api/external/v1/', $baseUri); | ||
} | ||
|
||
public function handlesEmptyPathInRelativePath(): void | ||
{ | ||
$relativePath = InviteHttpClientUrlSanitizer::ensureRelativePath(''); | ||
self::assertSame('', $relativePath); | ||
} | ||
|
||
public function combinesBaseUriAndRelativePathCorrectly(): void | ||
{ | ||
$baseUri = InviteHttpClientUrlSanitizer::buildBaseUri('/api/external/v1', 'http://localhost'); | ||
$relativePath = InviteHttpClientUrlSanitizer::ensureRelativePath('/internal/delete'); | ||
$fullUri = $baseUri . $relativePath; | ||
self::assertSame('http://localhost/api/external/v1/internal/delete', $fullUri); | ||
} | ||
} |