@@ -20,6 +20,17 @@ func TestNewRouter(t *testing.T) {
20
20
}
21
21
}
22
22
23
+ type routerTest struct {
24
+ path string
25
+ method string
26
+ code int
27
+ body string
28
+ }
29
+
30
+ func (tc routerTest ) name () string {
31
+ return fmt .Sprintf ("%s_%s_%d" , tc .method , tc .path , tc .code )
32
+ }
33
+
23
34
func TestRouterMiddleware (t * testing.T ) {
24
35
r := NewRouter ()
25
36
@@ -34,12 +45,7 @@ func TestRouterMiddleware(t *testing.T) {
34
45
fmt .Fprintf (w , "/middlewares\n " )
35
46
}))
36
47
37
- cases := []struct {
38
- path string
39
- method string
40
- code int
41
- body string
42
- }{
48
+ cases := []routerTest {
43
49
{
44
50
path : "/globalmiddleware" ,
45
51
method : http .MethodGet ,
@@ -61,22 +67,25 @@ func TestRouterMiddleware(t *testing.T) {
61
67
}
62
68
63
69
for _ , c := range cases {
64
- req := httptest .NewRequest (c .method , c .path , nil )
65
- rec := httptest .NewRecorder ()
66
-
67
- r .ServeHTTP (rec , req )
68
-
69
- if rec .Code != c .code {
70
- t .Errorf ("actual: %v expected: %v\n " , rec .Code , c .code )
71
- }
72
-
73
- recBody , _ := io .ReadAll (rec .Body )
74
- body := string (recBody )
75
- if body != c .body {
76
- t .Errorf ("actual: %v expected: %v\n " , body , c .body )
77
- }
70
+ t .Run (c .name (), func (t * testing.T ) {
71
+ req := httptest .NewRequest (c .method , c .path , nil )
72
+ rec := httptest .NewRecorder ()
73
+
74
+ r .ServeHTTP (rec , req )
75
+
76
+ if rec .Code != c .code {
77
+ t .Errorf ("actual: %v expected: %v\n " , rec .Code , c .code )
78
+ }
79
+
80
+ recBody , _ := io .ReadAll (rec .Body )
81
+ body := string (recBody )
82
+ if body != c .body {
83
+ t .Errorf ("actual: %v expected: %v\n " , body , c .body )
84
+ }
85
+ })
78
86
}
79
87
}
88
+
80
89
func TestRouter (t * testing.T ) {
81
90
r := NewRouter ()
82
91
@@ -142,12 +151,7 @@ func TestRouter(t *testing.T) {
142
151
fmt .Fprintf (w , "/%v" , id )
143
152
}))
144
153
145
- cases := []struct {
146
- path string
147
- method string
148
- code int
149
- body string
150
- }{
154
+ cases := []routerTest {
151
155
{
152
156
path : "/" ,
153
157
method : http .MethodGet ,
@@ -247,20 +251,22 @@ func TestRouter(t *testing.T) {
247
251
}
248
252
249
253
for _ , c := range cases {
250
- req := httptest .NewRequest (c .method , c .path , nil )
251
- rec := httptest .NewRecorder ()
252
-
253
- r .ServeHTTP (rec , req )
254
-
255
- if rec .Code != c .code {
256
- t .Errorf ("actual: %v expected: %v\n " , rec .Code , c .code )
257
- }
258
-
259
- recBody , _ := io .ReadAll (rec .Body )
260
- body := string (recBody )
261
- if body != c .body {
262
- t .Errorf ("actual: %v expected: %v\n " , body , c .body )
263
- }
254
+ t .Run (c .name (), func (t * testing.T ) {
255
+ req := httptest .NewRequest (c .method , c .path , nil )
256
+ rec := httptest .NewRecorder ()
257
+
258
+ r .ServeHTTP (rec , req )
259
+
260
+ if rec .Code != c .code {
261
+ t .Errorf ("actual: %v expected: %v\n " , rec .Code , c .code )
262
+ }
263
+
264
+ recBody , _ := io .ReadAll (rec .Body )
265
+ body := string (recBody )
266
+ if body != c .body {
267
+ t .Errorf ("actual: %v expected: %v\n " , body , c .body )
268
+ }
269
+ })
264
270
}
265
271
}
266
272
@@ -269,11 +275,7 @@ func TestDefaultErrorHandler(t *testing.T) {
269
275
r .Methods (http .MethodGet ).Handler (`/defaulterrorhandler` , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {}))
270
276
r .Methods (http .MethodGet ).Handler (`/methodnotallowed` , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {}))
271
277
272
- cases := []struct {
273
- path string
274
- method string
275
- code int
276
- }{
278
+ cases := []routerTest {
277
279
{
278
280
path : "/" ,
279
281
method : http .MethodGet ,
@@ -287,14 +289,16 @@ func TestDefaultErrorHandler(t *testing.T) {
287
289
}
288
290
289
291
for _ , c := range cases {
290
- req := httptest .NewRequest (c .method , c .path , nil )
291
- rec := httptest .NewRecorder ()
292
+ t .Run (c .name (), func (t * testing.T ) {
293
+ req := httptest .NewRequest (c .method , c .path , nil )
294
+ rec := httptest .NewRecorder ()
292
295
293
- r .ServeHTTP (rec , req )
296
+ r .ServeHTTP (rec , req )
294
297
295
- if rec .Code != c .code {
296
- t .Errorf ("actual: %v expected: %v\n " , rec .Code , c .code )
297
- }
298
+ if rec .Code != c .code {
299
+ t .Errorf ("actual: %v expected: %v\n " , rec .Code , c .code )
300
+ }
301
+ })
298
302
}
299
303
}
300
304
@@ -311,12 +315,7 @@ func TestCustomErrorHandler(t *testing.T) {
311
315
r .Methods (http .MethodGet ).Handler (`/custommethodnotfound` , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {}))
312
316
r .Methods (http .MethodGet ).Handler (`/custommethodnotallowed` , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {}))
313
317
314
- cases := []struct {
315
- path string
316
- method string
317
- code int
318
- body string
319
- }{
318
+ cases := []routerTest {
320
319
{
321
320
path : "/" ,
322
321
method : http .MethodGet ,
@@ -332,20 +331,22 @@ func TestCustomErrorHandler(t *testing.T) {
332
331
}
333
332
334
333
for _ , c := range cases {
335
- req := httptest .NewRequest (c .method , c .path , nil )
336
- rec := httptest .NewRecorder ()
337
-
338
- r .ServeHTTP (rec , req )
339
-
340
- if rec .Code != c .code {
341
- t .Errorf ("actual: %v expected: %v\n " , rec .Code , c .code )
342
- }
343
-
344
- recBody , _ := io .ReadAll (rec .Body )
345
- body := string (recBody )
346
- if body != c .body {
347
- t .Errorf ("actual: %v expected: %v\n " , body , c .body )
348
- }
334
+ t .Run (c .name (), func (t * testing.T ) {
335
+ req := httptest .NewRequest (c .method , c .path , nil )
336
+ rec := httptest .NewRecorder ()
337
+
338
+ r .ServeHTTP (rec , req )
339
+
340
+ if rec .Code != c .code {
341
+ t .Errorf ("actual: %v expected: %v\n " , rec .Code , c .code )
342
+ }
343
+
344
+ recBody , _ := io .ReadAll (rec .Body )
345
+ body := string (recBody )
346
+ if body != c .body {
347
+ t .Errorf ("actual: %v expected: %v\n " , body , c .body )
348
+ }
349
+ })
349
350
}
350
351
}
351
352
0 commit comments