transport/http: fix content type (#1070)

* fix content type
pull/1072/head
Tony Chen 3 years ago committed by GitHub
parent db02034dd1
commit 16b1da04e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      transport/http/context.go
  2. 9
      transport/http/route_test.go

@ -43,10 +43,27 @@ type Context interface {
Reset(http.ResponseWriter, *http.Request) Reset(http.ResponseWriter, *http.Request)
} }
type responseWriter struct {
code int
w http.ResponseWriter
}
func (w *responseWriter) rest(res http.ResponseWriter) {
w.w = res
w.code = http.StatusOK
}
func (w *responseWriter) Header() http.Header { return w.w.Header() }
func (w *responseWriter) WriteHeader(statusCode int) { w.code = statusCode }
func (w *responseWriter) Write(data []byte) (int, error) {
w.w.WriteHeader(w.code)
return w.w.Write(data)
}
type wrapper struct { type wrapper struct {
route *Route route *Route
req *http.Request req *http.Request
res http.ResponseWriter res http.ResponseWriter
w responseWriter
} }
func (c *wrapper) Header() http.Header { func (c *wrapper) Header() http.Header {
@ -83,14 +100,14 @@ func (c *wrapper) Returns(v interface{}, err error) error {
if err != nil { if err != nil {
return err return err
} }
if err := c.route.srv.enc(c.res, c.req, v); err != nil { if err := c.route.srv.enc(&c.w, c.req, v); err != nil {
return err return err
} }
return nil return nil
} }
func (c *wrapper) Result(code int, v interface{}) error { func (c *wrapper) Result(code int, v interface{}) error {
c.res.WriteHeader(code) c.w.WriteHeader(code)
if err := c.route.srv.enc(c.res, c.req, v); err != nil { if err := c.route.srv.enc(&c.w, c.req, v); err != nil {
return err return err
} }
return nil return nil
@ -124,6 +141,7 @@ func (c *wrapper) Stream(code int, contentType string, rd io.Reader) error {
return err return err
} }
func (c *wrapper) Reset(res http.ResponseWriter, req *http.Request) { func (c *wrapper) Reset(res http.ResponseWriter, req *http.Request) {
c.w.rest(res)
c.res = res c.res = res
c.req = req c.req = req
} }

@ -91,6 +91,9 @@ func testRoute(t *testing.T, srv *Server) {
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
t.Fatalf("code: %d", resp.StatusCode) t.Fatalf("code: %d", resp.StatusCode)
} }
if v := resp.Header.Get("Content-Type"); v != "application/json" {
t.Fatalf("contentType: %s", v)
}
u := new(User) u := new(User)
if err := json.NewDecoder(resp.Body).Decode(u); err != nil { if err := json.NewDecoder(resp.Body).Decode(u); err != nil {
t.Fatal(err) t.Fatal(err)
@ -107,6 +110,9 @@ func testRoute(t *testing.T, srv *Server) {
if resp.StatusCode != 201 { if resp.StatusCode != 201 {
t.Fatalf("code: %d", resp.StatusCode) t.Fatalf("code: %d", resp.StatusCode)
} }
if v := resp.Header.Get("Content-Type"); v != "application/json" {
t.Fatalf("contentType: %s", v)
}
u = new(User) u = new(User)
if err = json.NewDecoder(resp.Body).Decode(u); err != nil { if err = json.NewDecoder(resp.Body).Decode(u); err != nil {
t.Fatal(err) t.Fatal(err)
@ -125,6 +131,9 @@ func testRoute(t *testing.T, srv *Server) {
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
t.Fatalf("code: %d", resp.StatusCode) t.Fatalf("code: %d", resp.StatusCode)
} }
if v := resp.Header.Get("Content-Type"); v != "application/json" {
t.Fatalf("contentType: %s", v)
}
u = new(User) u = new(User)
if err = json.NewDecoder(resp.Body).Decode(u); err != nil { if err = json.NewDecoder(resp.Body).Decode(u); err != nil {
t.Fatal(err) t.Fatal(err)

Loading…
Cancel
Save