public function testCreateBearerReason()
 {
     $statusCode = 403;
     $headers = ['WWW-Authenticate' => 'Bearer realm="myprotectedresource", ' . 'error="insufficient_scope", error_description="Insufficient scope for this resource scopes"'];
     $reason = 'foo-bar';
     $request = new Request('GET', '/sample');
     $response = new Response($statusCode, $headers, null, '1.1', $reason);
     $exception = BearerErrorResponseException::create($request, $response);
     $this->assertInstanceOf(BearerErrorResponseException::class, $exception);
     $this->assertSame('insufficient_scope', $exception->getBearerReason());
 }
 /**
  * @param RequestInterface $request
  * @param array            $options
  *
  * @return PromiseInterface
  */
 public function __invoke(RequestInterface $request, array $options)
 {
     $nextHandler = $this->nextHandler;
     if ($request->hasHeader('Authorization')) {
         return $nextHandler($request, $options);
     }
     $request = $request->withHeader('Authorization', sprintf('Bearer %s', $this->session->getToken()));
     return $nextHandler($request, $options)->then(function (ResponseInterface $response) use($request, $options) {
         $code = $response->getStatusCode();
         if ($code < 400) {
             return $response;
         }
         if ($response->hasHeader("WWW-Authenticate")) {
             throw BearerErrorResponseException::create($request, $response);
         }
         throw RequestException::create($request, $response);
     });
 }