-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_test.go
More file actions
380 lines (278 loc) · 8.78 KB
/
service_test.go
File metadata and controls
380 lines (278 loc) · 8.78 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package service
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestNew(t *testing.T) {
t.Parallel()
t.Run("with config", func(t *testing.T) {
t.Parallel()
config := DefaultConfig()
config.Addr = ":8081"
svc := New("test", config)
if svc.Name != "test" {
t.Errorf("expected name 'test', got %s", svc.Name)
}
if svc.Config.Addr != ":8081" {
t.Errorf("expected addr ':8081', got %s", svc.Config.Addr)
}
if svc.Logger == nil {
t.Error("logger should not be nil")
}
if svc.Metrics == nil {
t.Error("metrics should not be nil")
}
})
t.Run("with nil config", func(t *testing.T) {
t.Parallel()
svc := New("test", nil)
if svc.Config == nil {
t.Error("config should not be nil when nil is passed")
}
if svc.Config.Addr != ":8080" {
t.Errorf("expected default addr ':8080', got %s", svc.Config.Addr)
}
})
}
func TestDefaultConfig(t *testing.T) {
t.Parallel()
config := DefaultConfig()
if config.Addr != ":8080" {
t.Errorf("expected default addr ':8080', got %s", config.Addr)
}
if config.MetricsAddr != ":9090" {
t.Errorf("expected default metrics addr ':9090', got %s", config.MetricsAddr)
}
if config.MetricsPath != "/metrics" {
t.Errorf("expected default metrics path '/metrics', got %s", config.MetricsPath)
}
if config.ReadTimeout != 10*time.Second {
t.Errorf("expected default read timeout 10s, got %v", config.ReadTimeout)
}
if config.Logger == nil {
t.Error("default logger should not be nil")
}
}
func TestLoadFromEnv(t *testing.T) {
// Set environment variables
t.Setenv("ADDR", ":8888")
t.Setenv("METRICS_ADDR", ":9999")
t.Setenv("METRICS_PATH", "/custom-metrics")
config, err := LoadFromEnv()
if err != nil {
t.Fatalf("failed to load config from env: %v", err)
}
if config.Addr != ":8888" {
t.Errorf("expected addr ':8888', got %s", config.Addr)
}
if config.MetricsAddr != ":9999" {
t.Errorf("expected metrics addr ':9999', got %s", config.MetricsAddr)
}
if config.MetricsPath != "/custom-metrics" {
t.Errorf("expected metrics path '/custom-metrics', got %s", config.MetricsPath)
}
}
func TestHandleFunc(t *testing.T) {
t.Parallel()
svc := New("test", nil)
// Add a simple handler
svc.HandleFunc("/test", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("test response"))
})
// Create a test request
req := httptest.NewRequest(http.MethodGet, "/test", nil)
recorder := httptest.NewRecorder()
// Serve the request
svc.mux.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", recorder.Code)
}
if strings.TrimSpace(recorder.Body.String()) != "test response" {
t.Errorf("expected body 'test response', got %s", recorder.Body.String())
}
}
func TestGetLogger(t *testing.T) {
t.Parallel()
svc := New("test", nil)
// Test with a request that has logger middleware applied
handler := applyMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger := GetLogger(r)
if logger == nil {
t.Error("logger should not be nil")
}
w.WriteHeader(http.StatusOK)
}), LoggerMiddleware(svc.Logger))
req := httptest.NewRequest(http.MethodGet, "/test", nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", recorder.Code)
}
}
func TestGetMetrics(t *testing.T) {
t.Parallel()
svc := New("test", nil)
// Test with a request that has metrics middleware applied
handler := applyMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
metrics := GetMetrics(r)
if metrics == nil {
t.Error("metrics should not be nil")
}
w.WriteHeader(http.StatusOK)
}), MetricsMiddleware(svc.Metrics))
req := httptest.NewRequest(http.MethodGet, "/test", nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", recorder.Code)
}
}
func TestRecoveryMiddleware(t *testing.T) {
t.Parallel()
svc := New("test", nil)
// Create a handler that panics
handler := applyMiddleware(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
panic("test panic")
}), RecoveryMiddleware(svc.Logger))
req := httptest.NewRequest(http.MethodGet, "/test", nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code != http.StatusInternalServerError {
t.Errorf("expected status 500, got %d", recorder.Code)
}
if !strings.Contains(recorder.Body.String(), "Internal Server Error") {
t.Error("expected 'Internal Server Error' in response body")
}
}
func TestMetricsMiddleware(t *testing.T) {
t.Parallel()
svc := New("test", nil)
// Create a simple handler
handler := applyMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("test"))
}), MetricsMiddleware(svc.Metrics))
req := httptest.NewRequest(http.MethodGet, "/test", nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", recorder.Code)
}
}
func TestShutdownHooks(t *testing.T) {
t.Parallel()
svc := New("test", nil)
hookCalled := false
svc.AddShutdownHook(func() error {
hookCalled = true
return nil
})
// Create a minimal server setup
svc.server = &http.Server{Addr: ":0"} //nolint:gosec
svc.metricsServer = &http.Server{Addr: ":0"} //nolint:gosec
// Test graceful shutdown
err := svc.gracefulShutdown()
if err != nil {
t.Errorf("graceful shutdown failed: %v", err)
}
if !hookCalled {
t.Error("shutdown hook was not called")
}
}
func TestUse(t *testing.T) {
t.Parallel()
svc := New("test", nil)
initialCount := len(svc.middlewares)
// Add a custom middleware
svc.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Custom", "test")
next.ServeHTTP(w, r)
})
})
if len(svc.middlewares) != initialCount+1 {
t.Errorf("expected %d middlewares, got %d", initialCount+1, len(svc.middlewares))
}
// Test that the custom middleware is applied
svc.HandleFunc("/test", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
req := httptest.NewRequest(http.MethodGet, "/test", nil)
recorder := httptest.NewRecorder()
svc.mux.ServeHTTP(recorder, req)
if recorder.Header().Get("X-Custom") != "test" {
t.Error("custom middleware was not applied")
}
}
func TestIntegration(t *testing.T) {
t.Parallel()
// Create a service with custom configuration
config := DefaultConfig()
config.Addr = ":0" // Use random port
config.MetricsAddr = ":0"
svc := New("integration-test", config)
// Add a test handler
svc.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
logger := GetLogger(r)
logger.Info("hello endpoint called")
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello, World!"))
})
// Test the handler
req := httptest.NewRequest(http.MethodGet, "/hello", nil)
recorder := httptest.NewRecorder()
svc.mux.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", recorder.Code)
}
if strings.TrimSpace(recorder.Body.String()) != "Hello, World!" {
t.Errorf("expected body 'Hello, World!', got %s", recorder.Body.String())
}
t.Log("Integration test completed successfully")
}
func TestStartMetricsServer(t *testing.T) {
t.Parallel()
svc := New("test", nil)
// Test that metrics server can be started (we'll use a mock)
// In practice, you'd test this by actually starting the server
// and checking if endpoints are accessible
if svc.Config.MetricsAddr != ":9090" {
t.Errorf("expected metrics addr ':9090', got %s", svc.Config.MetricsAddr)
}
if svc.Config.MetricsPath != "/metrics" {
t.Errorf("expected metrics path '/metrics', got %s", svc.Config.MetricsPath)
}
}
// BenchmarkHandleFunc benchmarks the HandleFunc method
func BenchmarkHandleFunc(b *testing.B) {
svc := New("benchmark", nil)
handler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("benchmark"))
}
svc.HandleFunc("/benchmark", handler)
req := httptest.NewRequest(http.MethodGet, "/benchmark", nil)
b.ResetTimer()
for range b.N {
recorder := httptest.NewRecorder()
svc.mux.ServeHTTP(recorder, req)
}
}
// BenchmarkMiddleware benchmarks the middleware stack
func BenchmarkMiddleware(b *testing.B) {
svc := New("benchmark", nil)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
wrappedHandler := applyMiddleware(handler, svc.middlewares...)
req := httptest.NewRequest(http.MethodGet, "/benchmark", nil)
b.ResetTimer()
for range b.N {
recorder := httptest.NewRecorder()
wrappedHandler.ServeHTTP(recorder, req)
}
}