MCPcopy
hub / github.com/grpc/grpc-go / TestGetRequestMetadataBadResponses

Method TestGetRequestMetadataBadResponses

credentials/sts/sts_test.go:358–410  ·  view source on GitHub ↗

TestGetRequestMetadataBadResponses verifies the scenario where the token exchange server returns bad responses.

(t *testing.T)

Source from the content-addressed store, hash-verified

356// TestGetRequestMetadataBadResponses verifies the scenario where the token
357// exchange server returns bad responses.
358func (s) TestGetRequestMetadataBadResponses(t *testing.T) {
359 tests := []struct {
360 name string
361 response *http.Response
362 }{
363 {
364 name: "bad JSON",
365 response: &http.Response{
366 Status: "200 OK",
367 StatusCode: http.StatusOK,
368 Body: io.NopCloser(strings.NewReader("not JSON")),
369 },
370 },
371 {
372 name: "no access token",
373 response: &http.Response{
374 Status: "200 OK",
375 StatusCode: http.StatusOK,
376 Body: io.NopCloser(strings.NewReader("{}")),
377 },
378 },
379 }
380
381 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
382 defer cancel()
383 for _, test := range tests {
384 t.Run(test.name, func(t *testing.T) {
385 defer overrideSubjectTokenGood()()
386
387 fc := &testutils.FakeHTTPClient{
388 ReqChan: testutils.NewChannel(),
389 RespChan: testutils.NewChannel(),
390 }
391 defer overrideHTTPClient(fc)()
392
393 creds, err := NewCredentials(goodOptions)
394 if err != nil {
395 t.Fatalf("NewCredentials(%v) = %v", goodOptions, err)
396 }
397
398 errCh := make(chan error, 1)
399 go receiveAndCompareRequest(fc.ReqChan, errCh)
400
401 fc.RespChan.Send(test.response)
402 if _, err := creds.GetRequestMetadata(createTestContext(ctx, credentials.PrivacyAndIntegrity), ""); err == nil {
403 t.Fatal("creds.GetRequestMetadata() succeeded when expected to fail")
404 }
405 if err := <-errCh; err != nil {
406 t.Fatal(err)
407 }
408 })
409 }
410}
411
412// TestGetRequestMetadataBadSubjectTokenRead verifies the scenario where the
413// attempt to read the subjectToken fails.

Callers

nothing calls this directly

Calls 10

NewChannelFunction · 0.92
overrideSubjectTokenGoodFunction · 0.85
overrideHTTPClientFunction · 0.85
receiveAndCompareRequestFunction · 0.85
createTestContextFunction · 0.85
NewCredentialsFunction · 0.70
FatalfMethod · 0.65
SendMethod · 0.65
GetRequestMetadataMethod · 0.65
FatalMethod · 0.65

Tested by

no test coverage detected