diff --git a/src/RequestWrapper.php b/src/RequestWrapper.php index 27a8ca78d229..c38313156ad8 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 54b7d8bde106..c09d05b93fac 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([