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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
package render
import (
"math"
"net/http"
"net/http/httptest"
"testing"
)
type GreetingP struct {
One string `json:"one"`
Two string `json:"two"`
}
func TestJSONPBasic(t *testing.T) {
render := New()
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.JSONP(w, 299, "helloCallback", GreetingP{"hello", "world"})
})
res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, 299)
expect(t, res.Header().Get(ContentType), ContentJSONP+"; charset=UTF-8")
expect(t, res.Body.String(), "helloCallback({\"one\":\"hello\",\"two\":\"world\"});")
}
func TestJSONPRenderIndented(t *testing.T) {
render := New(Options{
IndentJSON: true,
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.JSONP(w, http.StatusOK, "helloCallback", GreetingP{"hello", "world"})
})
res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, http.StatusOK)
expect(t, res.Header().Get(ContentType), ContentJSONP+"; charset=UTF-8")
expect(t, res.Body.String(), "helloCallback({\n \"one\": \"hello\",\n \"two\": \"world\"\n});\n")
}
func TestJSONPWithError(t *testing.T) {
render := New()
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.JSONP(w, 299, "helloCallback", math.NaN())
})
res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNotNil(t, err)
expect(t, res.Code, 500)
}
func TestJSONPCustomContentType(t *testing.T) {
render := New(Options{
JSONPContentType: "application/vnd.api+json",
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.JSONP(w, http.StatusOK, "helloCallback", GreetingP{"hello", "world"})
})
res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, http.StatusOK)
expect(t, res.Header().Get(ContentType), "application/vnd.api+json; charset=UTF-8")
expect(t, res.Body.String(), "helloCallback({\"one\":\"hello\",\"two\":\"world\"});")
}
func TestJSONPDisabledCharset(t *testing.T) {
render := New(Options{
DisableCharset: true,
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.JSONP(w, http.StatusOK, "helloCallback", GreetingP{"hello", "world"})
})
res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, http.StatusOK)
expect(t, res.Header().Get(ContentType), ContentJSONP)
expect(t, res.Body.String(), "helloCallback({\"one\":\"hello\",\"two\":\"world\"});")
}
|