Skip to content

Commit

Permalink
Merge pull request #124 from fastly/fgsch/discard-changes-after-sent
Browse files Browse the repository at this point in the history
fsttest: discard header changes after send
  • Loading branch information
fgsch authored Jun 28, 2024
2 parents 8a69f9c + 21c408f commit a5aefc0
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions fsttest/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
headersSent bool
}

// NewRecorder returns an initialized ResponseRecorder.
Expand All @@ -28,12 +29,19 @@ func NewRecorder() *ResponseRecorder {

// Header returns the response headers to mutate within a handler.
func (r *ResponseRecorder) Header() fsthttp.Header {
return r.HeaderMap
if !r.headersSent {
return r.HeaderMap
}
// 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) {
r.Code = code
if !r.headersSent {
r.Code = code
r.headersSent = true
}
}

// Write records the response body. The data is written to the Body
Expand Down

0 comments on commit a5aefc0

Please sign in to comment.