From 7bb61adc8c81ff1eb45d8a1c9c45554ac568c3c6 Mon Sep 17 00:00:00 2001 From: electron93 Date: Mon, 20 Aug 2018 14:51:35 +0300 Subject: [PATCH] Sometimes the $data argument of the checkResponse() method has a string type If the OAuth server responds with 502 status and non-json content, the parseResponse() method of the AbstractProvider class returns a string instead of an array. --- src/Provider.php | 5 +++-- tests/ProviderTest.php | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Provider.php b/src/Provider.php index 1f98438..93ee2d3 100644 --- a/src/Provider.php +++ b/src/Provider.php @@ -70,8 +70,9 @@ class Provider extends AbstractProvider protected function checkResponse(ResponseInterface $response, $data) { $statusCode = $response->getStatusCode(); - if ($statusCode !== 200 || isset($data['message'])) { - throw new IdentityProviderException($data['message'] ?: $response->getReasonPhrase(), $statusCode, $response); + if ($statusCode !== 200) { + $message = isset($data['message']) ? $data['message'] : $response->getReasonPhrase(); + throw new IdentityProviderException($message, $statusCode, $response); } } diff --git a/tests/ProviderTest.php b/tests/ProviderTest.php index bb40322..62a6f57 100644 --- a/tests/ProviderTest.php +++ b/tests/ProviderTest.php @@ -121,6 +121,28 @@ class ProviderTest extends \PHPUnit_Framework_TestCase $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); } + /** + * @expectedException \Ely\OAuth2\Client\Exception\IdentityProviderException + * @expectedExceptionMessage Bad Gateway + */ + public function testExceptionThrownOnIncorrectContentType() + { + /** @var m\Mock|ResponseInterface $postResponse */ + $postResponse = m::mock(ResponseInterface::class); + $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'text/html; charset=UTF-8']); + $postResponse->shouldReceive('getBody')->andReturn('html content'); + $postResponse->shouldReceive('getStatusCode')->andReturn(502); + $postResponse->shouldReceive('getReasonPhrase')->andReturn('Bad Gateway'); + + /** @var m\Mock|ClientInterface $client */ + $client = m::mock(ClientInterface::class); + $client->shouldReceive('send') + ->times(1) + ->andReturn($postResponse); + $this->provider->setHttpClient($client); + $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); + } + public function testGetResourceOwner() { /** @var m\Mock|ResponseInterface $postResponse */