Skip to content

Commit

Permalink
Add Unwrap method to http.ResponseWriter implementations. (#283)
Browse files Browse the repository at this point in the history
Also update httpsnoop to version 1.0.3, which supports Unwrap method as well.
  • Loading branch information
pstibrany authored May 9, 2023
1 parent d3f132e commit c01ae0b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions middleware/errorhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func newErrorInterceptor(w http.ResponseWriter, code int) *errorInterceptor {
return &i
}

// Unwrap method is used by http.ResponseController to get access to original http.ResponseWriter.
func (i *errorInterceptor) Unwrap() http.ResponseWriter {
return i.originalWriter
}

// Header implements http.ResponseWriter
func (i *errorInterceptor) Header() http.Header {
return i.headers
Expand Down
5 changes: 5 additions & 0 deletions middleware/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func newBadResponseLoggingWriter(rw http.ResponseWriter, buffer io.Writer) badRe
return &b
}

// Unwrap method is used by http.ResponseController to get access to original http.ResponseWriter.
func (b *nonFlushingBadResponseLoggingWriter) Unwrap() http.ResponseWriter {
return b.rw
}

// Header returns the header map that will be sent by WriteHeader.
// Implements ResponseWriter.
func (b *nonFlushingBadResponseLoggingWriter) Header() http.Header {
Expand Down
10 changes: 10 additions & 0 deletions middleware/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,13 @@ func TestBadResponseLoggingWriter_WithAndWithoutFlusher(t *testing.T) {
t.Errorf("Flush should have worked but did not")
}
}

type responseWriterWithUnwrap interface {
http.ResponseWriter
Unwrap() http.ResponseWriter
}

// Verify that custom http.ResponseWriter implementations implement Unwrap() method, used by http.ResponseContoller.
var _ responseWriterWithUnwrap = &nonFlushingBadResponseLoggingWriter{}
var _ responseWriterWithUnwrap = &flushingBadResponseLoggingWriter{}
var _ responseWriterWithUnwrap = &errorInterceptor{}

0 comments on commit c01ae0b

Please sign in to comment.