Skip to content

Commit

Permalink
Resolved #951
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Oct 22, 2017
1 parent e2de73d commit 1b448aa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/Kernel/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,23 @@ public function refresh(): AccessTokenInterface

/**
* @param array $credentials
*
* @param bool $toArray
*
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
*/
public function requestToken(array $credentials, $toArray = false)
{
$response = $this->sendRequest($credentials);
$result = json_decode($response->getBody()->getContents(), true);
$formatted = $this->resolveResponse($response, $this->app['config']->get('response_type', 'array'));

if (empty($result[$this->tokenKey])) {
throw new HttpException('Request access_token fail: '.json_encode($result, JSON_UNESCAPED_UNICODE), $response);
throw new HttpException('Request access_token fail: '.json_encode($result, JSON_UNESCAPED_UNICODE), $response, $formatted);
}

return $toArray ? $result : $this->resolveResponse($response, $this->app['config']->get('response_type', 'array'));
return $toArray ? $result : $formatted;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Kernel/Exceptions/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,25 @@ class HttpException extends Exception
*/
public $response;

/**
* @var \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*/
public $formattedResponse;

/**
* HttpException constructor.
*
* @param string $message
* @param \Psr\Http\Message\ResponseInterface|null $response
* @param null $formattedResponse
* @param int|null $code
*/
public function __construct($message, ResponseInterface $response = null, $code = null)
public function __construct($message, ResponseInterface $response = null, $formattedResponse = null, $code = null)
{
parent::__construct($message, $code);

$this->response = $response;
$this->formattedResponse = $formattedResponse;

if ($response) {
$response->getBody()->rewind();
Expand Down
11 changes: 8 additions & 3 deletions tests/Kernel/AccessTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,14 @@ public function testRequestToken()
$response = new Response(200, [], '{"error_msg":"mock-error-message"}');
$token->expects()->sendRequest($credentials)->andReturn($response)->once();

$this->expectException(HttpException::class);
$this->expectExceptionMessage('Request access_token fail: {"error_msg":"mock-error-message"}');
$token->requestToken($credentials);
try {
$token->requestToken($credentials);
} catch (\Exception $e) {
$this->assertInstanceOf(HttpException::class, $e);
$this->assertSame('Request access_token fail: {"error_msg":"mock-error-message"}', $e->getMessage());
$this->assertInstanceOf(Collection::class, $e->formattedResponse);
$this->assertSame('mock-error-message', $e->formattedResponse->get('error_msg'));
}
}

public function testApplyToRequest()
Expand Down

0 comments on commit 1b448aa

Please sign in to comment.