Skip to content

Commit 3a4d350

Browse files
committed
Detecting content type automatically to address issue awslabs#16
1 parent 6986cdf commit 3a4d350

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

core/response.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
)
1414

1515
const defaultStatusCode = -1
16+
const contentTypeHeaderKey = "Content-Type"
1617

1718
// ProxyResponseWriter implements http.ResponseWriter and adds the method
1819
// necessary to return an events.APIGatewayProxyResponse object
@@ -46,6 +47,14 @@ func (r *ProxyResponseWriter) Write(body []byte) (int, error) {
4647
r.status = http.StatusOK
4748
}
4849

50+
// if the content type header is not set when we write the body we try to
51+
// detect one and set it by default. If the content type cannot be detected
52+
// it is automatically set to "application/octet-stream" by the
53+
// DetectContentType method
54+
if r.Header().Get(contentTypeHeaderKey) == "" {
55+
r.Header().Add(contentTypeHeaderKey, http.DetectContentType(body))
56+
}
57+
4958
return (&r.body).Write(body)
5059
}
5160

core/response_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/base64"
55
"math/rand"
66
"net/http"
7+
"strings"
78

89
. "github.com/onsi/ginkgo"
910
. "github.com/onsi/gomega"
@@ -49,6 +50,50 @@ var _ = Describe("ResponseWriter tests", func() {
4950
})
5051
})
5152

53+
Context("Automatically set response content type", func() {
54+
xmlBodyContent := "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"
55+
htmlBodyContent := " <!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Title of the document</title></head><body>Content of the document......</body></html>"
56+
It("Does not set the content type if it's already set", func() {
57+
resp := NewProxyResponseWriter()
58+
resp.Header().Add("Content-Type", "application/json")
59+
60+
resp.Write([]byte(xmlBodyContent))
61+
62+
Expect("application/json").To(Equal(resp.Header().Get("Content-Type")))
63+
proxyResp, err := resp.GetProxyResponse()
64+
Expect(err).To(BeNil())
65+
Expect(1).To(Equal(len(proxyResp.Headers)))
66+
Expect("application/json").To(Equal(proxyResp.Headers["Content-Type"]))
67+
Expect(xmlBodyContent).To(Equal(proxyResp.Body))
68+
})
69+
70+
It("Sets the conte type to text/xml given the body", func() {
71+
resp := NewProxyResponseWriter()
72+
resp.Write([]byte(xmlBodyContent))
73+
74+
Expect("").ToNot(Equal(resp.Header().Get("Content-Type")))
75+
Expect(true).To(Equal(strings.HasPrefix(resp.Header().Get("Content-Type"), "text/xml;")))
76+
proxyResp, err := resp.GetProxyResponse()
77+
Expect(err).To(BeNil())
78+
Expect(1).To(Equal(len(proxyResp.Headers)))
79+
Expect(true).To(Equal(strings.HasPrefix(proxyResp.Headers["Content-Type"], "text/xml;")))
80+
Expect(xmlBodyContent).To(Equal(proxyResp.Body))
81+
})
82+
83+
It("Sets the conte type to text/html given the body", func() {
84+
resp := NewProxyResponseWriter()
85+
resp.Write([]byte(htmlBodyContent))
86+
87+
Expect("").ToNot(Equal(resp.Header().Get("Content-Type")))
88+
Expect(true).To(Equal(strings.HasPrefix(resp.Header().Get("Content-Type"), "text/html;")))
89+
proxyResp, err := resp.GetProxyResponse()
90+
Expect(err).To(BeNil())
91+
Expect(1).To(Equal(len(proxyResp.Headers)))
92+
Expect(true).To(Equal(strings.HasPrefix(proxyResp.Headers["Content-Type"], "text/html;")))
93+
Expect(htmlBodyContent).To(Equal(proxyResp.Body))
94+
})
95+
})
96+
5297
Context("Export API Gateway proxy response", func() {
5398
emtpyResponse := NewProxyResponseWriter()
5499
emtpyResponse.Header().Add("Content-Type", "application/json")
@@ -70,7 +115,7 @@ var _ = Describe("ResponseWriter tests", func() {
70115
Expect("hello").To(Equal(proxyResponse.Body))
71116
Expect(http.StatusOK).To(Equal(proxyResponse.StatusCode))
72117
Expect(1).To(Equal(len(proxyResponse.Headers)))
73-
Expect("text/plain").To(Equal(proxyResponse.Headers["Content-Type"]))
118+
Expect(true).To(Equal(strings.HasPrefix(proxyResponse.Headers["Content-Type"], "text/plain")))
74119
Expect(proxyResponse.IsBase64Encoded).To(BeFalse())
75120
})
76121

core/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77
"github.com/aws/aws-lambda-go/events"
88
)
99

10-
// Returns a dafault Gateway Timeout (504) response
10+
// GatewayTimeout returns a dafault Gateway Timeout (504) response
1111
func GatewayTimeout() events.APIGatewayProxyResponse {
1212
return events.APIGatewayProxyResponse{StatusCode: http.StatusGatewayTimeout}
1313
}
1414

15+
// New Logged Error
1516
func NewLoggedError(format string, a ...interface{}) error {
1617
err := fmt.Errorf(format, a...)
1718
fmt.Println(err.Error())

0 commit comments

Comments
 (0)