| 142 | } |
| 143 | |
| 144 | func TestWriteJSON(t *testing.T) { |
| 145 | // without indent |
| 146 | { |
| 147 | w := httptest.NewRecorder() |
| 148 | JSON(w, http.StatusTeapot, map[string]string{"hello": "world"}) |
| 149 | if got, want := w.Body.String(), "{\"hello\":\"world\"}\n"; got != want { |
| 150 | t.Errorf("Want JSON body %q, got %q", want, got) |
| 151 | } |
| 152 | if got, want := w.Header().Get("Content-Type"), "application/json; charset=utf-8"; got != want { |
| 153 | t.Errorf("Want Content-Type %q, got %q", want, got) |
| 154 | } |
| 155 | if got, want := w.Code, http.StatusTeapot; got != want { |
| 156 | t.Errorf("Want status code %d, got %d", want, got) |
| 157 | } |
| 158 | } |
| 159 | // with indent |
| 160 | { |
| 161 | indent = true |
| 162 | defer func() { |
| 163 | indent = false |
| 164 | }() |
| 165 | w := httptest.NewRecorder() |
| 166 | JSON(w, http.StatusTeapot, map[string]string{"hello": "world"}) |
| 167 | if got, want := w.Body.String(), "{\n \"hello\": \"world\"\n}\n"; got != want { |
| 168 | t.Errorf("Want JSON body %q, got %q", want, got) |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | func TestJSONArrayDynamic(t *testing.T) { |
| 174 | noctx := context.Background() |