Skip to content

Commit

Permalink
net/ghttp: fix Content-Type for jsonp response from `application/js…
Browse files Browse the repository at this point in the history
…on` to `application/javascript` (#3651)
  • Loading branch information
swift-fs authored Jun 24, 2024
1 parent 7d464d4 commit dba6c08
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions net/ghttp/ghttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ const (
contentTypeXml = "text/xml"
contentTypeHtml = "text/html"
contentTypeJson = "application/json"
contentTypeJavascript = "application/javascript"
swaggerUIPackedPath = "/goframe/swaggerui"
responseHeaderTraceID = "Trace-ID"
responseHeaderContentLength = "Content-Length"
Expand Down
2 changes: 1 addition & 1 deletion net/ghttp/ghttp_response_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (r *Response) WriteJsonExit(content interface{}) {
//
// Note that there should be a "callback" parameter in the request for JSONP format.
func (r *Response) WriteJsonP(content interface{}) {
r.Header().Set("Content-Type", contentTypeJson)
r.Header().Set("Content-Type", contentTypeJavascript)
// If given string/[]byte, response it directly to client.
switch content.(type) {
case string, []byte:
Expand Down
2 changes: 2 additions & 0 deletions net/ghttp/ghttp_z_unit_feature_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ func Test_Response_Write(t *testing.T) {
t.Assert(client.GetContent(ctx, "/Writefln", "name=john"), "john\n")
t.Assert(client.GetContent(ctx, "/WriteJson"), "{\"name\":\"john\"}")
t.Assert(client.GetContent(ctx, "/WriteJsonP"), "{\"name\":\"john\"}")
resp, _ := client.DoRequest(ctx, http.MethodGet, "/WriteJsonP", "{\"name\":\"john\"}", nil)
t.Assert(resp.Header.Get("Content-Type"), "application/javascript")
t.Assert(client.GetContent(ctx, "/WriteJsonPWithStruct"), "{\"name\":\"john\"}")
t.Assert(client.GetContent(ctx, "/WriteJsonPWithStruct", "callback=callback"),
"callback({\"name\":\"john\"})")
Expand Down

0 comments on commit dba6c08

Please sign in to comment.