Skip to content

Commit

Permalink
fix nil resp header (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfallis authored May 16, 2023
1 parent 840bf33 commit 4e9090b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func LambdaAdapter(l LambdaHandler) func(http.ResponseWriter, *http.Request) {
}
}

if resp.Headers == nil {
resp.Headers = make(map[string]string)
}
resp.Headers["Content-Type"] = "application/json"
w.WriteHeader(resp.StatusCode)
for k, v := range resp.Headers {
Expand Down
24 changes: 24 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ func TestLambdaAdapter(t *testing.T) {
assert.Equal(t, "success", w.Body.String())
}

func TestLambdaAdapter_without_content_type(t *testing.T) {
l := g8.LambdaHandler{
Handler: func(ctx context.Context, r events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
return &events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
MultiValueHeaders: map[string][]string{"Set-Cookie": {"cookie1", "cookie2"}},
Body: "success",
}, nil
},
Method: http.MethodGet,
Path: "/test/url/path/{var1}/{var2}",
PathParams: []string{"var1", "var2"},
}

w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/test/url/path/var1/var2", nil)
g8.LambdaAdapter(l)(w, r)

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
assert.Equal(t, "cookie1,cookie2", w.Header().Get("Set-Cookie"))
assert.Equal(t, "success", w.Body.String())
}

func TestLambdaAdapter_g8_error(t *testing.T) {
l := g8.LambdaHandler{
Handler: func(ctx context.Context, r events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
Expand Down

0 comments on commit 4e9090b

Please sign in to comment.