Skip to content

Commit

Permalink
Add improved null checking when checking error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenmaguire committed Nov 11, 2019
1 parent 13f7c1e commit 56614cd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# Changelog
All Notable changes to `oauth2-linkedin` will be documented in this file

## 5.1.1 - 2019-11-11

### Added
- Improved null checking when checking error responses - thanks @Addvilz.

### Deprecated
- Nothing

### Fixed
- Nothing

### Removed
- Nothing

### Security
- Nothing

## 5.1.0 - 2019-05-22

### Added
Expand Down
6 changes: 3 additions & 3 deletions src/Provider/LinkedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected function checkResponse(ResponseInterface $response, $data)
if ($response->getStatusCode() >= 400) {
throw new IdentityProviderException(
isset($data['message']) ? $data['message'] : $response->getReasonPhrase(),
$data['status'] ?: $response->getStatusCode(),
isset($data['status']) ? $data['status'] : $response->getStatusCode(),
$response
);
}
Expand All @@ -181,8 +181,8 @@ protected function checkResponseUnauthorized(ResponseInterface $response, $data)
{
if (isset($data['status']) && $data['status'] === 403) {
throw new LinkedInAccessDeniedException(
$data['message'] ?: $response->getReasonPhrase(),
$response->getStatusCode(),
isset($data['message']) ? $data['message'] : $response->getReasonPhrase(),
isset($data['status']) ? $data['status'] : $response->getStatusCode(),
$response
);
}
Expand Down
15 changes: 8 additions & 7 deletions test/src/Provider/LinkedInTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<?php namespace League\OAuth2\Client\Test\Provider;

use GuzzleHttp\ClientInterface;
use InvalidArgumentException;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Tool\QueryBuilderTrait;
use Mockery as m;
use Psr\Http\Message\ResponseInterface;

class LinkedinTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -401,13 +398,13 @@ public function testExceptionThrownWhenErrorObjectReceived()
public function testProviderExceptionThrownWhenErrorObjectReceivedWithoutMessage()
{
$statusCode = rand(400,600);
$postResponse = m::mock(ResponseInterface::class);
$postResponse->shouldReceive('getBody')->andReturn('{"status": '.$statusCode.', "serviceErrorCode": 100}');
$postResponse = m::mock('Psr\Http\Message\ResponseInterface');
$postResponse->shouldReceive('getBody')->andReturn('{"serviceErrorCode": 100}');
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$postResponse->shouldReceive('getStatusCode')->andReturn($statusCode);
$postResponse->shouldReceive('getReasonPhrase')->andReturn('mock reason phrase');

$client = m::mock(ClientInterface::class);
$client = m::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')
->times(1)
->andReturn($postResponse);
Expand All @@ -417,7 +414,11 @@ public function testProviderExceptionThrownWhenErrorObjectReceivedWithoutMessage
$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
} catch (\Exception $e) {

$this->assertInstanceOf(IdentityProviderException::class, $e, 'Unexpected exception thrown');
$this->assertInstanceOf(
'League\OAuth2\Client\Provider\Exception\IdentityProviderException',
$e,
'Unexpected exception thrown'
);

$this->assertEquals($e->getMessage(), 'mock reason phrase');
$this->assertEquals($e->getCode(), $statusCode);
Expand Down

0 comments on commit 56614cd

Please sign in to comment.