Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mutefiRe committed Feb 28, 2024
1 parent 4c90736 commit f16c703
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions injectproxy/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,50 @@ func TestSeries(t *testing.T) {
}
}

// Test if WithModifyProxyRequest is working as expected
// And can correctly set the Authorization header
func TestRequestOverride(t *testing.T) {
m := newMockUpstream(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// Check that the Authorization header is set.
authHeader := req.Header.Get("Authorization")

if authHeader != "Basic anytoken" {
t.Errorf("unexpected Authorization header: %q", authHeader)
}

w.Write(okResponse)
}))
defer m.Close()

modifyProxyRequest := func(req *http.Request) {
req.Header.Set("Authorization", "Basic anytoken")
}
r, err := NewRoutes(m.url, StaticLabelEnforcer{LabelName: proxyLabel, Values: []string{"default"}}, WithEnabledLabelsAPI(), WithModifyProxyRequest(modifyProxyRequest))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

w := httptest.NewRecorder()
u := "http://prometheus.example.com/api/v1/label/__name__/values"
req := httptest.NewRequest("GET", u, nil)

r.ServeHTTP(w, req)

resp := w.Result()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d", resp.StatusCode)
}
if string(body) != string(okResponse) {
t.Fatalf("unexpected response body: %q", string(body))
}
}

func TestSeriesWithPost(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down

0 comments on commit f16c703

Please sign in to comment.