@@ -11,51 +11,46 @@ import (
11
11
12
12
func TestHandleGetRedirects (t * testing.T ) {
13
13
t .Parallel ()
14
- tests := []struct {
15
- name string
16
- urlParam string
17
- expectedStatus int
18
- expectedBody map [string ]interface {}
19
- }{
20
- {
21
- name : "Missing URL parameter" ,
22
- urlParam : "" ,
23
- expectedStatus : http .StatusBadRequest ,
24
- expectedBody : KV {"error" : "missing URL parameter" },
25
- },
26
- {
27
- name : "Invalid URL" ,
28
- urlParam : "invalid-url" ,
29
- expectedStatus : http .StatusInternalServerError ,
30
- expectedBody : KV {"error" : "error: Get \" http://invalid-url\" " },
31
- },
32
- {
33
- name : "Valid URL with no redirects" ,
34
- urlParam : "example.com" ,
35
- expectedStatus : http .StatusOK ,
36
- expectedBody : KV {"redirects" : []interface {}{"http://example.com" }},
37
- },
38
- }
39
-
40
- for _ , tc := range tests {
41
- tc := tc
42
- t .Run (tc .name , func (t * testing.T ) {
43
- t .Parallel ()
44
- req := httptest .NewRequest ("GET" , "/redirects?url=" + tc .urlParam , nil )
45
- rec := httptest .NewRecorder ()
46
- HandleGetRedirects ().ServeHTTP (rec , req )
47
-
48
- assert .Equal (t , tc .expectedStatus , rec .Code )
49
-
50
- var response map [string ]interface {}
51
- err := json .Unmarshal (rec .Body .Bytes (), & response )
52
- assert .NoError (t , err )
53
- // TODO: break this out of table drive tests, should not use name as part of logic
54
- if tc .name == "Invalid URL" {
55
- assert .Contains (t , response ["error" ], tc .expectedBody ["error" ])
56
- } else {
57
- assert .Equal (t , tc .expectedBody , response )
58
- }
59
- })
60
- }
14
+
15
+ t .Run ("Missing URL parameter" , func (t * testing.T ) {
16
+ t .Parallel ()
17
+ req := httptest .NewRequest ("GET" , "/redirects?url=" , nil )
18
+ rec := httptest .NewRecorder ()
19
+
20
+ HandleGetRedirects ().ServeHTTP (rec , req )
21
+
22
+ assert .Equal (t , http .StatusBadRequest , rec .Code )
23
+ var response KV
24
+ err := json .Unmarshal (rec .Body .Bytes (), & response )
25
+ assert .NoError (t , err )
26
+ assert .Equal (t , KV {"error" : "missing URL parameter" }, response )
27
+ })
28
+
29
+ t .Run ("Invalid URL" , func (t * testing.T ) {
30
+ t .Parallel ()
31
+ req := httptest .NewRequest ("GET" , "/redirects?url=invalid-url" , nil )
32
+ rec := httptest .NewRecorder ()
33
+
34
+ HandleGetRedirects ().ServeHTTP (rec , req )
35
+
36
+ assert .Equal (t , http .StatusInternalServerError , rec .Code )
37
+ var response KV
38
+ err := json .Unmarshal (rec .Body .Bytes (), & response )
39
+ assert .NoError (t , err )
40
+ assert .Contains (t , response ["error" ], KV {"error" : "error: Get \" http://invalid-url\" " }["error" ])
41
+ })
42
+
43
+ t .Run ("Valid URL with no redirects" , func (t * testing.T ) {
44
+ t .Parallel ()
45
+ req := httptest .NewRequest ("GET" , "/redirects?url=example.com" , nil )
46
+ rec := httptest .NewRecorder ()
47
+
48
+ HandleGetRedirects ().ServeHTTP (rec , req )
49
+
50
+ assert .Equal (t , http .StatusOK , rec .Code )
51
+ var response KV
52
+ err := json .Unmarshal (rec .Body .Bytes (), & response )
53
+ assert .NoError (t , err )
54
+ assert .Equal (t , KV {"redirects" : []interface {}{"http://example.com" }}, response )
55
+ })
61
56
}
0 commit comments