Пример #1
0
func TestWriteXML(t *testing.T) {
	assert := assert.New(t)
	b := Sample{
		Hello: "world",
	}
	// test content-negotiation application/xml
	w := httptest.NewRecorder()
	r, err := http.NewRequest("GET", "/test", nil)
	r.Header.Set("Accept", "application/xml")
	err = shttp.Write(w, r, b, 200)
	assert.NoError(err)
	assert.Equal(w.Code, 200)
	assert.Equal(w.Body.String(), "<Sample><hello>world</hello></Sample>")
}
Пример #2
0
func TestWriteJSON(t *testing.T) {
	assert := assert.New(t)
	var b = struct {
		Hello string `json:"hello"`
	}{"world"}
	w := httptest.NewRecorder()
	r, err := http.NewRequest("GET", "/test", nil)
	assert.NoError(err)

	// test content-negotiation application/json
	r.Header.Set("Accept", "application/json")
	err = shttp.Write(w, r, b, 200)
	assert.NoError(err)
	assert.Equal(w.Code, 200)
	assert.Equal(w.Body.String(), `{"hello":"world"}`)
}