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

Added logic and unit tests for a simple version of Header forwarding … #10

Merged
merged 1 commit into from
Oct 19, 2017
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
18 changes: 18 additions & 0 deletions src/tr1d1um/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Comcast/webpa-common/logging"
"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
"strings"
)

//ConversionHandler wraps the main WDMP -> WRP conversion method
Expand Down Expand Up @@ -67,5 +68,22 @@ func (ch *ConversionHandler) ServeHTTP(origin http.ResponseWriter, req *http.Req
ctx, cancel := context.WithTimeout(req.Context(), ch.sender.GetRespTimeout())
response, err := ch.sender.Send(ch, origin, wdmpPayload, req.WithContext(ctx))
cancel()

ForwardHeadersByPrefix("X", origin, response)

ch.sender.HandleResponse(ch, err, response, origin)
}

// Helper functions

//ForwardHeadersByPrefix forwards header values whose keys start with the given prefix from some response
//into an responseWriter
func ForwardHeadersByPrefix(prefix string, origin http.ResponseWriter, resp *http.Response){
for headerKey, headerValues := range resp.Header {
if strings.HasPrefix(headerKey, prefix) {
for _, headerValue := range headerValues {
origin.Header().Add(headerKey, headerValue)
}
}
}
}
40 changes: 40 additions & 0 deletions src/tr1d1um/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,46 @@ func TestConversionHandler(t *testing.T) {
})
}

func TestForwardHeadersByPrefix(t *testing.T) {
t.Run("NoHeaders", func(t *testing.T) {
assert := assert.New(t)
origin := httptest.NewRecorder()

ForwardHeadersByPrefix("H", origin, resp)
assert.Empty(origin.Header())
})

t.Run("MultipleHeadersFiltered", func(t *testing.T) {
assert := assert.New(t)
origin := httptest.NewRecorder()
resp := &http.Response{Header:http.Header{}}

resp.Header.Add("Helium", "3")
resp.Header.Add("Hydrogen", "5")
resp.Header.Add("Hydrogen", "6")

ForwardHeadersByPrefix("He", origin, resp)
assert.NotEmpty(origin.Header())
assert.EqualValues(1, len(origin.Header()))
assert.EqualValues("3", origin.Header().Get("Helium"))
})

t.Run("MultipleHeadersFilteredFullArray", func(t *testing.T) {
assert := assert.New(t)
origin := httptest.NewRecorder()
resp := &http.Response{Header:http.Header{}}

resp.Header.Add("Helium", "3")
resp.Header.Add("Hydrogen", "5")
resp.Header.Add("Hydrogen", "6")

ForwardHeadersByPrefix("H", origin, resp)
assert.NotEmpty(origin.Header())
assert.EqualValues(2, len(origin.Header()))
assert.EqualValues([]string{"5","6"}, origin.Header()["Hydrogen"])
})
}

func SetUpTest(encodeArg interface{}, req *http.Request) {
recorder := httptest.NewRecorder()
timeout := time.Nanosecond
Expand Down