Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #145 from helpscout/143-client-header
User agent now provides php version and SDK version numbers
  • Loading branch information
bkuhl committed May 9, 2019
2 parents 473b839 + 5be818e commit 9c1b679
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Http/RestClient.php
Expand Up @@ -6,6 +6,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use HelpScout\Api\ApiClient;
use HelpScout\Api\Entity\Extractable;
use HelpScout\Api\Http\Hal\HalDeserializer;
use HelpScout\Api\Http\Hal\HalResource;
Expand Down Expand Up @@ -58,10 +59,13 @@ public function getAuthHeader(): array
/**
* @return array
*/
protected function getDefaultHeaders(): array
public function getDefaultHeaders(): array
{
return array_merge(
['Content-Type' => self::CONTENT_TYPE],
[
'Content-Type' => self::CONTENT_TYPE,
'User-Agent' => sprintf(self::CLIENT_USER_AGENT, ApiClient::CLIENT_VERSION, phpversion()),
],
$this->getAuthHeader()
);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Http/RestClientTest.php
Expand Up @@ -9,6 +9,7 @@
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use HelpScout\Api\ApiClient;
use HelpScout\Api\Http\Auth\ClientCredentials;
use HelpScout\Api\Http\Auth\LegacyCredentials;
use HelpScout\Api\Http\Auth\NullCredentials;
Expand All @@ -34,6 +35,26 @@ public function setUp()
$this->authenticator = \Mockery::mock(Authenticator::class);
}

public function testDefaultHeaders()
{
$this->authenticator->shouldReceive('getAuthHeader')->andReturn([
'Authorization' => 'Bearer 123abc',
]);

$restClient = new RestClient($this->methodsClient, $this->authenticator);

$headers = $restClient->getDefaultHeaders();

$this->assertArrayHasKey('Content-Type', $headers);
$this->assertEquals(RestClient::CONTENT_TYPE, $headers['Content-Type']);

$this->assertArrayHasKey('User-Agent', $headers);
$this->assertEquals('Help Scout PHP API Client/'.ApiClient::CLIENT_VERSION.' (PHP '.phpversion().')', $headers['User-Agent']);

$this->assertArrayHasKey('Authorization', $headers);
$this->assertEquals('Bearer 123abc', $headers['Authorization']);
}

public function testRunReport()
{
$params = new ParameterBag([]);
Expand Down

0 comments on commit 9c1b679

Please sign in to comment.