-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frontend: pass the request ID to Clusters Service
This change injects the request ID generated by the RP frontend into all requests made to the CS API. Jira: https://issues.redhat.com/browse/ARO-14904 Signed-off-by: Simon Pasquier <[email protected]>
- Loading branch information
1 parent
bda68e7
commit 6b8e454
Showing
4 changed files
with
97 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package frontend | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/Azure/ARO-HCP/internal/api/arm" | ||
"github.com/google/uuid" | ||
) | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
func TestRequestIDPropagator(t *testing.T) { | ||
const testRequestID = "00000000-0000-0000-0000-000000000000" | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte(r.Header.Get(clusterServerRequestIDHeader))) | ||
})) | ||
defer ts.Close() | ||
|
||
do := func(c *http.Client) string { | ||
t.Helper() | ||
|
||
ctx := context.Background() | ||
r, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL, nil) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
correlationData := arm.NewCorrelationData(r) | ||
correlationData.RequestID = uuid.MustParse(testRequestID) | ||
r = r.WithContext(ContextWithCorrelationData(ctx, correlationData)) | ||
|
||
rs, err := c.Do(r) | ||
if err != nil { | ||
t.Fatalf("unexpected error from server: %s", err) | ||
} | ||
|
||
if rs.StatusCode != http.StatusOK { | ||
t.Fatalf("unexpected status code: %d", rs.StatusCode) | ||
} | ||
|
||
b, err := io.ReadAll(rs.Body) | ||
if err != nil { | ||
t.Fatalf("unexpected error reading response: %s", err) | ||
} | ||
|
||
return string(b) | ||
} | ||
|
||
// Without the transport wrapper, the request ID isn't echoed. | ||
c := ts.Client() | ||
if ret := do(c); ret != "" { | ||
t.Fatalf("expecting an empty response, got %q", ret) | ||
} | ||
|
||
// With the transport wrapper, the request ID is echoed. | ||
c.Transport = RequestIDPropagator(c.Transport) | ||
if ret := do(c); ret != testRequestID { | ||
t.Fatalf("expecting %q, got %q", testRequestID, ret) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters