@@ -20,6 +20,17 @@ func TestNewRouter(t *testing.T) {
2020 }
2121}
2222
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+
2334func TestRouterMiddleware (t * testing.T ) {
2435 r := NewRouter ()
2536
@@ -34,12 +45,7 @@ func TestRouterMiddleware(t *testing.T) {
3445 fmt .Fprintf (w , "/middlewares\n " )
3546 }))
3647
37- cases := []struct {
38- path string
39- method string
40- code int
41- body string
42- }{
48+ cases := []routerTest {
4349 {
4450 path : "/globalmiddleware" ,
4551 method : http .MethodGet ,
@@ -61,22 +67,25 @@ func TestRouterMiddleware(t *testing.T) {
6167 }
6268
6369 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+ })
7886 }
7987}
88+
8089func TestRouter (t * testing.T ) {
8190 r := NewRouter ()
8291
@@ -142,12 +151,7 @@ func TestRouter(t *testing.T) {
142151 fmt .Fprintf (w , "/%v" , id )
143152 }))
144153
145- cases := []struct {
146- path string
147- method string
148- code int
149- body string
150- }{
154+ cases := []routerTest {
151155 {
152156 path : "/" ,
153157 method : http .MethodGet ,
@@ -247,20 +251,22 @@ func TestRouter(t *testing.T) {
247251 }
248252
249253 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+ })
264270 }
265271}
266272
@@ -269,11 +275,7 @@ func TestDefaultErrorHandler(t *testing.T) {
269275 r .Methods (http .MethodGet ).Handler (`/defaulterrorhandler` , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {}))
270276 r .Methods (http .MethodGet ).Handler (`/methodnotallowed` , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {}))
271277
272- cases := []struct {
273- path string
274- method string
275- code int
276- }{
278+ cases := []routerTest {
277279 {
278280 path : "/" ,
279281 method : http .MethodGet ,
@@ -287,14 +289,16 @@ func TestDefaultErrorHandler(t *testing.T) {
287289 }
288290
289291 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 ()
292295
293- r .ServeHTTP (rec , req )
296+ r .ServeHTTP (rec , req )
294297
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+ })
298302 }
299303}
300304
@@ -311,12 +315,7 @@ func TestCustomErrorHandler(t *testing.T) {
311315 r .Methods (http .MethodGet ).Handler (`/custommethodnotfound` , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {}))
312316 r .Methods (http .MethodGet ).Handler (`/custommethodnotallowed` , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {}))
313317
314- cases := []struct {
315- path string
316- method string
317- code int
318- body string
319- }{
318+ cases := []routerTest {
320319 {
321320 path : "/" ,
322321 method : http .MethodGet ,
@@ -332,20 +331,22 @@ func TestCustomErrorHandler(t *testing.T) {
332331 }
333332
334333 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+ })
349350 }
350351}
351352
0 commit comments