Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fsttest: discard header changes after send #124

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 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
headersDone bool
}

// NewRecorder returns an initialized ResponseRecorder.
Expand All @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding a comment here to explain the purpose? I'm not sure it's obvious without additional context.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point and I agree. Being part of our test harness, I was initally being more liberal about the lack of comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I will update the PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Let me know if that works for you.

}

// 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
Expand Down