Skip to content

Commit

Permalink
Added logic and unit tests for a simple version of Header forwarding …
Browse files Browse the repository at this point in the history
…(from responses from api.xmidt to some TR1D1UM caller)
  • Loading branch information
joe94 committed Oct 19, 2017
1 parent 4edb4c3 commit 91705c4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
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

0 comments on commit 91705c4

Please sign in to comment.