Skip to content

Commit

Permalink
Merge pull request #339 from SAPessi/develop
Browse files Browse the repository at this point in the history
Added Forward headers similar to API Gateway's
  • Loading branch information
PaulMaddox authored Apr 5, 2018
2 parents 2844be1 + c9bdb4b commit 290b208
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
12 changes: 12 additions & 0 deletions router/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"

"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -69,6 +70,17 @@ func NewEvent(req *http.Request, isBase64Encoded bool) (*Event, error) {
}
}

// add the forwarded headers we expect to see from an API Gateway request
if _, ok := headers["Host"]; !ok {
host := req.Host
if strings.Contains(host, ":") {
host = host[:strings.Index(host, ":")]
}
headers["Host"] = host
}
headers["X-Forwarded-Proto"] = req.URL.Scheme
headers["X-Forwarded-Port"] = req.URL.Port()

query := map[string]string{}
for name, values := range req.URL.Query() {
for _, value := range values {
Expand Down
34 changes: 34 additions & 0 deletions router/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,40 @@ var _ = Describe("Event", func() {
r.Router().ServeHTTP(rec, req)
})
})

Context("Includes forwarded headers", func() {
req, _ := http.NewRequest("GET", "http://localhost:3000/get", new(bytes.Buffer))

It("Includes Host header", func() {
r.AddFunction(function, func(w http.ResponseWriter, e *Event) {
hostHeader, ok := e.Headers["Host"]
Expect(ok).To(BeTrue())
Expect(hostHeader).To(BeIdenticalTo("localhost"))
})
rec := httptest.NewRecorder()
r.Router().ServeHTTP(rec, req)
})

It("Includes X-Forwarded-Proto header", func() {
r.AddFunction(function, func(w http.ResponseWriter, e *Event) {
hostHeader, ok := e.Headers["X-Forwarded-Proto"]
Expect(ok).To(BeTrue())
Expect(hostHeader).To(BeIdenticalTo("http"))
})
rec := httptest.NewRecorder()
r.Router().ServeHTTP(rec, req)
})

It("Includes X-Forwarded-Port header", func() {
r.AddFunction(function, func(w http.ResponseWriter, e *Event) {
hostHeader, ok := e.Headers["X-Forwarded-Port"]
Expect(ok).To(BeTrue())
Expect(hostHeader).To(BeIdenticalTo("3000"))
})
rec := httptest.NewRecorder()
r.Router().ServeHTTP(rec, req)
})
})
})

Context("with no parameters on the route", func() {
Expand Down

0 comments on commit 290b208

Please sign in to comment.