| 1 |
package render |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"math" |
| 6 |
"net/http" |
| 7 |
"net/http/httptest" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
type Greeting struct { |
| 12 |
One string `json:"one"` |
| 13 |
Two string `json:"two"` |
| 14 |
} |
| 15 |
|
| 16 |
func TestJSONBasic(t *testing.T) { |
| 17 |
render := New() |
| 18 |
|
| 19 |
var err error |
| 20 |
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 21 |
err = render.JSON(w, 299, Greeting{"hello", "world"}) |
| 22 |
}) |
| 23 |
|
| 24 |
res := httptest.NewRecorder() |
| 25 |
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 26 |
h.ServeHTTP(res, req) |
| 27 |
|
| 28 |
expectNil(t, err) |
| 29 |
expect(t, res.Code, 299) |
| 30 |
expect(t, res.Header().Get(ContentType), ContentJSON+"; charset=UTF-8") |
| 31 |
expect(t, res.Body.String(), "{\"one\":\"hello\",\"two\":\"world\"}") |
| 32 |
} |
| 33 |
|
| 34 |
func TestJSONPrefix(t *testing.T) { |
| 35 |
prefix := ")]}',\n" |
| 36 |
render := New(Options{ |
| 37 |
PrefixJSON: []byte(prefix), |
| 38 |
}) |
| 39 |
|
| 40 |
var err error |
| 41 |
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 42 |
err = render.JSON(w, 300, Greeting{"hello", "world"}) |
| 43 |
}) |
| 44 |
|
| 45 |
res := httptest.NewRecorder() |
| 46 |
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 47 |
h.ServeHTTP(res, req) |
| 48 |
|
| 49 |
expectNil(t, err) |
| 50 |
expect(t, res.Code, 300) |
| 51 |
expect(t, res.Header().Get(ContentType), ContentJSON+"; charset=UTF-8") |
| 52 |
expect(t, res.Body.String(), prefix+"{\"one\":\"hello\",\"two\":\"world\"}") |
| 53 |
} |
| 54 |
|
| 55 |
func TestJSONIndented(t *testing.T) { |
| 56 |
render := New(Options{ |
| 57 |
IndentJSON: true, |
| 58 |
}) |
| 59 |
|
| 60 |
var err error |
| 61 |
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 62 |
err = render.JSON(w, http.StatusOK, Greeting{"hello", "world"}) |
| 63 |
}) |
| 64 |
|
| 65 |
res := httptest.NewRecorder() |
| 66 |
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 67 |
h.ServeHTTP(res, req) |
| 68 |
|
| 69 |
expectNil(t, err) |
| 70 |
expect(t, res.Code, http.StatusOK) |
| 71 |
expect(t, res.Header().Get(ContentType), ContentJSON+"; charset=UTF-8") |
| 72 |
expect(t, res.Body.String(), "{\n \"one\": \"hello\",\n \"two\": \"world\"\n}\n") |
| 73 |
} |
| 74 |
|
| 75 |
func TestJSONConsumeIndented(t *testing.T) { |
| 76 |
render := New(Options{ |
| 77 |
IndentJSON: true, |
| 78 |
}) |
| 79 |
|
| 80 |
var renErr error |
| 81 |
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 82 |
renErr = render.JSON(w, http.StatusOK, Greeting{"hello", "world"}) |
| 83 |
}) |
| 84 |
|
| 85 |
res := httptest.NewRecorder() |
| 86 |
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 87 |
h.ServeHTTP(res, req) |
| 88 |
|
| 89 |
var output Greeting |
| 90 |
err := json.Unmarshal(res.Body.Bytes(), &output) |
| 91 |
expectNil(t, err) |
| 92 |
expectNil(t, renErr) |
| 93 |
expect(t, output.One, "hello") |
| 94 |
expect(t, output.Two, "world") |
| 95 |
} |
| 96 |
|
| 97 |
func TestJSONWithError(t *testing.T) { |
| 98 |
render := New(Options{}, Options{}, Options{}, Options{}) |
| 99 |
|
| 100 |
var err error |
| 101 |
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 102 |
err = render.JSON(w, 299, math.NaN()) |
| 103 |
}) |
| 104 |
|
| 105 |
res := httptest.NewRecorder() |
| 106 |
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 107 |
h.ServeHTTP(res, req) |
| 108 |
|
| 109 |
expectNotNil(t, err) |
| 110 |
expect(t, res.Code, 500) |
| 111 |
} |
| 112 |
|
| 113 |
func TestJSONWithOutUnEscapeHTML(t *testing.T) { |
| 114 |
render := New(Options{ |
| 115 |
UnEscapeHTML: false, |
| 116 |
}) |
| 117 |
var err error |
| 118 |
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 119 |
err = render.JSON(w, http.StatusOK, Greeting{"<span>test&test</span>", "<div>test&test</div>"}) |
| 120 |
}) |
| 121 |
|
| 122 |
res := httptest.NewRecorder() |
| 123 |
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 124 |
h.ServeHTTP(res, req) |
| 125 |
|
| 126 |
expectNil(t, err) |
| 127 |
expect(t, res.Body.String(), `{"one":"\u003cspan\u003etest\u0026test\u003c/span\u003e","two":"\u003cdiv\u003etest\u0026test\u003c/div\u003e"}`) |
| 128 |
} |
| 129 |
|
| 130 |
func TestJSONWithUnEscapeHTML(t *testing.T) { |
| 131 |
render := New(Options{ |
| 132 |
UnEscapeHTML: true, |
| 133 |
}) |
| 134 |
var err error |
| 135 |
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 136 |
err = render.JSON(w, http.StatusOK, Greeting{"<span>test&test</span>", "<div>test&test</div>"}) |
| 137 |
}) |
| 138 |
|
| 139 |
res := httptest.NewRecorder() |
| 140 |
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 141 |
h.ServeHTTP(res, req) |
| 142 |
|
| 143 |
expectNil(t, err) |
| 144 |
expect(t, res.Body.String(), "{\"one\":\"<span>test&test</span>\",\"two\":\"<div>test&test</div>\"}") |
| 145 |
} |
| 146 |
|
| 147 |
func TestJSONStream(t *testing.T) { |
| 148 |
render := New(Options{ |
| 149 |
StreamingJSON: true, |
| 150 |
}) |
| 151 |
var err error |
| 152 |
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 153 |
err = render.JSON(w, 299, Greeting{"hello", "world"}) |
| 154 |
}) |
| 155 |
|
| 156 |
res := httptest.NewRecorder() |
| 157 |
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 158 |
h.ServeHTTP(res, req) |
| 159 |
|
| 160 |
expectNil(t, err) |
| 161 |
expect(t, res.Code, 299) |
| 162 |
expect(t, res.Header().Get(ContentType), ContentJSON+"; charset=UTF-8") |
| 163 |
expect(t, res.Body.String(), "{\"one\":\"hello\",\"two\":\"world\"}\n") |
| 164 |
} |
| 165 |
|
| 166 |
func TestJSONStreamPrefix(t *testing.T) { |
| 167 |
prefix := ")]}',\n" |
| 168 |
render := New(Options{ |
| 169 |
PrefixJSON: []byte(prefix), |
| 170 |
StreamingJSON: true, |
| 171 |
}) |
| 172 |
|
| 173 |
var err error |
| 174 |
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 175 |
err = render.JSON(w, 300, Greeting{"hello", "world"}) |
| 176 |
}) |
| 177 |
|
| 178 |
res := httptest.NewRecorder() |
| 179 |
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 180 |
h.ServeHTTP(res, req) |
| 181 |
|
| 182 |
expectNil(t, err) |
| 183 |
expect(t, res.Code, 300) |
| 184 |
expect(t, res.Header().Get(ContentType), ContentJSON+"; charset=UTF-8") |
| 185 |
expect(t, res.Body.String(), prefix+"{\"one\":\"hello\",\"two\":\"world\"}\n") |
| 186 |
} |
| 187 |
|
| 188 |
func TestJSONStreamWithError(t *testing.T) { |
| 189 |
render := New(Options{ |
| 190 |
StreamingJSON: true, |
| 191 |
}) |
| 192 |
|
| 193 |
var err error |
| 194 |
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 195 |
err = render.JSON(w, 299, math.NaN()) |
| 196 |
}) |
| 197 |
|
| 198 |
res := httptest.NewRecorder() |
| 199 |
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 200 |
h.ServeHTTP(res, req) |
| 201 |
|
| 202 |
expectNotNil(t, err) |
| 203 |
expect(t, res.Code, 299) |
| 204 |
|
| 205 |
|
| 206 |
expect(t, res.Body.String(), "json: unsupported value: NaN\n") |
| 207 |
|
| 208 |
expect(t, res.Header().Get(ContentType), "text/plain; charset=utf-8") |
| 209 |
} |
| 210 |
|
| 211 |
func TestJSONCharset(t *testing.T) { |
| 212 |
render := New(Options{ |
| 213 |
Charset: "foobar", |
| 214 |
}) |
| 215 |
|
| 216 |
var err error |
| 217 |
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 218 |
err = render.JSON(w, 300, Greeting{"hello", "world"}) |
| 219 |
}) |
| 220 |
|
| 221 |
res := httptest.NewRecorder() |
| 222 |
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 223 |
h.ServeHTTP(res, req) |
| 224 |
|
| 225 |
expectNil(t, err) |
| 226 |
expect(t, res.Code, 300) |
| 227 |
expect(t, res.Header().Get(ContentType), ContentJSON+"; charset=foobar") |
| 228 |
expect(t, res.Body.String(), "{\"one\":\"hello\",\"two\":\"world\"}") |
| 229 |
} |
| 230 |
|
| 231 |
func TestJSONCustomContentType(t *testing.T) { |
| 232 |
render := New(Options{ |
| 233 |
JSONContentType: "application/vnd.api+json", |
| 234 |
}) |
| 235 |
|
| 236 |
var err error |
| 237 |
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 238 |
err = render.JSON(w, http.StatusOK, Greeting{"hello", "world"}) |
| 239 |
}) |
| 240 |
|
| 241 |
res := httptest.NewRecorder() |
| 242 |
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 243 |
h.ServeHTTP(res, req) |
| 244 |
|
| 245 |
expectNil(t, err) |
| 246 |
expect(t, res.Code, http.StatusOK) |
| 247 |
expect(t, res.Header().Get(ContentType), "application/vnd.api+json; charset=UTF-8") |
| 248 |
expect(t, res.Body.String(), "{\"one\":\"hello\",\"two\":\"world\"}") |
| 249 |
} |
| 250 |
|
| 251 |
func TestJSONDisabledCharset(t *testing.T) { |
| 252 |
render := New(Options{ |
| 253 |
DisableCharset: true, |
| 254 |
}) |
| 255 |
|
| 256 |
var err error |
| 257 |
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 258 |
err = render.JSON(w, http.StatusOK, Greeting{"hello", "world"}) |
| 259 |
}) |
| 260 |
|
| 261 |
res := httptest.NewRecorder() |
| 262 |
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 263 |
h.ServeHTTP(res, req) |
| 264 |
|
| 265 |
expectNil(t, err) |
| 266 |
expect(t, res.Code, http.StatusOK) |
| 267 |
expect(t, res.Header().Get(ContentType), ContentJSON) |
| 268 |
expect(t, res.Body.String(), "{\"one\":\"hello\",\"two\":\"world\"}") |
| 269 |
} |