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