| 1 | package render |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | type GreetingP struct { |
| 11 | One string `json:"one"` |
| 12 | Two string `json:"two"` |
| 13 | } |
| 14 | |
| 15 | func TestJSONPBasic(t *testing.T) { |
| 16 | render := New() |
| 17 | |
| 18 | var err error |
| 19 | h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 20 | err = render.JSONP(w, 299, "helloCallback", GreetingP{"hello", "world"}) |
| 21 | }) |
| 22 | |
| 23 | res := httptest.NewRecorder() |
| 24 | req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 25 | h.ServeHTTP(res, req) |
| 26 | |
| 27 | expectNil(t, err) |
| 28 | expect(t, res.Code, 299) |
| 29 | expect(t, res.Header().Get(ContentType), ContentJSONP+"; charset=UTF-8") |
| 30 | expect(t, res.Body.String(), "helloCallback({\"one\":\"hello\",\"two\":\"world\"});") |
| 31 | } |
| 32 | |
| 33 | func TestJSONPRenderIndented(t *testing.T) { |
| 34 | render := New(Options{ |
| 35 | IndentJSON: true, |
| 36 | }) |
| 37 | |
| 38 | var err error |
| 39 | h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 40 | err = render.JSONP(w, http.StatusOK, "helloCallback", GreetingP{"hello", "world"}) |
| 41 | }) |
| 42 | |
| 43 | res := httptest.NewRecorder() |
| 44 | req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 45 | h.ServeHTTP(res, req) |
| 46 | |
| 47 | expectNil(t, err) |
| 48 | expect(t, res.Code, http.StatusOK) |
| 49 | expect(t, res.Header().Get(ContentType), ContentJSONP+"; charset=UTF-8") |
| 50 | expect(t, res.Body.String(), "helloCallback({\n \"one\": \"hello\",\n \"two\": \"world\"\n});\n") |
| 51 | } |
| 52 | |
| 53 | func TestJSONPWithError(t *testing.T) { |
| 54 | render := New() |
| 55 | |
| 56 | var err error |
| 57 | h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 58 | err = render.JSONP(w, 299, "helloCallback", math.NaN()) |
| 59 | }) |
| 60 | |
| 61 | res := httptest.NewRecorder() |
| 62 | req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 63 | h.ServeHTTP(res, req) |
| 64 | |
| 65 | expectNotNil(t, err) |
| 66 | expect(t, res.Code, 500) |
| 67 | } |
| 68 | |
| 69 | func TestJSONPCustomContentType(t *testing.T) { |
| 70 | render := New(Options{ |
| 71 | JSONPContentType: "application/vnd.api+json", |
| 72 | }) |
| 73 | |
| 74 | var err error |
| 75 | h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 76 | err = render.JSONP(w, http.StatusOK, "helloCallback", GreetingP{"hello", "world"}) |
| 77 | }) |
| 78 | |
| 79 | res := httptest.NewRecorder() |
| 80 | req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 81 | h.ServeHTTP(res, req) |
| 82 | |
| 83 | expectNil(t, err) |
| 84 | expect(t, res.Code, http.StatusOK) |
| 85 | expect(t, res.Header().Get(ContentType), "application/vnd.api+json; charset=UTF-8") |
| 86 | expect(t, res.Body.String(), "helloCallback({\"one\":\"hello\",\"two\":\"world\"});") |
| 87 | } |
| 88 | |
| 89 | func TestJSONPDisabledCharset(t *testing.T) { |
| 90 | render := New(Options{ |
| 91 | DisableCharset: true, |
| 92 | }) |
| 93 | |
| 94 | var err error |
| 95 | h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 96 | err = render.JSONP(w, http.StatusOK, "helloCallback", GreetingP{"hello", "world"}) |
| 97 | }) |
| 98 | |
| 99 | res := httptest.NewRecorder() |
| 100 | req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 101 | h.ServeHTTP(res, req) |
| 102 | |
| 103 | expectNil(t, err) |
| 104 | expect(t, res.Code, http.StatusOK) |
| 105 | expect(t, res.Header().Get(ContentType), ContentJSONP) |
| 106 | expect(t, res.Body.String(), "helloCallback({\"one\":\"hello\",\"two\":\"world\"});") |
| 107 | } |