From 0e1556c1aa95cbea653809d5148ba57b12b68410 Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Thu, 27 Jun 2024 10:58:27 +0100 Subject: [PATCH 1/3] Discard changes after we send the response This should correctly ignore headers set too late to make it into the response. --- fsttest/recorder.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/fsttest/recorder.go b/fsttest/recorder.go index 7e4a60e..d7d4817 100644 --- a/fsttest/recorder.go +++ b/fsttest/recorder.go @@ -12,9 +12,10 @@ import ( // ResponseRecorder is an implementation of fsthttp.ResponseWriter that // records its mutations for later inspection in tests. type ResponseRecorder struct { - Code int - HeaderMap fsthttp.Header - Body *bytes.Buffer + Code int + HeaderMap fsthttp.Header + Body *bytes.Buffer + headersDone bool } // NewRecorder returns an initialized ResponseRecorder. @@ -28,12 +29,18 @@ func NewRecorder() *ResponseRecorder { // Header returns the response headers to mutate within a handler. func (r *ResponseRecorder) Header() fsthttp.Header { - return r.HeaderMap + if !r.headersDone { + return r.HeaderMap + } + return r.HeaderMap.Clone() } // WriteHeader records the response code. func (r *ResponseRecorder) WriteHeader(code int) { - r.Code = code + if !r.headersDone { + r.Code = code + r.headersDone = true + } } // Write records the response body. The data is written to the Body From af333b6f52fab1bb8f916ee6c99200abbfff344d Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Fri, 28 Jun 2024 10:16:34 +0100 Subject: [PATCH 2/3] Add comment --- fsttest/recorder.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fsttest/recorder.go b/fsttest/recorder.go index d7d4817..2283da9 100644 --- a/fsttest/recorder.go +++ b/fsttest/recorder.go @@ -32,6 +32,8 @@ func (r *ResponseRecorder) Header() fsthttp.Header { if !r.headersDone { return r.HeaderMap } + // Once the send the headers, return a copy so any changes + // are discarded. return r.HeaderMap.Clone() } From 21c408f385a110afc0bb35b568b7e02440aa7229 Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Fri, 28 Jun 2024 16:59:54 +0100 Subject: [PATCH 3/3] Rename --- fsttest/recorder.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/fsttest/recorder.go b/fsttest/recorder.go index 2283da9..2648b75 100644 --- a/fsttest/recorder.go +++ b/fsttest/recorder.go @@ -15,7 +15,7 @@ type ResponseRecorder struct { Code int HeaderMap fsthttp.Header Body *bytes.Buffer - headersDone bool + headersSent bool } // NewRecorder returns an initialized ResponseRecorder. @@ -29,19 +29,18 @@ func NewRecorder() *ResponseRecorder { // Header returns the response headers to mutate within a handler. func (r *ResponseRecorder) Header() fsthttp.Header { - if !r.headersDone { + if !r.headersSent { return r.HeaderMap } - // Once the send the headers, return a copy so any changes - // are discarded. + // Once sent, return a copy so any changes are discarded. return r.HeaderMap.Clone() } // WriteHeader records the response code. func (r *ResponseRecorder) WriteHeader(code int) { - if !r.headersDone { + if !r.headersSent { r.Code = code - r.headersDone = true + r.headersSent = true } }