Merge 7d753188a7c06af5fa9fe8fb120bdb4d180cf659 into 22f118f3b604734b4782f0dd767171687ffd7774

This commit is contained in:
Josh Brandoff 2015-06-13 02:29:00 +00:00
commit 29d219126f
4 changed files with 50 additions and 1 deletions

View File

@ -331,6 +331,15 @@ func (c *Context) JSON(code int, obj interface{}) {
} }
} }
// Serializes the given struct as JSON into the response body.
// It also sets the Content-Type as "application/vnd.api+json".
func (c *Context) JSONAPI(code int, obj interface{}) {
c.writermem.WriteHeader(code)
if err := render.WriteJSONAPI(c.Writer, obj); err != nil {
c.renderError(err)
}
}
// Serializes the given struct as XML into the response body. // Serializes the given struct as XML into the response body.
// It also sets the Content-Type as "application/xml". // It also sets the Content-Type as "application/xml".
func (c *Context) XML(code int, obj interface{}) { func (c *Context) XML(code int, obj interface{}) {

View File

@ -219,6 +219,17 @@ func TestContextRenderJSON(t *testing.T) {
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8") assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
} }
// Tests that the response is serialized as JSON
// and Content-Type is set to application/vnd.api+json
func TestContextRenderJSON(t *testing.T) {
c, w, _ := createTestContext()
c.JSONAPI(201, H{"foo": "bar"})
assert.Equal(t, w.Code, 201)
assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/vnd.api+json")
}
// Tests that the response is serialized as JSON // Tests that the response is serialized as JSON
// and Content-Type is set to application/json // and Content-Type is set to application/json
func TestContextRenderIndentedJSON(t *testing.T) { func TestContextRenderIndentedJSON(t *testing.T) {

View File

@ -13,18 +13,29 @@ type (
JSON struct { JSON struct {
Data interface{} Data interface{}
} }
JSONAPI struct {
Data interface{}
}
IndentedJSON struct { IndentedJSON struct {
Data interface{} Data interface{}
} }
) )
var jsonContentType = []string{"application/json; charset=utf-8"} var (
jsonContentType = []string{"application/json; charset=utf-8"}
jsonAPIContentType = []string{"application/vnd.api+json"}
)
func (r JSON) Render(w http.ResponseWriter) error { func (r JSON) Render(w http.ResponseWriter) error {
return WriteJSON(w, r.Data) return WriteJSON(w, r.Data)
} }
func (r JSONAPI) Render(w http.ResponseWriter) error {
return WriteJSONAPI(w, r.Data)
}
func (r IndentedJSON) Render(w http.ResponseWriter) error { func (r IndentedJSON) Render(w http.ResponseWriter) error {
w.Header()["Content-Type"] = jsonContentType w.Header()["Content-Type"] = jsonContentType
jsonBytes, err := json.MarshalIndent(r.Data, "", " ") jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
@ -39,3 +50,8 @@ func WriteJSON(w http.ResponseWriter, obj interface{}) error {
w.Header()["Content-Type"] = jsonContentType w.Header()["Content-Type"] = jsonContentType
return json.NewEncoder(w).Encode(obj) return json.NewEncoder(w).Encode(obj)
} }
func WriteJSONAPI(w http.ResponseWriter, obj interface{}) error {
w.Header()["Content-Type"] = jsonAPIContentType
return json.NewEncoder(w).Encode(obj)
}

View File

@ -29,6 +29,19 @@ func TestRenderJSON(t *testing.T) {
assert.Equal(t, w.Header().Get("Content-Type"), "application/json; charset=utf-8") assert.Equal(t, w.Header().Get("Content-Type"), "application/json; charset=utf-8")
} }
func TestRenderJSONAPI(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]interface{}{
"foo": "bar",
}
err := (JSONAPI{data}).Render(w)
assert.NoError(t, err)
assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")
assert.Equal(t, w.Header().Get("Content-Type"), "application/vnd.api+json")
}
func TestRenderIndentedJSON(t *testing.T) { func TestRenderIndentedJSON(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
data := map[string]interface{}{ data := map[string]interface{}{