From 3a4d35011e4a52d877a5fcb0aa494bbb4d20e27b Mon Sep 17 00:00:00 2001 From: sapessi Date: Fri, 13 Jul 2018 08:33:04 -0700 Subject: [PATCH] Detecting content type automatically to address issue #16 --- core/response.go | 9 +++++++++ core/response_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++- core/types.go | 3 ++- 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/core/response.go b/core/response.go index e503d88..2c4f2c8 100644 --- a/core/response.go +++ b/core/response.go @@ -13,6 +13,7 @@ import ( ) const defaultStatusCode = -1 +const contentTypeHeaderKey = "Content-Type" // ProxyResponseWriter implements http.ResponseWriter and adds the method // necessary to return an events.APIGatewayProxyResponse object @@ -46,6 +47,14 @@ func (r *ProxyResponseWriter) Write(body []byte) (int, error) { r.status = http.StatusOK } + // if the content type header is not set when we write the body we try to + // detect one and set it by default. If the content type cannot be detected + // it is automatically set to "application/octet-stream" by the + // DetectContentType method + if r.Header().Get(contentTypeHeaderKey) == "" { + r.Header().Add(contentTypeHeaderKey, http.DetectContentType(body)) + } + return (&r.body).Write(body) } diff --git a/core/response_test.go b/core/response_test.go index f525ce5..8203ab1 100644 --- a/core/response_test.go +++ b/core/response_test.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "math/rand" "net/http" + "strings" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -49,6 +50,50 @@ var _ = Describe("ResponseWriter tests", func() { }) }) + Context("Automatically set response content type", func() { + xmlBodyContent := "ToveJaniReminderDon't forget me this weekend!" + htmlBodyContent := " Title of the documentContent of the document......" + It("Does not set the content type if it's already set", func() { + resp := NewProxyResponseWriter() + resp.Header().Add("Content-Type", "application/json") + + resp.Write([]byte(xmlBodyContent)) + + Expect("application/json").To(Equal(resp.Header().Get("Content-Type"))) + proxyResp, err := resp.GetProxyResponse() + Expect(err).To(BeNil()) + Expect(1).To(Equal(len(proxyResp.Headers))) + Expect("application/json").To(Equal(proxyResp.Headers["Content-Type"])) + Expect(xmlBodyContent).To(Equal(proxyResp.Body)) + }) + + It("Sets the conte type to text/xml given the body", func() { + resp := NewProxyResponseWriter() + resp.Write([]byte(xmlBodyContent)) + + Expect("").ToNot(Equal(resp.Header().Get("Content-Type"))) + Expect(true).To(Equal(strings.HasPrefix(resp.Header().Get("Content-Type"), "text/xml;"))) + proxyResp, err := resp.GetProxyResponse() + Expect(err).To(BeNil()) + Expect(1).To(Equal(len(proxyResp.Headers))) + Expect(true).To(Equal(strings.HasPrefix(proxyResp.Headers["Content-Type"], "text/xml;"))) + Expect(xmlBodyContent).To(Equal(proxyResp.Body)) + }) + + It("Sets the conte type to text/html given the body", func() { + resp := NewProxyResponseWriter() + resp.Write([]byte(htmlBodyContent)) + + Expect("").ToNot(Equal(resp.Header().Get("Content-Type"))) + Expect(true).To(Equal(strings.HasPrefix(resp.Header().Get("Content-Type"), "text/html;"))) + proxyResp, err := resp.GetProxyResponse() + Expect(err).To(BeNil()) + Expect(1).To(Equal(len(proxyResp.Headers))) + Expect(true).To(Equal(strings.HasPrefix(proxyResp.Headers["Content-Type"], "text/html;"))) + Expect(htmlBodyContent).To(Equal(proxyResp.Body)) + }) + }) + Context("Export API Gateway proxy response", func() { emtpyResponse := NewProxyResponseWriter() emtpyResponse.Header().Add("Content-Type", "application/json") @@ -70,7 +115,7 @@ var _ = Describe("ResponseWriter tests", func() { Expect("hello").To(Equal(proxyResponse.Body)) Expect(http.StatusOK).To(Equal(proxyResponse.StatusCode)) Expect(1).To(Equal(len(proxyResponse.Headers))) - Expect("text/plain").To(Equal(proxyResponse.Headers["Content-Type"])) + Expect(true).To(Equal(strings.HasPrefix(proxyResponse.Headers["Content-Type"], "text/plain"))) Expect(proxyResponse.IsBase64Encoded).To(BeFalse()) }) diff --git a/core/types.go b/core/types.go index c7f500e..f6fa4ea 100644 --- a/core/types.go +++ b/core/types.go @@ -7,11 +7,12 @@ import ( "github.com/aws/aws-lambda-go/events" ) -// Returns a dafault Gateway Timeout (504) response +// GatewayTimeout returns a dafault Gateway Timeout (504) response func GatewayTimeout() events.APIGatewayProxyResponse { return events.APIGatewayProxyResponse{StatusCode: http.StatusGatewayTimeout} } +// New Logged Error func NewLoggedError(format string, a ...interface{}) error { err := fmt.Errorf(format, a...) fmt.Println(err.Error())