1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-22 20:06:21 +02:00

Fix XML double WriteHeader (#677)

* WriteHeader if not committed in test.Response

Call WriteHeader if response isn't committed yet in calls to Write to
behave the same as fasthttp and standard Responses.

* Fix XML double WriteHeader

XMLBlob was writing xml.Header to the reply before writing the header
triggering a WriteHeader(http.StatusOK).
This commit is contained in:
Jordon Smith 2016-10-25 15:56:41 +01:00 committed by Vishal Rana
parent 459f5647e6
commit 60b69fbb3b
3 changed files with 9 additions and 3 deletions

View File

@ -390,10 +390,13 @@ func (c *context) XML(code int, i interface{}) (err error) {
}
func (c *context) XMLBlob(code int, b []byte) (err error) {
c.response.Header().Set(HeaderContentType, MIMEApplicationXMLCharsetUTF8)
c.response.WriteHeader(code)
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
return
}
return c.Blob(code, MIMEApplicationXMLCharsetUTF8, b)
_, err = c.response.Write(b)
return
}
func (c *context) Blob(code int, contentType string, b []byte) (err error) {

View File

@ -99,9 +99,9 @@ func TestContext(t *testing.T) {
// XML
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*context)
err = c.XML(http.StatusOK, user{1, "Jon Snow"})
err = c.XML(http.StatusCreated, user{1, "Jon Snow"})
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, http.StatusCreated, rec.Status())
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, xml.Header+userXML, rec.Body.String())
}

View File

@ -55,6 +55,9 @@ func (r *Response) WriteHeader(code int) {
}
func (r *Response) Write(b []byte) (n int, err error) {
if !r.committed {
r.WriteHeader(http.StatusOK)
}
n, err = r.writer.Write(b)
r.size += int64(n)
return