-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcore_test.go
More file actions
184 lines (167 loc) · 4.69 KB
/
Copy pathcore_test.go
File metadata and controls
184 lines (167 loc) · 4.69 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package core
import (
"context"
"testing"
"github.com/go-coldbrew/core/config"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
)
// mockCBService is a minimal implementation of CBService for testing.
type mockCBService struct{}
func (m *mockCBService) InitHTTP(_ context.Context, _ *runtime.ServeMux, _ string, _ []grpc.DialOption) error {
return nil
}
func (m *mockCBService) InitGRPC(_ context.Context, _ *grpc.Server) error {
return nil
}
func TestNew(t *testing.T) {
// removed t.Parallel() — core tests mutate package-level globals
cb := New(config.Config{
DisableSignalHandler: true,
})
if cb == nil {
t.Fatal("New() returned nil")
}
}
func TestSetService_Nil(t *testing.T) {
// removed t.Parallel() — core tests mutate package-level globals
c := &cb{
svc: make([]CBService, 0),
}
err := c.SetService(nil)
if err == nil {
t.Fatal("SetService(nil) should return an error")
}
if err.Error() != "service is nil" {
t.Fatalf("unexpected error message: %s", err.Error())
}
}
func TestSetService_Valid(t *testing.T) {
// removed t.Parallel() — core tests mutate package-level globals
c := &cb{
svc: make([]CBService, 0),
}
err := c.SetService(&mockCBService{})
if err != nil {
t.Fatalf("SetService with valid service returned error: %v", err)
}
if len(c.svc) != 1 {
t.Fatalf("expected 1 service, got %d", len(c.svc))
}
}
func TestParseHeaders(t *testing.T) {
// removed t.Parallel() — core tests mutate package-level globals
tests := []struct {
name string
input string
expected map[string]string
}{
{
name: "empty string",
input: "",
expected: map[string]string{},
},
{
name: "single pair",
input: "key1=value1",
expected: map[string]string{
"key1": "value1",
},
},
{
name: "multiple pairs",
input: "key1=value1,key2=value2",
expected: map[string]string{
"key1": "value1",
"key2": "value2",
},
},
{
name: "malformed pair is ignored",
input: "noequals",
expected: map[string]string{},
},
{
name: "whitespace is trimmed",
input: " key1 = value1 , key2 = value2 ",
expected: map[string]string{
"key1": "value1",
"key2": "value2",
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// removed t.Parallel() — core tests mutate package-level globals
result := parseHeaders(tc.input)
if len(result) != len(tc.expected) {
t.Fatalf("expected %d entries, got %d: %v", len(tc.expected), len(result), result)
}
for k, v := range tc.expected {
got, ok := result[k]
if !ok {
t.Fatalf("missing key %q", k)
}
if got != v {
t.Fatalf("key %q: expected %q, got %q", k, v, got)
}
}
})
}
}
func TestGetCustomHeaderMatcher(t *testing.T) {
// removed t.Parallel() — core tests mutate package-level globals
matcher := getCustomHeaderMatcher([]string{"X-Custom-"}, "X-Trace-Id")
t.Run("matching trace header returns true", func(t *testing.T) {
// removed t.Parallel() — core tests mutate package-level globals
key, matched := matcher("X-Trace-Id")
if !matched {
t.Fatal("expected trace header to match")
}
if key != "x-trace-id" {
t.Fatalf("expected key %q, got %q", "x-trace-id", key)
}
})
t.Run("matching prefix returns true", func(t *testing.T) {
// removed t.Parallel() — core tests mutate package-level globals
key, matched := matcher("X-Custom-Something")
if !matched {
t.Fatal("expected prefixed header to match")
}
if key != "x-custom-something" {
t.Fatalf("expected key %q, got %q", "x-custom-something", key)
}
})
t.Run("non-matching header falls back to default matcher", func(t *testing.T) {
// removed t.Parallel() — core tests mutate package-level globals
// "Content-Type" is handled by DefaultHeaderMatcher
key, matched := matcher("Content-Type")
if !matched {
t.Fatal("expected Content-Type to be matched by default matcher")
}
if key == "" {
t.Fatal("expected non-empty key for Content-Type")
}
// Unknown non-prefixed header should not match
_, matched2 := matcher("X-Unknown-Non-Prefixed")
if matched2 {
t.Fatal("expected non-matching header to not match")
}
})
}
func TestGetCustomHeaderMatcher_MultipleHeaders(t *testing.T) {
// removed t.Parallel() — core tests mutate package-level globals
matcher := getCustomHeaderMatcher(nil, "X-Trace-Id", "X-Debug-Log-Level")
_, traceMatched := matcher("X-Trace-Id")
if !traceMatched {
t.Fatal("expected trace header to match")
}
_, debugMatched := matcher("X-Debug-Log-Level")
if !debugMatched {
t.Fatal("expected debug log header to match")
}
_, unknownMatched := matcher("X-Random")
if unknownMatched {
t.Fatal("expected unknown header to not match")
}
}