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

add support for multi param and multi header #19

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func NewRequest(ctx context.Context, e events.APIGatewayProxyRequest) (*http.Req
for k, v := range e.QueryStringParameters {
q.Set(k, v)
}

for k, values := range e.MultiValueQueryStringParameters {
for _, v := range values {
q.Add(k, v)
}
}
u.RawQuery = q.Encode()

// base64 encoded body
Expand All @@ -52,6 +58,10 @@ func NewRequest(ctx context.Context, e events.APIGatewayProxyRequest) (*http.Req
req.Header.Set(k, v)
}

for k, values := range e.MultiValueHeaders {
req.Header[k] = values
}

// content-length
if req.Header.Get("Content-Length") == "" && body != "" {
req.Header.Set("Content-Length", strconv.Itoa(len(body)))
Expand Down
55 changes: 55 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ func TestNewRequest_queryString(t *testing.T) {
assert.Equal(t, `desc`, r.URL.Query().Get("order"))
}

func TestNewRequest_multiValueQueryString(t *testing.T) {
e := events.APIGatewayProxyRequest{
HTTPMethod: "GET",
Path: "/pets",
MultiValueQueryStringParameters: map[string][]string{
"multi_fields": []string{"name", "species"},
"multi_arr[]": []string{"arr1", "arr2"},
},
QueryStringParameters: map[string]string{
"order": "desc",
"fields": "name,species",
},
}

r, err := NewRequest(context.Background(), e)
assert.NoError(t, err)

assert.Equal(t, `/pets?fields=name%2Cspecies&multi_arr%5B%5D=arr1&multi_arr%5B%5D=arr2&multi_fields=name&multi_fields=species&order=desc`, r.URL.String())
assert.Equal(t, []string{"name", "species"}, r.URL.Query()["multi_fields"])
assert.Equal(t, []string{"arr1", "arr2"}, r.URL.Query()["multi_arr[]"])
}

func TestNewRequest_remoteAddr(t *testing.T) {
e := events.APIGatewayProxyRequest{
HTTPMethod: "GET",
Expand Down Expand Up @@ -95,6 +117,39 @@ func TestNewRequest_header(t *testing.T) {
assert.Equal(t, `bar`, r.Header.Get("X-Foo"))
}

func TestNewRequest_multiHeader(t *testing.T) {
e := events.APIGatewayProxyRequest{
HTTPMethod: "POST",
Path: "/pets",
Body: `{ "name": "Tobi" }`,
MultiValueHeaders: map[string][]string{
"X-APEX": []string{"apex1", "apex2"},
"X-APEX-2": []string{"apex-1", "apex-2"},
},
Headers: map[string]string{
"Content-Type": "application/json",
"X-Foo": "bar",
"Host": "example.com",
},
RequestContext: events.APIGatewayProxyRequestContext{
RequestID: "1234",
Stage: "prod",
},
}

r, err := NewRequest(context.Background(), e)
assert.NoError(t, err)

assert.Equal(t, `example.com`, r.Host)
assert.Equal(t, `prod`, r.Header.Get("X-Stage"))
assert.Equal(t, `1234`, r.Header.Get("X-Request-Id"))
assert.Equal(t, `18`, r.Header.Get("Content-Length"))
assert.Equal(t, `application/json`, r.Header.Get("Content-Type"))
assert.Equal(t, `bar`, r.Header.Get("X-Foo"))
assert.Equal(t, []string{"apex1", "apex2"}, r.Header["X-APEX"])
assert.Equal(t, []string{"apex-1", "apex-2"}, r.Header["X-APEX-2"])
}

func TestNewRequest_body(t *testing.T) {
e := events.APIGatewayProxyRequest{
HTTPMethod: "POST",
Expand Down
8 changes: 6 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ func (w *ResponseWriter) WriteHeader(status int) {
w.out.StatusCode = status

h := make(map[string]string)
mvh := make(map[string][]string)

for k, v := range w.Header() {
if len(v) > 0 {
h[k] = v[len(v)-1]
if len(v) == 1 {
h[k] = v[0]
} else if len(v) > 1 {
mvh[k] = v
}
}

w.out.Headers = h
w.out.MultiValueHeaders = mvh
w.wroteHeader = true
}

Expand Down
13 changes: 13 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ func TestResponseWriter_Header(t *testing.T) {
assert.Equal(t, "Bar: baz\r\nFoo: bar\r\n", buf.String())
}

func TestResponseWriter_multiHeader(t *testing.T) {
w := NewResponse()
w.Header().Set("Foo", "bar")
w.Header().Set("Bar", "baz")
w.Header().Add("X-APEX", "apex1")
w.Header().Add("X-APEX", "apex2")

var buf bytes.Buffer
w.header.Write(&buf)

assert.Equal(t, "Bar: baz\r\nFoo: bar\r\nX-Apex: apex1\r\nX-Apex: apex2\r\n", buf.String())
}

func TestResponseWriter_Write_text(t *testing.T) {
types := []string{
"text/x-custom",
Expand Down