Пример #1
0
 /**
  * Called when the middleware is handled by the client.
  *
  * @param callable $handler
  *
  * @return callable
  */
 public function __invoke(callable $handler)
 {
     $token = $this->jwtManager->getJwtToken()->getToken();
     return function (RequestInterface $request, array $options) use($handler, $token) {
         return $handler($request->withHeader('Authorization', sprintf(self::AUTH_BEARER, $token)), $options);
     };
 }
 public function testGetTokenShouldUseTheCachedTokenIfItIsValid()
 {
     $mockHandler = new MockHandler([function (RequestInterface $request) {
         $this->assertTrue($request->hasHeader('timeout'));
         $this->assertEquals(3, $request->getHeaderLine('timeout'));
         return new Response(200, ['Content-Type' => 'application/json'], json_encode(['token' => '1453720507', 'expires_in' => 3600]));
     }, function (RequestInterface $request) {
         $this->assertTrue($request->hasHeader('timeout'));
         $this->assertEquals(3, $request->getHeaderLine('timeout'));
         return new Response(200, ['Content-Type' => 'application/json'], json_encode(['token' => 'foo123']));
     }]);
     $handler = HandlerStack::create($mockHandler);
     $authClient = new Client(['handler' => $handler]);
     $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
     $jwtManager = new JwtManager($authClient, $authStrategy, ['token_url' => '/api/token', 'timeout' => 3]);
     $token = $jwtManager->getJwtToken();
     $this->assertInstanceOf(JwtToken::class, $token);
     $this->assertEquals('1453720507', $token->getToken());
     $token = $jwtManager->getJwtToken();
     $this->assertInstanceOf(JwtToken::class, $token);
     $this->assertEquals('1453720507', $token->getToken());
 }