Skip to content

Commit

Permalink
Adds service url with port support
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejlew committed Mar 22, 2024
1 parent 37a0550 commit 6d4b620
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/Curl/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/Infrastructure/HttpClient/Decorator/BaseUriDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down

0 comments on commit 6d4b620

Please sign in to comment.