TestGetRequestMetadataCacheExpiry verifies the case where the cached access token has expired, and the credentials implementation will have to send a fresh token exchange request.
(t *testing.T)
| 302 | // token has expired, and the credentials implementation will have to send a |
| 303 | // fresh token exchange request. |
| 304 | func (s) TestGetRequestMetadataCacheExpiry(t *testing.T) { |
| 305 | const expiresInSecs = 1 |
| 306 | defer overrideSubjectTokenGood()() |
| 307 | fc := &testutils.FakeHTTPClient{ |
| 308 | ReqChan: testutils.NewChannel(), |
| 309 | RespChan: testutils.NewChannel(), |
| 310 | } |
| 311 | defer overrideHTTPClient(fc)() |
| 312 | |
| 313 | creds, err := NewCredentials(goodOptions) |
| 314 | if err != nil { |
| 315 | t.Fatalf("NewCredentials(%v) = %v", goodOptions, err) |
| 316 | } |
| 317 | |
| 318 | // The fakeClient is configured to return an access_token with a one second |
| 319 | // expiry. So, in the second iteration, the credentials will find the cache |
| 320 | // entry, but that would have expired, and therefore we expect it to send |
| 321 | // out a fresh request. |
| 322 | for i := 0; i < 2; i++ { |
| 323 | errCh := make(chan error, 1) |
| 324 | go receiveAndCompareRequest(fc.ReqChan, errCh) |
| 325 | |
| 326 | respJSON, _ := json.Marshal(responseParameters{ |
| 327 | AccessToken: accessTokenContents, |
| 328 | IssuedTokenType: "urn:ietf:params:oauth:token-type:access_token", |
| 329 | TokenType: "Bearer", |
| 330 | ExpiresIn: expiresInSecs, |
| 331 | }) |
| 332 | respBody := io.NopCloser(bytes.NewReader(respJSON)) |
| 333 | resp := &http.Response{ |
| 334 | Status: "200 OK", |
| 335 | StatusCode: http.StatusOK, |
| 336 | Body: respBody, |
| 337 | } |
| 338 | fc.RespChan.Send(resp) |
| 339 | |
| 340 | ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 341 | defer cancel() |
| 342 | gotMetadata, err := creds.GetRequestMetadata(createTestContext(ctx, credentials.PrivacyAndIntegrity), "") |
| 343 | if err != nil { |
| 344 | t.Fatalf("creds.GetRequestMetadata() = %v", err) |
| 345 | } |
| 346 | if !cmp.Equal(gotMetadata, goodMetadata) { |
| 347 | t.Fatalf("creds.GetRequestMetadata() = %v, want %v", gotMetadata, goodMetadata) |
| 348 | } |
| 349 | if err := <-errCh; err != nil { |
| 350 | t.Fatal(err) |
| 351 | } |
| 352 | time.Sleep(expiresInSecs * time.Second) |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // TestGetRequestMetadataBadResponses verifies the scenario where the token |
| 357 | // exchange server returns bad responses. |
nothing calls this directly
no test coverage detected