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:
parent
459f5647e6
commit
60b69fbb3b
@ -390,10 +390,13 @@ func (c *context) XML(code int, i interface{}) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) XMLBlob(code int, b []byte) (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 {
|
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
|
||||||
return
|
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) {
|
func (c *context) Blob(code int, contentType string, b []byte) (err error) {
|
||||||
|
@ -99,9 +99,9 @@ func TestContext(t *testing.T) {
|
|||||||
// XML
|
// XML
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
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) {
|
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, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||||
assert.Equal(t, xml.Header+userXML, rec.Body.String())
|
assert.Equal(t, xml.Header+userXML, rec.Body.String())
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,9 @@ func (r *Response) WriteHeader(code int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) Write(b []byte) (n int, err error) {
|
func (r *Response) Write(b []byte) (n int, err error) {
|
||||||
|
if !r.committed {
|
||||||
|
r.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
n, err = r.writer.Write(b)
|
n, err = r.writer.Write(b)
|
||||||
r.size += int64(n)
|
r.size += int64(n)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user