Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Core/GraphConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

final class GraphConstants
{
const BETA_API_VERSION = "beta";
const V1_API_VERSION = "v1.0";

// These can be overwritten in setters in the Graph object
const REST_ENDPOINT = "https://graph.microsoft.com/";

Expand Down
6 changes: 5 additions & 1 deletion src/Http/GraphRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,11 @@ public function upload(string $path, ?ClientInterface $client = null)
private function initHeaders(string $baseUrl): void
{
$coreSdkVersion = "graph-php-core/".GraphConstants::SDK_VERSION;
$serviceLibSdkVersion = "Graph-php-".$this->graphClient->getSdkVersion();
if ($this->graphClient->getApiVersion() === GraphConstants::BETA_API_VERSION) {
$serviceLibSdkVersion = "graph-php-beta/".$this->graphClient->getSdkVersion();
} else {
$serviceLibSdkVersion = "graph-php/".$this->graphClient->getSdkVersion();
}
if (NationalCloud::containsNationalCloudHost($this->requestUri)) {
$this->headers = [
'Content-Type' => 'application/json',
Expand Down
16 changes: 14 additions & 2 deletions tests/Http/Request/GraphRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,26 @@ public function testConstructorGivenInvalidFullEndpointUriAppendsItToDefaultBase
public function testConstructorSetsExpectedHeadersGivenValidGraphBaseUrl(): void {
$expectedHeaders = [
'Content-Type' => ['application/json'],
'SdkVersion' => ["graph-php-core/".GraphConstants::SDK_VERSION.", Graph-php-".$this->mockGraphClient->getSdkVersion()],
'SdkVersion' => ["graph-php-core/".GraphConstants::SDK_VERSION.", graph-php/".$this->mockGraphClient->getSdkVersion()],
'Authorization' => ['Bearer ' . $this->mockGraphClient->getAccessToken()],
'Host' => [substr($this->mockGraphClient->getNationalCloud(), strlen("https://"))]
];
$request = new GraphRequest("GET", "/me", $this->mockGraphClient);
$this->assertEquals($expectedHeaders, $request->getHeaders());
}

public function testConstructorSetsExpectedBetaSdkVersionHeader(): void {
$graphClient = $this->createMock(AbstractGraphClient::class);
$graphClient->method('getAccessToken')->willReturn("abc");
$graphClient->method('getNationalCloud')->willReturn(NationalCloud::GLOBAL);
$graphClient->method('getSdkVersion')->willReturn('2.0.0');
$graphClient->method('getApiVersion')->willReturn(GraphConstants::BETA_API_VERSION);

$request = new GraphRequest("GET", "/me", $graphClient);
$expected = ["graph-php-core/".GraphConstants::SDK_VERSION.", graph-php-beta/".$graphClient->getSdkVersion()];
$this->assertEquals($expected, $request->getHeaders()["SdkVersion"]);
}

public function testConstructorSetsExpectedHeadersGivenValidCustomBaseUrl(): void {
$baseUrl = "https://www.outlook.com";
$expectedHeaders = [
Expand All @@ -113,7 +125,7 @@ public function testConstructorSetsExpectedHeadersGivenGraphEndpointUrl(): void
$endpoint = "https://graph.microsoft.com/v1.0/me/users\$skip=10&\$top=5";
$expectedHeaders = [
'Content-Type' => ['application/json'],
'SdkVersion' => ["graph-php-core/".GraphConstants::SDK_VERSION.", Graph-php-".$this->mockGraphClient->getSdkVersion()],
'SdkVersion' => ["graph-php-core/".GraphConstants::SDK_VERSION.", graph-php/".$this->mockGraphClient->getSdkVersion()],
'Authorization' => ['Bearer ' . $this->mockGraphClient->getAccessToken()],
'Host' => [substr($this->mockGraphClient->getNationalCloud(), strlen("https://"))]
];
Expand Down