Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Warns if response is attempted to be written twice #2059

Merged
merged 5 commits into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
282 changes: 141 additions & 141 deletions packrd/packed-packr.go

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ type Response struct {
}

// WriteHeader sets the status code for a response
func (w *Response) WriteHeader(i int) {
w.Status = i
w.ResponseWriter.WriteHeader(i)
func (w *Response) WriteHeader(code int) {
if code == w.Status {
return
}

if w.Status > 0 {
fmt.Printf("[WARNING] Headers were already written. Wanted to override status code %d with %d", w.Status, code)
return
}

w.Status = code
w.ResponseWriter.WriteHeader(code)
}

// Write the body of the response
Expand Down
22 changes: 22 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package buffalo

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func Test_Response_MultipleWrite(t *testing.T) {
r := require.New(t)
resWr := httptest.NewRecorder()
res := Response{
ResponseWriter: resWr,
}

res.WriteHeader(http.StatusOK)
res.WriteHeader(http.StatusInternalServerError)

r.Equal(res.Status, http.StatusOK)
}
2 changes: 1 addition & 1 deletion runtime/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package runtime

// Version is the current version of the buffalo binary
var Version = "v0.16.16"
var Version = "v0.16.17"