diff --git a/src/Client.php b/src/Client.php index 49f3eb9..55d2577 100644 --- a/src/Client.php +++ b/src/Client.php @@ -87,11 +87,9 @@ public function __construct( public function getGlobalRequest(?string $server = null): Request { - $request = new Request($this->serverUrls[$server ?? $this->defaultServer], $this); - $paramGroup = new MultipleParams('Global Parameters'); - $paramGroup->parameters($this->globalConfig)->validate(self::getJsonHelper($this)); - $paramGroup->apply($request); - return $request; + $globalParams = new MultipleParams('Global Parameters'); + $globalParams->parameters($this->globalConfig)->validate(self::getJsonHelper($this)); + return new Request($this->serverUrls[$server ?? $this->defaultServer], $this, $globalParams); } public function getGlobalResponseHandler(): ResponseHandler diff --git a/src/Request/Request.php b/src/Request/Request.php index 6132bd7..35ef30a 100644 --- a/src/Request/Request.php +++ b/src/Request/Request.php @@ -6,6 +6,7 @@ use Closure; use Core\Client; +use Core\Request\Parameters\MultipleParams; use Core\Types\Sdk\CoreFileWrapper; use Core\Utils\CoreHelper; use CoreInterfaces\Core\Format; @@ -29,10 +30,14 @@ class Request implements RequestSetterInterface /** * Creates a new Request object. */ - public function __construct(string $queryUrl, ?Client $client = null) + public function __construct(string $queryUrl, ?Client $client = null, ?MultipleParams $globalParams = null) { - $this->queryUrl = CoreHelper::validateUrl($queryUrl); + $this->queryUrl = $queryUrl; $this->converter = Client::getConverter($client); + if ($globalParams != null) { + $globalParams->apply($this); + } + $this->queryUrl = CoreHelper::validateUrl($this->queryUrl); } /** diff --git a/tests/ClientTest.php b/tests/ClientTest.php index bac9955..d5f0e84 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -241,4 +241,30 @@ public function testStrictTypeBodyParamValidation() Client::getJsonHelper(MockHelper::getClient()) ); } + + public function testRequestInitializationWithCustomBaseUrl() + { + $customUrl = 'https://my/path/'; + $customUrlWithoutSlash = 'https://my/path'; + + $client = ClientBuilder::init(new MockHttpClient()) + ->converter(new MockConverter()) + ->apiCallback(MockHelper::getCallbackCatcher()) + ->jsonHelper(MockHelper::getJsonHelper()) + ->serverUrls([ + 'ServerA' => '{custom-url-a}', + 'ServerB' => '{custom-url-b}', + ], 'ServerA') + ->globalConfig([ + TemplateParam::init('custom-url-a', $customUrl)->dontEncode(), + TemplateParam::init('custom-url-b', $customUrlWithoutSlash)->dontEncode() + ]) + ->build(); + + $requestA = $client->getGlobalRequest('ServerA'); + $this->assertEquals($customUrlWithoutSlash, $requestA->getQueryUrl()); + + $requestB = $client->getGlobalRequest('ServerB'); + $this->assertEquals($customUrlWithoutSlash, $requestB->getQueryUrl()); + } }