diff --git a/router.go b/router.go index b6b0a25..9523403 100644 --- a/router.go +++ b/router.go @@ -138,6 +138,7 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse { response.StatusCode = http.StatusNotFound response.Body = string(respbody) + response.Headers = r.headers return response } @@ -162,13 +163,14 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse { respbody, _ := json.Marshal(map[string]string{"error": err.Error()}) if strings.Contains(err.Error(), "record not found") { status = 204 - } else if status < 400 { + } else if status != 204 && status < 400 { status = 400 } log.Printf("%v error: %v", status, err.Error()) response.StatusCode = status response.Body = string(respbody) + response.Headers = r.headers return response } } diff --git a/router_test.go b/router_test.go index f9cc168..fe4e567 100644 --- a/router_test.go +++ b/router_test.go @@ -70,6 +70,10 @@ func TestRouterSpec(t *testing.T) { So(response.StatusCode, ShouldEqual, http.StatusNotFound) So(response.Body, ShouldEqual, "{\"error\":\"no route matching path found\"}") + So(response.Headers, ShouldResemble, map[string]string{ + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Credentials": "true", + }) }) }) @@ -112,7 +116,7 @@ func TestRouterSpec(t *testing.T) { Convey("When the handler func does return a record not found", func() { hdlrfunc := func(ctx *APIGContext) { - ctx.Status = http.StatusBadRequest + ctx.Status = http.StatusNoContent ctx.Body = []byte("hello") ctx.Err = errors.New("record not found")