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

Function TestOAuth2UserAgent

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

Source from the content-addressed store, hash-verified

1924}
1925
1926func TestOAuth2UserAgent(t *testing.T) {
1927 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1928 require.Equalf(t, "myuseragent", r.Header.Get("User-Agent"), "Expected User-Agent header in oauth request to be 'myuseragent', got '%s'", r.Header.Get("User-Agent"))
1929
1930 res, _ := json.Marshal(oauth2TestServerResponse{
1931 AccessToken: "12345",
1932 TokenType: "Bearer",
1933 })
1934 w.Header().Add("Content-Type", "application/json")
1935 _, _ = w.Write(res)
1936 }))
1937 defer ts.Close()
1938
1939 config := DefaultHTTPClientConfig
1940 config.OAuth2 = &OAuth2{
1941 ClientID: "1",
1942 ClientSecret: "2",
1943 Scopes: []string{"A", "B"},
1944 EndpointParams: map[string]string{"hi": "hello"},
1945 TokenURL: ts.URL + "/token",
1946 }
1947
1948 rt, err := NewRoundTripperFromConfig(config, "test_oauth2", WithUserAgent("myuseragent"))
1949 require.NoError(t, err)
1950
1951 client := http.Client{
1952 Transport: rt,
1953 }
1954 resp, err := client.Get(ts.URL)
1955 require.NoError(t, err)
1956
1957 authorization := resp.Request.Header.Get("Authorization")
1958 require.Equalf(t, "Bearer 12345", authorization, "Expected authorization header to be 'Bearer 12345', got '%s'", authorization)
1959}
1960
1961func TestOAuth2DialContextFunc(t *testing.T) {
1962 tokenServerInvoked := false

Callers

nothing calls this directly

Calls 5

WithUserAgentFunction · 0.85
GetMethod · 0.80
AddMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…