-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathjsonp_test.go
47 lines (37 loc) · 1.3 KB
/
jsonp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package rest
import (
"testing"
"github.com/ant0ine/go-json-rest/rest/test"
)
func TestJsonpMiddleware(t *testing.T) {
api := NewApi()
// the middleware to test
api.Use(&JsonpMiddleware{})
// router app with success and error paths
router, err := MakeRouter(
Get("/ok", func(w ResponseWriter, r *Request) {
w.WriteJson(map[string]string{"Id": "123"})
}),
Get("/error", func(w ResponseWriter, r *Request) {
Error(w, "jsonp error", 500)
}),
)
if err != nil {
t.Fatal(err)
}
api.SetApp(router)
// wrap all
handler := api.MakeHandler()
recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/ok?callback=parseResponse", nil))
recorded.CodeIs(200)
recorded.HeaderIs("Content-Type", "text/javascript")
recorded.HeaderIs("Content-Disposition", "filename=f.txt")
recorded.HeaderIs("X-Content-Type-Options", "nosniff")
recorded.BodyIs("/**/parseResponse({\"Id\":\"123\"})")
recorded = test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/error?callback=parseResponse", nil))
recorded.CodeIs(500)
recorded.HeaderIs("Content-Type", "text/javascript")
recorded.HeaderIs("Content-Disposition", "filename=f.txt")
recorded.HeaderIs("X-Content-Type-Options", "nosniff")
recorded.BodyIs("/**/parseResponse({\"Error\":\"jsonp error\"})")
}