diff --git a/CHANGELOG.md b/CHANGELOG.md index a46c0c1..b9497f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ## [Unreleased] ### Added - service url scheme and host validation +- service url with port support ### Changed - PSR-18 HTTP client related exceptions namespace moved diff --git a/src/Curl/HttpClient.php b/src/Curl/HttpClient.php index 2ef3ca6..7bfadcb 100644 --- a/src/Curl/HttpClient.php +++ b/src/Curl/HttpClient.php @@ -34,7 +34,13 @@ public function sendRequest(RequestInterface $request): ResponseInterface private function prepareRequestHttpClient(RequestInterface $request) { - $url = sprintf("%s://%s%s", $request->getUri()->getScheme(), $request->getUri()->getHost(), $request->getRequestTarget()); + $url = strtr("{scheme}://{host}{port}{path}", [ + '{scheme}' => $request->getUri()->getScheme(), + '{host}' => $request->getUri()->getHost(), + '{port}' => $request->getUri()->getPort() ? ':' . $request->getUri()->getPort() : '', + '{path}' => $request->getRequestTarget() + ]); + $httpClient = curl_init($url); if ($httpClient === false) { diff --git a/src/Infrastructure/HttpClient/Decorator/BaseUriDecorator.php b/src/Infrastructure/HttpClient/Decorator/BaseUriDecorator.php index 1fa91f9..79cc2fc 100644 --- a/src/Infrastructure/HttpClient/Decorator/BaseUriDecorator.php +++ b/src/Infrastructure/HttpClient/Decorator/BaseUriDecorator.php @@ -42,10 +42,12 @@ private function prependBaseUri(RequestInterface $request): RequestInterface $scheme = $baseUriParts['scheme'] ?? ''; $host = $baseUriParts['host'] ?? ''; + $port = $baseUriParts['port'] ?? null; $basePath = $baseUriParts['path'] ?? ''; $basePath = rtrim($basePath, '/'); $uri = $uri->withPath($basePath . '/' . $uri->getPath()); + $uri = $uri->withPort($port); $uri = $uri->withHost($host); $uri = $uri->withScheme($scheme); diff --git a/tests/Unit/Infrastructure/HttpClient/Decorator/BaseUriDecoratorTest.php b/tests/Unit/Infrastructure/HttpClient/Decorator/BaseUriDecoratorTest.php index 677852f..0e538bd 100644 --- a/tests/Unit/Infrastructure/HttpClient/Decorator/BaseUriDecoratorTest.php +++ b/tests/Unit/Infrastructure/HttpClient/Decorator/BaseUriDecoratorTest.php @@ -19,8 +19,8 @@ class BaseUriDecoratorTest extends TestCase * ["http://example.com", "http://example.com/endpoint", "http"] * ["any://example.com", "any://example.com/endpoint", "any"] * ["any://example.com/base/", "any://example.com/base/endpoint", "any"] - * ["any://example.com:80", "any://example.com/endpoint", "any"] - * ["any://example.com:80/base/", "any://example.com/base/endpoint", "any"] + * ["any://example.com:80", "any://example.com:80/endpoint", "any"] + * ["any://example.com:80/base/", "any://example.com:80/base/endpoint", "any"] */ public function send_request_with_base_schema(string $baseUri, string $expectedRequestUri, string $expectedRequestSchema) { @@ -58,9 +58,9 @@ public function dont_send_request_without_base_schema_or_host(string $baseUri) * @test * @testWith * ["any://example.com", "any://example.com/endpoint", "example.com"] - * ["any://example.com:80", "any://example.com/endpoint", "example.com"] + * ["any://example.com:80", "any://example.com:80/endpoint", "example.com"] * ["any://example", "any://example/endpoint", "example"] - * ["any://example:80", "any://example/endpoint", "example"] + * ["any://example:80", "any://example:80/endpoint", "example"] */ public function send_request_with_base_host(string $baseUri, string $expectedRequestUri, string $expectedRequestHost) { @@ -76,8 +76,8 @@ public function send_request_with_base_host(string $baseUri, string $expectedReq /** * @test * @testWith - * ["any://example.com:80", "any://example.com/endpoint", ""] - * ["any://example:80", "any://example/endpoint", ""] + * ["any://example.com:80", "any://example.com:80/endpoint", "80"] + * ["any://example:80", "any://example:80/endpoint", "80"] */ public function send_request_with_base_port(string $baseUri, string $expectedRequestUri, string $expectedRequestPort) { @@ -113,12 +113,12 @@ public function send_request_without_base_port(string $baseUri, string $expected * @test * @testWith * ["any://example.com/base", "any://example.com/base/endpoint", "/base/endpoint"] - * ["any://example.com:80/base/", "any://example.com/base/endpoint", "/base/endpoint"] - * ["any://example:80/base", "any://example/base/endpoint", "/base/endpoint"] + * ["any://example.com:80/base/", "any://example.com:80/base/endpoint", "/base/endpoint"] + * ["any://example:80/base", "any://example:80/base/endpoint", "/base/endpoint"] * ["any://example.com/base/", "any://example.com/base/endpoint", "/base/endpoint"] * ["any://example/base/", "any://example/base/endpoint", "/base/endpoint"] - * ["any://example.com:80/base/", "any://example.com/base/endpoint", "/base/endpoint"] - * ["any://example:80/base", "any://example/base/endpoint", "/base/endpoint"] + * ["any://example.com:80/base/", "any://example.com:80/base/endpoint", "/base/endpoint"] + * ["any://example:80/base", "any://example:80/base/endpoint", "/base/endpoint"] */ public function send_request_with_base_path(string $baseUri, string $expectedRequestUri, string $expectedRequestPath) { @@ -136,12 +136,12 @@ public function send_request_with_base_path(string $baseUri, string $expectedReq * @testWith * ["any://example.com", "any://example.com/endpoint", "/endpoint"] * ["any://example", "any://example/endpoint", "/endpoint"] - * ["any://example.com:80", "any://example.com/endpoint", "/endpoint"] - * ["any://example:80", "any://example/endpoint", "/endpoint"] + * ["any://example.com:80", "any://example.com:80/endpoint", "/endpoint"] + * ["any://example:80", "any://example:80/endpoint", "/endpoint"] * ["any://example.com/", "any://example.com/endpoint", "/endpoint"] * ["any://example/", "any://example/endpoint", "/endpoint"] - * ["any://example.com:80/", "any://example.com/endpoint", "/endpoint"] - * ["any://example:80/", "any://example/endpoint", "/endpoint"] + * ["any://example.com:80/", "any://example.com:80/endpoint", "/endpoint"] + * ["any://example:80/", "any://example:80/endpoint", "/endpoint"] */ public function send_request_without_base_path(string $baseUri, string $expectedRequestUri, string $expectedRequestPath) {