MCPcopy Create free account
hub / github.com/prometheus/common / TestOAuth2WithFile

Function TestOAuth2WithFile

config/http_config_test.go:1545–1655  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1543}
1544
1545func TestOAuth2WithFile(t *testing.T) {
1546 var expectedAuth *string
1547 var previousAuth string
1548 tokenTS := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1549 auth := r.Header.Get("Authorization")
1550 if auth != *expectedAuth {
1551 t.Fatalf("bad auth, expected %s, got %s", *expectedAuth, auth)
1552 }
1553 if auth == previousAuth {
1554 t.Fatal("token endpoint called twice")
1555 }
1556 previousAuth = auth
1557 res, _ := json.Marshal(oauth2TestServerResponse{
1558 AccessToken: "12345",
1559 TokenType: "Bearer",
1560 })
1561 w.Header().Add("Content-Type", "application/json")
1562 _, _ = w.Write(res)
1563 }))
1564 defer tokenTS.Close()
1565 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1566 auth := r.Header.Get("Authorization")
1567 if auth != "Bearer 12345" {
1568 t.Fatalf("bad auth, expected %s, got %s", "Bearer 12345", auth)
1569 }
1570 fmt.Fprintln(w, "Hello, client")
1571 }))
1572 defer ts.Close()
1573
1574 secretFile, err := os.CreateTemp("", "oauth2_secret")
1575 if err != nil {
1576 t.Fatal(err)
1577 }
1578 defer os.Remove(secretFile.Name())
1579
1580 yamlConfig := fmt.Sprintf(`
1581client_id: 1
1582client_secret_file: %s
1583scopes:
1584 - A
1585 - B
1586token_url: %s
1587endpoint_params:
1588 hi: hello
1589`, secretFile.Name(), tokenTS.URL)
1590 expectedConfig := OAuth2{
1591 ClientID: "1",
1592 ClientSecretFile: secretFile.Name(),
1593 Scopes: []string{"A", "B"},
1594 EndpointParams: map[string]string{"hi": "hello"},
1595 TokenURL: tokenTS.URL,
1596 }
1597
1598 var unmarshalledConfig OAuth2
1599 err = yaml.Unmarshal([]byte(yamlConfig), &unmarshalledConfig)
1600 if err != nil {
1601 t.Fatalf("Expected no error unmarshalling yaml, got %v", err)
1602 }

Callers

nothing calls this directly

Calls 5

NewOAuth2RoundTripperFunction · 0.85
GetMethod · 0.80
AddMethod · 0.80
CloseMethod · 0.65
NameMethod · 0.45

Tested by

no test coverage detected