Skip to content

Commit

Permalink
Ensure correct invite url
Browse files Browse the repository at this point in the history
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
johanib committed Nov 27, 2024
1 parent c651c57 commit 1612a09
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ public function __construct(
],
];

$baseUri = InviteHttpClientUrlSanitizer::buildBaseUri($path, $host);

$this->httpClient = HttpClient::createForBaseUri(
$host . $path,
$baseUri,
$options
);
}
Expand All @@ -54,6 +56,7 @@ public function __construct(
*/
public function post(string $path, array $payload): ResponseInterface
{
$path = InviteHttpClientUrlSanitizer::ensureRelativePath($path);
return $this->httpClient->request(
'POST',
$path,
Expand All @@ -66,6 +69,7 @@ public function post(string $path, array $payload): ResponseInterface
*/
public function delete(string $path): ResponseInterface
{
$path = InviteHttpClientUrlSanitizer::ensureRelativePath($path);
return $this->httpClient->request(
'DELETE',
$path,
Expand Down
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, '/') . '/';
}
}
64 changes: 64 additions & 0 deletions tests/unit/Infrastructure/Invite/InviteHttpClientTest.php
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);
}
}

0 comments on commit 1612a09

Please sign in to comment.