| @@ 81-134 (lines=54) @@ | ||
| 78 | /** |
|
| 79 | * testGetTokenWithSublevelResponse |
|
| 80 | */ |
|
| 81 | public function testGetTokenWithSublevelResponse() |
|
| 82 | { |
|
| 83 | $mockHandler = new MockHandler([ |
|
| 84 | function (RequestInterface $request) { |
|
| 85 | ||
| 86 | $this->assertTrue($request->hasHeader('timeout')); |
|
| 87 | $this->assertEquals( |
|
| 88 | 3, |
|
| 89 | $request->getHeaderLine('timeout') |
|
| 90 | ); |
|
| 91 | ||
| 92 | $jsonPayload = <<<EOF |
|
| 93 | { |
|
| 94 | "status": "success", |
|
| 95 | "message": "Login successful", |
|
| 96 | "payload": { |
|
| 97 | "token": "1453720507" |
|
| 98 | }, |
|
| 99 | "expires_in": 3600 |
|
| 100 | } |
|
| 101 | EOF; |
|
| 102 | ||
| 103 | return new Response( |
|
| 104 | 200, |
|
| 105 | ['Content-Type' => 'application/json'], |
|
| 106 | $jsonPayload |
|
| 107 | ); |
|
| 108 | }, |
|
| 109 | ]); |
|
| 110 | ||
| 111 | $handler = HandlerStack::create($mockHandler); |
|
| 112 | ||
| 113 | $authClient = new Client([ |
|
| 114 | 'handler' => $handler, |
|
| 115 | ]); |
|
| 116 | ||
| 117 | $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']); |
|
| 118 | ||
| 119 | $jwtManager = new JwtManager( |
|
| 120 | $authClient, |
|
| 121 | $authStrategy, |
|
| 122 | null, |
|
| 123 | [ |
|
| 124 | 'token_url' => '/api/token', |
|
| 125 | 'timeout' => 3, |
|
| 126 | 'token_key' => 'payload.token', |
|
| 127 | 'expire_key' => 'expires_in' |
|
| 128 | ] |
|
| 129 | ); |
|
| 130 | $token = $jwtManager->getJwtToken(); |
|
| 131 | ||
| 132 | $this->assertInstanceOf(JwtToken::class, $token); |
|
| 133 | $this->assertEquals('1453720507', $token->getToken()); |
|
| 134 | } |
|
| 135 | ||
| 136 | /** |
|
| 137 | * testGetToken. |
|
| @@ 139-176 (lines=38) @@ | ||
| 136 | /** |
|
| 137 | * testGetToken. |
|
| 138 | */ |
|
| 139 | public function testGetToken() |
|
| 140 | { |
|
| 141 | $mockHandler = new MockHandler([ |
|
| 142 | function (RequestInterface $request) { |
|
| 143 | ||
| 144 | $this->assertTrue($request->hasHeader('timeout')); |
|
| 145 | $this->assertEquals( |
|
| 146 | 3, |
|
| 147 | $request->getHeaderLine('timeout') |
|
| 148 | ); |
|
| 149 | ||
| 150 | return new Response( |
|
| 151 | 200, |
|
| 152 | ['Content-Type' => 'application/json'], |
|
| 153 | json_encode(['token' => '1453720507', 'expires_in' => 3600]) |
|
| 154 | ); |
|
| 155 | }, |
|
| 156 | ]); |
|
| 157 | ||
| 158 | $handler = HandlerStack::create($mockHandler); |
|
| 159 | ||
| 160 | $authClient = new Client([ |
|
| 161 | 'handler' => $handler, |
|
| 162 | ]); |
|
| 163 | ||
| 164 | $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']); |
|
| 165 | ||
| 166 | $jwtManager = new JwtManager( |
|
| 167 | $authClient, |
|
| 168 | $authStrategy, |
|
| 169 | null, |
|
| 170 | ['token_url' => '/api/token', 'timeout' => 3] |
|
| 171 | ); |
|
| 172 | $token = $jwtManager->getJwtToken(); |
|
| 173 | ||
| 174 | $this->assertInstanceOf(JwtToken::class, $token); |
|
| 175 | $this->assertEquals('1453720507', $token->getToken()); |
|
| 176 | } |
|
| 177 | ||
| 178 | public function testGetTokenWithTokenKeyOption() |
|
| 179 | { |
|
| @@ 178-215 (lines=38) @@ | ||
| 175 | $this->assertEquals('1453720507', $token->getToken()); |
|
| 176 | } |
|
| 177 | ||
| 178 | public function testGetTokenWithTokenKeyOption() |
|
| 179 | { |
|
| 180 | $mockHandler = new MockHandler([ |
|
| 181 | function (RequestInterface $request) { |
|
| 182 | ||
| 183 | $this->assertTrue($request->hasHeader('timeout')); |
|
| 184 | $this->assertEquals( |
|
| 185 | 3, |
|
| 186 | $request->getHeaderLine('timeout') |
|
| 187 | ); |
|
| 188 | ||
| 189 | return new Response( |
|
| 190 | 200, |
|
| 191 | ['Content-Type' => 'application/json'], |
|
| 192 | json_encode(['tokenkey' => '1453720507']) |
|
| 193 | ); |
|
| 194 | }, |
|
| 195 | ]); |
|
| 196 | ||
| 197 | $handler = HandlerStack::create($mockHandler); |
|
| 198 | ||
| 199 | $authClient = new Client([ |
|
| 200 | 'handler' => $handler, |
|
| 201 | ]); |
|
| 202 | ||
| 203 | $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']); |
|
| 204 | ||
| 205 | $jwtManager = new JwtManager( |
|
| 206 | $authClient, |
|
| 207 | $authStrategy, |
|
| 208 | null, |
|
| 209 | ['token_url' => '/api/token', 'timeout' => 3, 'token_key' => 'tokenkey'] |
|
| 210 | ); |
|
| 211 | $token = $jwtManager->getJwtToken(); |
|
| 212 | ||
| 213 | $this->assertInstanceOf(JwtToken::class, $token); |
|
| 214 | $this->assertEquals('1453720507', $token->getToken()); |
|
| 215 | } |
|
| 216 | ||
| 217 | public function testGetTokenShouldGetNewTokenIfCachedTokenIsNotValid() |
|
| 218 | { |
|