Skip to content

Commit e3a1eed

Browse files
authored
Merge pull request #3 from moleculer-go/fix/sendResponse
fix (sendResponse) add content type json to header
2 parents 8cb4bab + 13f6dbd commit e3a1eed

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

actionHandler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ var resultParseErrorStatusCode = 500
6565
// sendReponse send the result payload back using the ResponseWriter
6666
func (handler *actionHandler) sendReponse(logger *log.Entry, result moleculer.Payload, response http.ResponseWriter) {
6767
var json []byte
68+
response.Header().Add("Content-Type", "application/json")
6869
if result.IsError() {
6970
response.WriteHeader(errorStatusCode)
7071
json = jsonSerializer.PayloadToBytes(payload.Empty().Add("error", result.Error().Error()))

gateway_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ var _ = Describe("API Gateway", func() {
124124
}
125125
actionHandlers = filterActions(ctx, settings, services)
126126
Expect(len(actionHandlers)).Should(Equal(2))
127-
Expect(actionHandlers[0].pattern()).Should(Equal("/user/list"))
128-
Expect(actionHandlers[1].pattern()).Should(Equal("/user/update"))
127+
patterns := actionHandlers[0].pattern() + " " + actionHandlers[1].pattern()
128+
Expect(patterns).Should(ContainSubstring("/user/list"))
129+
Expect(patterns).Should(ContainSubstring("/user/update"))
129130
})
130131

131132
It("should handle multiple routes", func() {
@@ -181,7 +182,7 @@ var _ = Describe("API Gateway", func() {
181182
"nilvalue": nil,
182183
}
183184

184-
response := &mockReponseWriter{}
185+
response := &mockReponseWriter{header: map[string][]string{}}
185186
ah := actionHandler{}
186187
ah.sendReponse(log.WithField("test", ""), payload.New(result), response)
187188
json := response.String()
@@ -190,15 +191,17 @@ var _ = Describe("API Gateway", func() {
190191
Expect(gjson.Get(json, "name").String()).Should(Equal("John"))
191192

192193
Expect(response.statusCode).Should(Equal(succesStatusCode))
194+
Expect(response.Header().Get("Content-Type")).Should(Equal("application/json"))
193195
})
194196

195197
It("should convert error result into JSON and send in the reponse with error status code", func() {
196-
response := &mockReponseWriter{}
198+
response := &mockReponseWriter{header: map[string][]string{}}
197199
ah := actionHandler{}
198200
ah.sendReponse(log.WithField("test", ""), payload.New(errors.New("Some error...")), response)
199201
json := response.String()
200202
Expect(gjson.Get(json, "error").String()).Should(Equal("Some error..."))
201203
Expect(response.statusCode).Should(Equal(errorStatusCode))
204+
Expect(response.Header().Get("Content-Type")).Should(Equal("application/json"))
202205
})
203206
})
204207

0 commit comments

Comments
 (0)