Skip to content

Commit e9e6d90

Browse files
authored
feat: Add x-goog-api-client header to requests (googleapis#1709)
1 parent ea5c94d commit e9e6d90

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

src/Google/Client.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -804,12 +804,24 @@ public function prepareScopes()
804804
*/
805805
public function execute(RequestInterface $request, $expectedClass = null)
806806
{
807-
$request = $request->withHeader(
808-
'User-Agent',
809-
$this->config['application_name']
810-
. " " . self::USER_AGENT_SUFFIX
811-
. $this->getLibraryVersion()
812-
);
807+
$request = $request
808+
->withHeader(
809+
'User-Agent',
810+
sprintf(
811+
'%s %s%s',
812+
$this->config['application_name'],
813+
self::USER_AGENT_SUFFIX,
814+
$this->getLibraryVersion()
815+
)
816+
)
817+
->withHeader(
818+
'x-goog-api-client',
819+
sprintf(
820+
'gl-php/%s gdcl/%s',
821+
phpversion(),
822+
$this->getLibraryVersion()
823+
)
824+
);
813825

814826
if ($this->config['api_format_v2']) {
815827
$request = $request->withHeader(

tests/Google/ClientTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,4 +707,45 @@ public function testExecuteWithFormat()
707707
$request = new Request('POST', 'http://foo.bar/');
708708
$client->execute($request);
709709
}
710+
711+
public function testExecuteSetsCorrectHeaders()
712+
{
713+
$this->onlyGuzzle6();
714+
715+
$client = new Google_Client();
716+
$guzzle = $this->getMock('GuzzleHttp\Client');
717+
$guzzle->expects($this->once())
718+
->method('send')
719+
->with(
720+
$this->callback(
721+
function (RequestInterface $request) {
722+
$userAgent = sprintf(
723+
'%s%s',
724+
Google_Client::USER_AGENT_SUFFIX,
725+
Google_Client::LIBVER
726+
);
727+
$xGoogApiClient = sprintf(
728+
'gl-php/%s gdcl/%s',
729+
phpversion(),
730+
Google_Client::LIBVER
731+
);
732+
733+
if ($request->getHeaderLine('User-Agent') !== $userAgent) {
734+
return false;
735+
}
736+
737+
if ($request->getHeaderLine('x-goog-api-client') !== $xGoogApiClient) {
738+
return false;
739+
}
740+
741+
return true;
742+
}
743+
)
744+
)->will($this->returnValue(new Response(200, [], null)));
745+
746+
$client->setHttpClient($guzzle);
747+
748+
$request = new Request('POST', 'http://foo.bar/');
749+
$client->execute($request);
750+
}
710751
}

0 commit comments

Comments
 (0)