Skip to content

Commit

Permalink
add unit tests for response_writer
Browse files Browse the repository at this point in the history
  • Loading branch information
potterbm-cb committed Nov 27, 2024
1 parent 43d8896 commit b963a54
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions headerforwarder/response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
// responses, and set on the rosetta response.
type ResponseWriter struct {
writer http.ResponseWriter
calledWriteHeaders bool
RosettaRequestID string
GetAdditionalHeaders func(string) (http.Header, bool)
}
Expand All @@ -46,11 +47,16 @@ func (hfrw *ResponseWriter) Header() http.Header {

// Write passes through to the underlying ResponseWriter instance
func (hfrw *ResponseWriter) Write(b []byte) (int, error) {
if !hfrw.calledWriteHeaders {
hfrw.AddExtractedHeaders()
}

return hfrw.writer.Write(b)
}

// WriteHeader will add any final extracted headers, and then pass through to the underlying ResponseWriter instance
func (hfrw *ResponseWriter) WriteHeader(statusCode int) {
hfrw.calledWriteHeaders = true
hfrw.AddExtractedHeaders()
hfrw.writer.WriteHeader(statusCode)
}
Expand Down
56 changes: 56 additions & 0 deletions headerforwarder/response_writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package headerforwarder

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

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

func mockGetter(id string, headers http.Header) func(string) (http.Header, bool) {
return func(s string) (http.Header, bool) {
if s != id {
return nil, false
}

return headers, true
}
}

var mockHeader http.Header = http.Header{
"Test-Header": []string{"test-value"},
}

func TestWriteHeader(t *testing.T) {
writer := httptest.NewRecorder()
hfrw := NewResponseWriter(writer, "test-id", mockGetter("test-id", mockHeader))

hfrw.WriteHeader(200)

result := writer.Result()
assert.Equal(t, 200, result.StatusCode)
}

func TestWriteHeaderAddsHeaders(t *testing.T) {
writer := httptest.NewRecorder()
hfrw := NewResponseWriter(writer, "test-id", mockGetter("test-id", mockHeader))

hfrw.WriteHeader(200)

result := writer.Result()
assert.Equal(t, 200, result.StatusCode)
assert.Equal(t, mockHeader.Values("test-header"), result.Header.Values("test-header"))
}

// Tests that headers are written if Write is called before WriteHeader
func TestWriteAddsHeaders(t *testing.T) {
writer := httptest.NewRecorder()
hfrw := NewResponseWriter(writer, "test-id", mockGetter("test-id", mockHeader))

hfrw.Write([]byte("test-bytes"))

Check failure on line 51 in headerforwarder/response_writer_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `hfrw.Write` is not checked (errcheck)

result := writer.Result()
assert.Equal(t, 200, result.StatusCode)
assert.Equal(t, mockHeader.Values("test-header"), result.Header.Values("test-header"))
}

0 comments on commit b963a54

Please sign in to comment.