From ffb402170eb2b042f76a346670060c7ed2e2bd8f Mon Sep 17 00:00:00 2001 From: Benjamin Fagin Date: Sun, 8 Nov 2020 18:51:31 -0800 Subject: [PATCH] #38 ensure that headers and status code get written, even if nothing was ever written. This better aligns with the golang http package's own behavior to write on close. --- v2/response.go | 3 +++ v2/response_test.go | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/v2/response.go b/v2/response.go index 33c2c20..be5f71f 100644 --- a/v2/response.go +++ b/v2/response.go @@ -92,6 +92,9 @@ func (w *ResponseWriter) End() events.APIGatewayV2HTTPResponse { w.out.Cookies = w.header["Set-Cookie"] w.header.Del("Set-Cookie") + // ensure headers are written out + w.Write([]byte{}) + // notify end w.closeNotifyCh <- true diff --git a/v2/response_test.go b/v2/response_test.go index 47e1357..025cee4 100644 --- a/v2/response_test.go +++ b/v2/response_test.go @@ -105,3 +105,12 @@ func TestResponseWriter_WriteHeader(t *testing.T) { assert.Equal(t, "Not Found\n", e.Body) assert.Equal(t, "text/plain; charset=utf8", e.Headers["Content-Type"]) } + +func TestResponseWriter_WriteHeadersWhenEmpty(t *testing.T) { + w := NewResponse() + w.Header().Set("Content-Type", "text/xml") + + e := w.End() + assert.Equal(t, 200, e.StatusCode) + assert.Equal(t, "text/xml", e.Headers["Content-Type"]) +}