fix: don't flush status immediately
Closes: #148 Co-authored-by: Roberto Abdelkader Martínez Pérez <robertomartinezp@gmail.com>
This commit is contained in:
@@ -194,7 +194,6 @@ func setResponseStatus(w http.ResponseWriter, r *http.Request, h *model.Handler)
|
||||
httperror.ErrorJSON(w, InvalidStatusCode, http.StatusBadRequest)
|
||||
} else {
|
||||
h.Status = si
|
||||
h.Writer.WriteHeader(si)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,6 +222,12 @@ func setResponseCookies(w http.ResponseWriter, r *http.Request, h *model.Handler
|
||||
}
|
||||
|
||||
func setResponseBody(w http.ResponseWriter, r *http.Request, h *model.Handler) {
|
||||
|
||||
if !h.BodyOut {
|
||||
h.Writer.WriteHeader(h.Status)
|
||||
h.BodyOut = true
|
||||
}
|
||||
|
||||
n, err := io.Copy(h.Writer, r.Body)
|
||||
if err != nil {
|
||||
if n > 0 {
|
||||
|
||||
@@ -1319,7 +1319,7 @@ func TestSetResponseStatusSetsGivenStatus(t *testing.T) {
|
||||
setResponseStatus(w, r, &h)
|
||||
|
||||
res := hw.Result()
|
||||
if res.StatusCode != http.StatusTeapot {
|
||||
if h.Status != http.StatusTeapot {
|
||||
t.Errorf("Status code mismatch. Expected: 418, Got: %d", res.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user