@@ -7,13 +7,8 @@ import (
7
7
8
8
func TestJSONP (t * testing.T ) {
9
9
10
- handler := ResourceHandler {
11
- DisableJsonIndent : true ,
12
- PreRoutingMiddlewares : []Middleware {
13
- & JsonpMiddleware {},
14
- },
15
- }
16
- handler .SetRoutes (
10
+ // router app with success and error paths
11
+ router , err := MakeRouter (
17
12
& Route {"GET" , "/ok" ,
18
13
func (w ResponseWriter , r * Request ) {
19
14
w .WriteJson (map [string ]string {"Id" : "123" })
@@ -25,13 +20,24 @@ func TestJSONP(t *testing.T) {
25
20
},
26
21
},
27
22
)
23
+ if err != nil {
24
+ t .Fatal (err )
25
+ }
26
+
27
+ api := NewApi (router )
28
+
29
+ // the middleware to test
30
+ api .Use (& JsonpMiddleware {})
31
+
32
+ // wrap all
33
+ handler := api .MakeHandler ()
28
34
29
- recorded := test .RunRequest (t , & handler , test .MakeSimpleRequest ("GET" , "http://1.2.3.4 /ok?callback=parseResponse" , nil ))
35
+ recorded := test .RunRequest (t , handler , test .MakeSimpleRequest ("GET" , "http://localhost /ok?callback=parseResponse" , nil ))
30
36
recorded .CodeIs (200 )
31
37
recorded .HeaderIs ("Content-Type" , "text/javascript" )
32
38
recorded .BodyIs ("parseResponse({\" Id\" :\" 123\" })" )
33
39
34
- recorded = test .RunRequest (t , & handler , test .MakeSimpleRequest ("GET" , "http://1.2.3.4 /error?callback=parseResponse" , nil ))
40
+ recorded = test .RunRequest (t , handler , test .MakeSimpleRequest ("GET" , "http://localhost /error?callback=parseResponse" , nil ))
35
41
recorded .CodeIs (500 )
36
42
recorded .HeaderIs ("Content-Type" , "text/javascript" )
37
43
recorded .BodyIs ("parseResponse({\" Error\" :\" jsonp error\" })" )
0 commit comments