diff --git a/src/Http/RestClient.php b/src/Http/RestClient.php index 57519f61..7ee0bc0f 100644 --- a/src/Http/RestClient.php +++ b/src/Http/RestClient.php @@ -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; @@ -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() ); } diff --git a/tests/Http/RestClientTest.php b/tests/Http/RestClientTest.php index c6a39b69..e1018bb1 100644 --- a/tests/Http/RestClientTest.php +++ b/tests/Http/RestClientTest.php @@ -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; @@ -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([]);