Skip to content

Commit 36cec66

Browse files
committed
test(auth): handle OpenTelemetry wrapped case
1 parent 5a71c13 commit 36cec66

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

internal/auth/http_test.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,27 @@ var (
116116
_ auth.Storage = &memoryTokenStorage{}
117117
)
118118

119+
type testContextKeyType string
120+
121+
const (
122+
testContextKey testContextKeyType = "test"
123+
)
124+
119125
func TestExtractToken(t *testing.T) {
120126
t.Run("no token", func(t *testing.T) {
121-
r := http.Request{}
127+
mockCtx := context.WithValue(context.Background(), testContextKey, "test")
128+
129+
r := &http.Request{}
130+
r = r.WithContext(mockCtx)
131+
122132
storage := &mockTokenStorage{}
123-
ctx, err := auth.ExtractToken(&r, storage)
133+
ctx, err := auth.ExtractToken(r, storage)
124134
if err != nil {
125135
t.Fatalf("expected no error, got %v", err)
126136
}
127137

128-
if ctx != r.Context() {
129-
t.Fatalf("expected context to be the same, got %v", ctx)
138+
if ctx.Value(testContextKey) != "test" {
139+
t.Fatalf("expected context to be the same, got %v", ctx.Value(testContextKey))
130140
}
131141
})
132142

@@ -219,6 +229,8 @@ func TestExtractToken(t *testing.T) {
219229
})
220230

221231
t.Run("revoked token should be treated like no token", func(t *testing.T) {
232+
mockCtx := context.WithValue(context.Background(), testContextKey, "test")
233+
222234
storage := &memoryTokenStorage{
223235
storage: map[string]auth.TokenInfo{},
224236
}
@@ -237,10 +249,11 @@ func TestExtractToken(t *testing.T) {
237249
}
238250

239251
// Verify token exists and works
240-
r := http.Request{
252+
r := &http.Request{
241253
Header: http.Header{"Authorization": {"Bearer " + token}},
242254
}
243-
ctx, err := auth.ExtractToken(&r, storage)
255+
r = r.WithContext(mockCtx)
256+
ctx, err := auth.ExtractToken(r, storage)
244257
if err != nil {
245258
t.Fatalf("expected no error for valid token, got %v", err)
246259
}
@@ -260,13 +273,13 @@ func TestExtractToken(t *testing.T) {
260273
}
261274

262275
// Test that the revoked token is treated like no token
263-
ctx, err = auth.ExtractToken(&r, storage)
276+
ctx, err = auth.ExtractToken(r, storage)
264277
if err != nil {
265278
t.Fatalf("expected no error for revoked token, got %v", err)
266279
}
267280

268281
// Should not have user context (same as no token)
269-
if ctx != r.Context() {
282+
if ctx.Value(testContextKey) != "test" {
270283
t.Fatalf("expected context to be the same as original, got different context")
271284
}
272285

0 commit comments

Comments
 (0)