context_17_test.go 801 B

123456789101112131415161718192021222324252627
  1. // Copyright 2018 Gin Core Team. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. // +build go1.7
  5. package gin
  6. import (
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. // Tests that the response is serialized as JSON
  13. // and Content-Type is set to application/json
  14. // and special HTML characters are preserved
  15. func TestContextRenderPureJSON(t *testing.T) {
  16. w := httptest.NewRecorder()
  17. c, _ := CreateTestContext(w)
  18. c.PureJSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})
  19. assert.Equal(t, http.StatusCreated, w.Code)
  20. assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())
  21. assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
  22. }