Skip to content

Commit

Permalink
Set correct base URL when custom request adapter is used
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndiritu committed Jun 16, 2023
1 parent 1ee374d commit 024324a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/GraphServiceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,20 @@ 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(
RequestAdapter $requestAdapter,
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, [], '', $requestAdapter);
}

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

0 comments on commit 024324a

Please sign in to comment.