Merge pull request #98 from matt-royal/respond-with-json-sets-content-type
Set Content-Type to application/json in RespondWithJSON* methods
diff --git a/ghttp/handlers.go b/ghttp/handlers.go
index 143d2cb..c3b56a4 100644
--- a/ghttp/handlers.go
+++ b/ghttp/handlers.go
@@ -183,7 +183,17 @@
func RespondWithJSONEncoded(statusCode int, object interface{}, optionalHeader ...http.Header) http.HandlerFunc {
data, err := json.Marshal(object)
Ω(err).ShouldNot(HaveOccurred())
- return RespondWith(statusCode, string(data), optionalHeader...)
+
+ var headers http.Header
+ if len(optionalHeader) == 1 {
+ headers = optionalHeader[0]
+ } else {
+ headers = make(http.Header)
+ }
+ if _, found := headers["Content-Type"]; !found {
+ headers["Content-Type"] = []string{"application/json"}
+ }
+ return RespondWith(statusCode, string(data), headers)
}
/*
@@ -200,9 +210,16 @@
return func(w http.ResponseWriter, req *http.Request) {
data, err := json.Marshal(object)
Ω(err).ShouldNot(HaveOccurred())
+ var headers http.Header
if len(optionalHeader) == 1 {
- copyHeader(optionalHeader[0], w.Header())
+ headers = optionalHeader[0]
+ } else {
+ headers = make(http.Header)
}
+ if _, found := headers["Content-Type"]; !found {
+ headers["Content-Type"] = []string{"application/json"}
+ }
+ copyHeader(headers, w.Header())
w.WriteHeader(*statusCode)
w.Write(data)
}
diff --git a/ghttp/test_server_test.go b/ghttp/test_server_test.go
index 288ef55..82aa3aa 100644
--- a/ghttp/test_server_test.go
+++ b/ghttp/test_server_test.go
@@ -555,22 +555,72 @@
})
Describe("RespondWithJSON", func() {
- BeforeEach(func() {
- s.AppendHandlers(CombineHandlers(
- VerifyRequest("POST", "/foo"),
- RespondWithJSONEncoded(http.StatusCreated, []int{1, 2, 3}),
- ))
+ Context("when no optional headers are set", func() {
+ BeforeEach(func() {
+ s.AppendHandlers(CombineHandlers(
+ VerifyRequest("POST", "/foo"),
+ RespondWithJSONEncoded(http.StatusCreated, []int{1, 2, 3}),
+ ))
+ })
+
+ It("should return the response", func() {
+ resp, err = http.Post(s.URL()+"/foo", "application/json", nil)
+ Ω(err).ShouldNot(HaveOccurred())
+
+ Ω(resp.StatusCode).Should(Equal(http.StatusCreated))
+
+ body, err := ioutil.ReadAll(resp.Body)
+ Ω(err).ShouldNot(HaveOccurred())
+ Ω(body).Should(MatchJSON("[1,2,3]"))
+ })
+
+ It("should set the Content-Type header to application/json", func() {
+ resp, err = http.Post(s.URL()+"/foo", "application/json", nil)
+ Ω(err).ShouldNot(HaveOccurred())
+
+ Ω(resp.Header["Content-Type"]).Should(Equal([]string{"application/json"}))
+ })
})
- It("should return the response", func() {
- resp, err = http.Post(s.URL()+"/foo", "application/json", nil)
- Ω(err).ShouldNot(HaveOccurred())
+ Context("when optional headers are set", func() {
+ var headers http.Header
+ BeforeEach(func() {
+ headers = http.Header{"Stuff": []string{"things"}}
+ })
- Ω(resp.StatusCode).Should(Equal(http.StatusCreated))
+ JustBeforeEach(func() {
+ s.AppendHandlers(CombineHandlers(
+ VerifyRequest("POST", "/foo"),
+ RespondWithJSONEncoded(http.StatusCreated, []int{1, 2, 3}, headers),
+ ))
+ })
- body, err := ioutil.ReadAll(resp.Body)
- Ω(err).ShouldNot(HaveOccurred())
- Ω(body).Should(MatchJSON("[1,2,3]"))
+ It("should preserve those headers", func() {
+ resp, err = http.Post(s.URL()+"/foo", "application/json", nil)
+ Ω(err).ShouldNot(HaveOccurred())
+
+ Ω(resp.Header["Stuff"]).Should(Equal([]string{"things"}))
+ })
+
+ It("should set the Content-Type header to application/json", func() {
+ resp, err = http.Post(s.URL()+"/foo", "application/json", nil)
+ Ω(err).ShouldNot(HaveOccurred())
+
+ Ω(resp.Header["Content-Type"]).Should(Equal([]string{"application/json"}))
+ })
+
+ Context("when setting the Content-Type explicitly", func() {
+ BeforeEach(func() {
+ headers["Content-Type"] = []string{"not-json"}
+ })
+
+ It("should use the Content-Type header that was explicitly set", func() {
+ resp, err = http.Post(s.URL()+"/foo", "application/json", nil)
+ Ω(err).ShouldNot(HaveOccurred())
+
+ Ω(resp.Header["Content-Type"]).Should(Equal([]string{"not-json"}))
+ })
+ })
})
})
@@ -582,29 +632,82 @@
var code int
var object testObject
- BeforeEach(func() {
- code = http.StatusOK
- object = testObject{}
- s.AppendHandlers(CombineHandlers(
- VerifyRequest("POST", "/foo"),
- RespondWithJSONEncodedPtr(&code, &object),
- ))
+
+ Context("when no optional headers are set", func() {
+ BeforeEach(func() {
+ code = http.StatusOK
+ object = testObject{}
+ s.AppendHandlers(CombineHandlers(
+ VerifyRequest("POST", "/foo"),
+ RespondWithJSONEncodedPtr(&code, &object),
+ ))
+ })
+
+ It("should return the response", func() {
+ code = http.StatusCreated
+ object = testObject{
+ Key: "Jim",
+ Value: "Codes",
+ }
+ resp, err = http.Post(s.URL()+"/foo", "application/json", nil)
+ Ω(err).ShouldNot(HaveOccurred())
+
+ Ω(resp.StatusCode).Should(Equal(http.StatusCreated))
+
+ body, err := ioutil.ReadAll(resp.Body)
+ Ω(err).ShouldNot(HaveOccurred())
+ Ω(body).Should(MatchJSON(`{"Key": "Jim", "Value": "Codes"}`))
+ })
+
+ It("should set the Content-Type header to application/json", func() {
+ resp, err = http.Post(s.URL()+"/foo", "application/json", nil)
+ Ω(err).ShouldNot(HaveOccurred())
+
+ Ω(resp.Header["Content-Type"]).Should(Equal([]string{"application/json"}))
+ })
})
- It("should return the response", func() {
- code = http.StatusCreated
- object = testObject{
- Key: "Jim",
- Value: "Codes",
- }
- resp, err = http.Post(s.URL()+"/foo", "application/json", nil)
- Ω(err).ShouldNot(HaveOccurred())
+ Context("when optional headers are set", func() {
+ var headers http.Header
+ BeforeEach(func() {
+ headers = http.Header{"Stuff": []string{"things"}}
+ })
- Ω(resp.StatusCode).Should(Equal(http.StatusCreated))
+ JustBeforeEach(func() {
+ code = http.StatusOK
+ object = testObject{}
+ s.AppendHandlers(CombineHandlers(
+ VerifyRequest("POST", "/foo"),
+ RespondWithJSONEncodedPtr(&code, &object, headers),
+ ))
+ })
- body, err := ioutil.ReadAll(resp.Body)
- Ω(err).ShouldNot(HaveOccurred())
- Ω(body).Should(MatchJSON(`{"Key": "Jim", "Value": "Codes"}`))
+ It("should preserve those headers", func() {
+ resp, err = http.Post(s.URL()+"/foo", "application/json", nil)
+ Ω(err).ShouldNot(HaveOccurred())
+
+ Ω(resp.Header["Stuff"]).Should(Equal([]string{"things"}))
+ })
+
+ It("should set the Content-Type header to application/json", func() {
+ resp, err = http.Post(s.URL()+"/foo", "application/json", nil)
+ Ω(err).ShouldNot(HaveOccurred())
+
+ Ω(resp.Header["Content-Type"]).Should(Equal([]string{"application/json"}))
+ })
+
+ Context("when setting the Content-Type explicitly", func() {
+ BeforeEach(func() {
+ headers["Content-Type"] = []string{"not-json"}
+ })
+
+ It("should use the Content-Type header that was explicitly set", func() {
+ resp, err = http.Post(s.URL()+"/foo", "application/json", nil)
+ Ω(err).ShouldNot(HaveOccurred())
+
+ Ω(resp.Header["Content-Type"]).Should(Equal([]string{"not-json"}))
+ })
+ })
})
})
})