-
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.
Merge pull request #1323 from SURFnet/feature/fix-invite-url
Ensure correct invite url
- Loading branch information
Showing
3 changed files
with
125 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
56 changes: 56 additions & 0 deletions
56
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,56 @@ | ||
<?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 | ||
{ | ||
if (trim($path) === '') { | ||
return self::ensureEndsWithSingleSlash($host); | ||
} | ||
|
||
$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, '/') . '/'; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
tests/unit/Infrastructure/Invite/InviteHttpClientUrlSanitizerTest.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,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 InviteHttpClientUrlSanitizerTest extends TestCase | ||
{ | ||
|
||
public function testHandlesBaseUriCorrectly(): void | ||
{ | ||
$baseUri = InviteHttpClientUrlSanitizer::buildBaseUri('/api/external/v1', 'http://localhost'); | ||
self::assertSame('http://localhost/api/external/v1/', $baseUri); | ||
} | ||
|
||
public function testEnsuresRelativePathCorrectly(): void | ||
{ | ||
$relativePath = InviteHttpClientUrlSanitizer::ensureRelativePath('/internal/delete'); | ||
self::assertSame('internal/delete', $relativePath); | ||
} | ||
|
||
public function testHandlesEmptyPathInBaseUri(): void | ||
{ | ||
$baseUri = InviteHttpClientUrlSanitizer::buildBaseUri('', 'http://localhost'); | ||
self::assertSame('http://localhost/', $baseUri); | ||
} | ||
|
||
public function testHandlesEmptyHostInBaseUri(): void | ||
{ | ||
$baseUri = InviteHttpClientUrlSanitizer::buildBaseUri('/api/external/v1', ''); | ||
self::assertSame('/api/external/v1/', $baseUri); | ||
} | ||
|
||
public function testHandlesEmptyPathInRelativePath(): void | ||
{ | ||
$relativePath = InviteHttpClientUrlSanitizer::ensureRelativePath(''); | ||
self::assertSame('', $relativePath); | ||
} | ||
|
||
public function testCombinesBaseUriAndRelativePathCorrectly(): 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); | ||
} | ||
} |