Skip to content

Commit

Permalink
fix: validated query url after adding applying global params (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
asadali214 authored Feb 26, 2024
1 parent 0ccfb70 commit 2236fa7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 2236fa7

Please sign in to comment.