| @@ 217-275 (lines=59) @@ | ||
| 214 | $this->assertEquals('1453720507', $token->getToken()); |
|
| 215 | } |
|
| 216 | ||
| 217 | public function testGetTokenShouldGetNewTokenIfCachedTokenIsNotValid() |
|
| 218 | { |
|
| 219 | $mockHandler = new MockHandler( |
|
| 220 | [ |
|
| 221 | function (RequestInterface $request) { |
|
| 222 | ||
| 223 | $this->assertTrue($request->hasHeader('timeout')); |
|
| 224 | $this->assertEquals( |
|
| 225 | 3, |
|
| 226 | $request->getHeaderLine('timeout') |
|
| 227 | ); |
|
| 228 | ||
| 229 | return new Response( |
|
| 230 | 200, |
|
| 231 | ['Content-Type' => 'application/json'], |
|
| 232 | json_encode(['token' => '1453720507']) |
|
| 233 | ); |
|
| 234 | }, |
|
| 235 | function (RequestInterface $request) { |
|
| 236 | ||
| 237 | $this->assertTrue($request->hasHeader('timeout')); |
|
| 238 | $this->assertEquals( |
|
| 239 | 3, |
|
| 240 | $request->getHeaderLine('timeout') |
|
| 241 | ); |
|
| 242 | ||
| 243 | return new Response( |
|
| 244 | 200, |
|
| 245 | ['Content-Type' => 'application/json'], |
|
| 246 | json_encode(['token' => 'foo123']) |
|
| 247 | ); |
|
| 248 | }, |
|
| 249 | ] |
|
| 250 | ); |
|
| 251 | ||
| 252 | $handler = HandlerStack::create($mockHandler); |
|
| 253 | ||
| 254 | $authClient = new Client([ |
|
| 255 | 'handler' => $handler, |
|
| 256 | ]); |
|
| 257 | ||
| 258 | $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']); |
|
| 259 | ||
| 260 | $jwtManager = new JwtManager( |
|
| 261 | $authClient, |
|
| 262 | $authStrategy, |
|
| 263 | null, |
|
| 264 | ['token_url' => '/api/token', 'timeout' => 3] |
|
| 265 | ); |
|
| 266 | $token = $jwtManager->getJwtToken(); |
|
| 267 | ||
| 268 | $this->assertInstanceOf(JwtToken::class, $token); |
|
| 269 | $this->assertEquals('1453720507', $token->getToken()); |
|
| 270 | ||
| 271 | $token = $jwtManager->getJwtToken(); |
|
| 272 | ||
| 273 | $this->assertInstanceOf(JwtToken::class, $token); |
|
| 274 | $this->assertEquals('foo123', $token->getToken()); |
|
| 275 | } |
|
| 276 | ||
| 277 | public function testGetTokenShouldUseTheCachedTokenIfItIsValid() |
|
| 278 | { |
|
| @@ 277-335 (lines=59) @@ | ||
| 274 | $this->assertEquals('foo123', $token->getToken()); |
|
| 275 | } |
|
| 276 | ||
| 277 | public function testGetTokenShouldUseTheCachedTokenIfItIsValid() |
|
| 278 | { |
|
| 279 | $mockHandler = new MockHandler( |
|
| 280 | [ |
|
| 281 | function (RequestInterface $request) { |
|
| 282 | ||
| 283 | $this->assertTrue($request->hasHeader('timeout')); |
|
| 284 | $this->assertEquals( |
|
| 285 | 3, |
|
| 286 | $request->getHeaderLine('timeout') |
|
| 287 | ); |
|
| 288 | ||
| 289 | return new Response( |
|
| 290 | 200, |
|
| 291 | ['Content-Type' => 'application/json'], |
|
| 292 | json_encode(['token' => '1453720507', 'expires_in' => 3600]) |
|
| 293 | ); |
|
| 294 | }, |
|
| 295 | function (RequestInterface $request) { |
|
| 296 | ||
| 297 | $this->assertTrue($request->hasHeader('timeout')); |
|
| 298 | $this->assertEquals( |
|
| 299 | 3, |
|
| 300 | $request->getHeaderLine('timeout') |
|
| 301 | ); |
|
| 302 | ||
| 303 | return new Response( |
|
| 304 | 200, |
|
| 305 | ['Content-Type' => 'application/json'], |
|
| 306 | json_encode(['token' => 'foo123']) |
|
| 307 | ); |
|
| 308 | }, |
|
| 309 | ] |
|
| 310 | ); |
|
| 311 | ||
| 312 | $handler = HandlerStack::create($mockHandler); |
|
| 313 | ||
| 314 | $authClient = new Client([ |
|
| 315 | 'handler' => $handler, |
|
| 316 | ]); |
|
| 317 | ||
| 318 | $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']); |
|
| 319 | ||
| 320 | $jwtManager = new JwtManager( |
|
| 321 | $authClient, |
|
| 322 | $authStrategy, |
|
| 323 | null, |
|
| 324 | ['token_url' => '/api/token', 'timeout' => 3] |
|
| 325 | ); |
|
| 326 | $token = $jwtManager->getJwtToken(); |
|
| 327 | ||
| 328 | $this->assertInstanceOf(JwtToken::class, $token); |
|
| 329 | $this->assertEquals('1453720507', $token->getToken()); |
|
| 330 | ||
| 331 | $token = $jwtManager->getJwtToken(); |
|
| 332 | ||
| 333 | $this->assertInstanceOf(JwtToken::class, $token); |
|
| 334 | $this->assertEquals('1453720507', $token->getToken()); |
|
| 335 | } |
|
| 336 | ||
| 337 | public function testGetTokenShouldUseTheCachedTokenIfItIsValidBasedOnExpField() |
|
| 338 | { |
|