Skip to content

Commit 553d6ee

Browse files
committed
polish jsonp middleware unit tests
1 parent 2370f1d commit 553d6ee

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

rest/jsonp_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@ import (
77

88
func TestJSONP(t *testing.T) {
99

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(
1712
&Route{"GET", "/ok",
1813
func(w ResponseWriter, r *Request) {
1914
w.WriteJson(map[string]string{"Id": "123"})
@@ -25,13 +20,24 @@ func TestJSONP(t *testing.T) {
2520
},
2621
},
2722
)
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()
2834

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))
3036
recorded.CodeIs(200)
3137
recorded.HeaderIs("Content-Type", "text/javascript")
3238
recorded.BodyIs("parseResponse({\"Id\":\"123\"})")
3339

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))
3541
recorded.CodeIs(500)
3642
recorded.HeaderIs("Content-Type", "text/javascript")
3743
recorded.BodyIs("parseResponse({\"Error\":\"jsonp error\"})")

rest/middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Middleware interface {
2929
}
3030

3131
// MiddlewareSimple is an adapter type that makes it easy to write a Middleware with a simple
32-
// function. eg: api.Use(rest.MiddlewareSimple(func(h HandlerFunc) Handlerfunc { .. }))
32+
// function. eg: api.Use(rest.MiddlewareSimple(func(h HandlerFunc) Handlerfunc { ... }))
3333
type MiddlewareSimple func(handler HandlerFunc) HandlerFunc
3434

3535
// MiddlewareFunc makes MiddlewareSimple implement the Middleware interface.

0 commit comments

Comments
 (0)