From d9b7ab3bbdf0efdf24fc7354c1e01a04e4733262 Mon Sep 17 00:00:00 2001 From: Michael Crumm Date: Fri, 18 Mar 2016 11:08:20 -0700 Subject: [PATCH] fix(credentials): Rewind the keyFileStream before each read --- src/RequestWrapper.php | 1 + tests/RequestWrapperTest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/RequestWrapper.php b/src/RequestWrapper.php index 27a8ca78d22..c38313156ad 100644 --- a/src/RequestWrapper.php +++ b/src/RequestWrapper.php @@ -173,6 +173,7 @@ public function getCredentialsFetcher() } if ($this->keyFileStream) { + $this->keyFileStream->rewind(); return CredentialsLoader::makeCredentials($this->scopes, $this->keyFileStream); } diff --git a/tests/RequestWrapperTest.php b/tests/RequestWrapperTest.php index 54b7d8bde10..c09d05b93fa 100644 --- a/tests/RequestWrapperTest.php +++ b/tests/RequestWrapperTest.php @@ -104,6 +104,19 @@ public function testCredentialsFetcher($wrapperConfig) ); } + /** + * @dataProvider keyFileCredentialsProvider + */ + public function testCredentialsFromKeyFileStreamCanBeReadMultipleTimes($wrapperConfig) + { + $requestWrapper = new RequestWrapper($wrapperConfig); + + $requestWrapper->getCredentialsFetcher(); + $credentials = $requestWrapper->getCredentialsFetcher(); + + $this->assertInstanceOf('Google\Auth\FetchAuthTokenInterface', $credentials); + } + public function credentialsProvider() { $config = [ @@ -130,6 +143,25 @@ public function credentialsProvider() ]; } + public function keyFileCredentialsProvider() + { + $config = [ + 'authHttpHandler' => function ($request, $options = []) { + return new Response(200, [], json_encode(['access_token' => 'abc'])); + }, + 'httpHandler' => function ($request, $options = []) { + return new Response(200, []); + } + ]; + + $keyFilePath = __DIR__ . '/fixtures/json-key-fixture.json'; + + return [ + [$config + ['keyFile' => file_get_contents($keyFilePath)]], // keyFile + [$config + ['keyFilePath' => $keyFilePath]], //keyFilePath + ]; + } + public function testAddsUserAgentToRequest() { $requestWrapper = new RequestWrapper([