-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_context_test.go
More file actions
127 lines (106 loc) · 3.08 KB
/
Copy pathrequest_context_test.go
File metadata and controls
127 lines (106 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package options
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRequestContext_SingleContextValue(t *testing.T) {
ctx := context.Background()
// Adding options creates a RequestContext
ctx = AddToOptions(ctx, "opt-key", "opt-val")
rc := RequestContextFromContext(ctx)
require.NotNil(t, rc, "RequestContext should exist after AddToOptions")
// Adding log fields reuses the same RequestContext
ctx = AddToLogFields(ctx, "log-key", "log-val")
rc2 := RequestContextFromContext(ctx)
assert.Same(t, rc, rc2, "should reuse same RequestContext")
// Both are accessible
opts := FromContext(ctx)
require.NotNil(t, opts)
v, ok := opts.Get("opt-key")
assert.True(t, ok)
assert.Equal(t, "opt-val", v)
lf := LogFieldsFromContext(ctx)
require.NotNil(t, lf)
v, ok = lf.Get("log-key")
assert.True(t, ok)
assert.Equal(t, "log-val", v)
}
func TestRequestContext_LogFieldsFirst(t *testing.T) {
ctx := context.Background()
// Log fields first, then options
ctx = AddToLogFields(ctx, "log-key", "log-val")
ctx = AddToOptions(ctx, "opt-key", "opt-val")
rc := RequestContextFromContext(ctx)
require.NotNil(t, rc)
assert.NotNil(t, rc.opts)
assert.NotNil(t, rc.logFields)
}
func TestRequestContext_EagerInit(t *testing.T) {
ctx := context.Background()
ctx = AddToOptions(ctx, "k", "v")
rc := RequestContextFromContext(ctx)
require.NotNil(t, rc)
assert.NotNil(t, rc.opts, "opts should be initialized")
assert.NotNil(t, rc.logFields, "logFields should be eagerly initialized")
}
func TestRequestContext_EmptyContext(t *testing.T) {
ctx := context.Background()
assert.Nil(t, RequestContextFromContext(ctx))
assert.Nil(t, FromContext(ctx))
assert.Nil(t, LogFieldsFromContext(ctx))
}
func TestLogFieldsFromContext_NilCtx(t *testing.T) {
assert.Nil(t, LogFieldsFromContext(nil))
}
func TestAddToLogFields_NilCtx(t *testing.T) {
ctx := AddToLogFields(nil, "key", "val")
assert.NotNil(t, ctx, "should create context from nil")
lf := LogFieldsFromContext(ctx)
require.NotNil(t, lf)
v, ok := lf.Get("key")
assert.True(t, ok)
assert.Equal(t, "val", v)
}
func TestAddToLogFields_EmptyKey(t *testing.T) {
ctx := context.Background()
ctx = AddToLogFields(ctx, "", "val")
assert.Nil(t, RequestContextFromContext(ctx), "empty key should not create RequestContext")
}
func TestRangeSlice(t *testing.T) {
o := &Options{}
o.Add("a", 1)
o.Add("b", 2)
o.Add("c", 3)
collected := make(map[string]any)
o.RangeSlice(func(key, value any) bool {
collected[key.(string)] = value
return true
})
assert.Len(t, collected, 3)
assert.Equal(t, 1, collected["a"])
assert.Equal(t, 2, collected["b"])
assert.Equal(t, 3, collected["c"])
}
func TestRangeSlice_EarlyStop(t *testing.T) {
o := &Options{}
o.Add("a", 1)
o.Add("b", 2)
o.Add("c", 3)
count := 0
o.RangeSlice(func(key, value any) bool {
count++
return false // stop after first
})
assert.Equal(t, 1, count)
}
func TestRangeSlice_Empty(t *testing.T) {
o := &Options{}
called := false
o.RangeSlice(func(key, value any) bool {
called = true
return true
})
assert.False(t, called)
}