From 97a70336520361e236d44ebc61d058c1b2deb7fe Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Fri, 16 Jun 2023 12:48:31 +0300 Subject: [PATCH] Set correct base URL when custom request adapter is used --- src/GraphServiceClient.php | 10 ++++++---- tests/GraphServiceClientTest.php | 22 ++++++++++++++++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/GraphServiceClient.php b/src/GraphServiceClient.php index 2662d26afeb..be54046617a 100644 --- a/src/GraphServiceClient.php +++ b/src/GraphServiceClient.php @@ -53,8 +53,8 @@ public function __construct( * Get an instance of GraphServiceClient that uses $requestAdapter * * @param RequestAdapter $requestAdapter - * @param string $nationalCloud Defaults to https://graph.microsoft.com. See - * https://learn.microsoft.com/en-us/graph/deployments + * @param string $nationalCloud Used to build base URL of $requestAdapter if none has been specified + * Defaults to https://graph.microsoft.com. See https://learn.microsoft.com/en-us/graph/deployments * @return GraphServiceClient */ public static function createWithRequestAdapter( @@ -62,9 +62,11 @@ public static function createWithRequestAdapter( string $nationalCloud = NationalCloud::GLOBAL ): GraphServiceClient { - $requestAdapter->setBaseUrl($nationalCloud); + if (!$requestAdapter->getBaseUrl()) { + $requestAdapter->setBaseUrl("$nationalCloud/v1.0"); + } $placeholder = new ClientCredentialContext('tenant', 'client', 'secret'); - return new GraphServiceClient($placeholder, [], $nationalCloud, $requestAdapter); + return new GraphServiceClient($placeholder, [], 'placeholder', $requestAdapter); } /** diff --git a/tests/GraphServiceClientTest.php b/tests/GraphServiceClientTest.php index 598fdc446bf..2d430e8c14f 100644 --- a/tests/GraphServiceClientTest.php +++ b/tests/GraphServiceClientTest.php @@ -2,8 +2,7 @@ namespace Microsoft\Graph\Test; -use Microsoft\Graph\Core\BaseGraphRequestAdapter; -use Microsoft\Graph\GraphRequestAdapter; +use Microsoft\Graph\Core\NationalCloud; use Microsoft\Graph\GraphServiceClient; use Microsoft\Kiota\Abstractions\Authentication\AnonymousAuthenticationProvider; use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext; @@ -22,4 +21,23 @@ public function testsInit(): void $client = new GraphServiceClient($testContext, ['Users.Read']); $this->assertInstanceOf(GraphServiceClient::class, $client); } + + public function testCorrectDefaultBaseUrlIsSetWhenRequestAdapterHasEmptyBaseUrl(): void + { + $requestAdapter = new GuzzleRequestAdapter(new AnonymousAuthenticationProvider()); + $client = GraphServiceClient::createWithRequestAdapter($requestAdapter); + $this->assertEquals(NationalCloud::GLOBAL.'/v1.0', $client->getRequestAdapter()->getBaseUrl()); + + $requestAdapter = new GuzzleRequestAdapter(new AnonymousAuthenticationProvider()); + $client = GraphServiceClient::createWithRequestAdapter($requestAdapter, NationalCloud::CHINA); + $this->assertEquals(NationalCloud::CHINA.'/v1.0', $client->getRequestAdapter()->getBaseUrl()); + } + + public function testCustomBaseUrlIsNotOverriden(): void + { + $requestAdapter = new GuzzleRequestAdapter(new AnonymousAuthenticationProvider()); + $requestAdapter->setBaseUrl(NationalCloud::CHINA); + $client = GraphServiceClient::createWithRequestAdapter($requestAdapter, NationalCloud::US_DOD); + $this->assertEquals(NationalCloud::CHINA, $client->getRequestAdapter()->getBaseUrl()); + } }