Repository: go-kratos/kratos
Pull request #1071 (2021-06-17 02:23:53)

add comment


Patch:
diff --git a/README_zh.md b/README_zh.md
index 53a16d62..5f1851f0 100644
--- a/README_zh.md
+++ b/README_zh.md
@@ -102,8 +102,8 @@ app.Run()
 ## Community
 * [Wechat Group](https://github.com/go-kratos/kratos/issues/682)
 * [Discord Group](https://discord.gg/BWzJsUJ)
+* Website:  [go-kratos.dev](https://go-kratos.dev)
 * QQ Group: 716486124
-
 ## Sponsors and Backers
 
 ![kratos](docs/images/alipay.png)
diff --git a/transport/http/context.go b/transport/http/context.go
index b70e3903..4225483f 100644
--- a/transport/http/context.go
+++ b/transport/http/context.go
@@ -43,10 +43,27 @@ type Context interface {
 	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 {
 	route *Route
 	req   *http.Request
 	res   http.ResponseWriter
+	w     responseWriter
 }
 
 func (c *wrapper) Header() http.Header {
@@ -83,14 +100,14 @@ func (c *wrapper) Returns(v interface{}, err error) error {
 	if err != nil {
 		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 nil
 }
 func (c *wrapper) Result(code int, v interface{}) error {
-	c.res.WriteHeader(code)
-	if err := c.route.srv.enc(c.res, c.req, v); err != nil {
+	c.w.WriteHeader(code)
+	if err := c.route.srv.enc(&c.w, c.req, v); err != nil {
 		return err
 	}
 	return nil
@@ -124,6 +141,7 @@ func (c *wrapper) Stream(code int, contentType string, rd io.Reader) error {
 	return err
 }
 func (c *wrapper) Reset(res http.ResponseWriter, req *http.Request) {
+	c.w.rest(res)
 	c.res = res
 	c.req = req
 }
diff --git a/transport/transport.go b/transport/transport.go
index 7d378311..833523a7 100644
--- a/transport/transport.go
+++ b/transport/transport.go
@@ -31,9 +31,17 @@ type Header interface {
 
 // Transporter is transport context value interface.
 type Transporter interface {
+	// grpc
+	// http
 	Kind() Kind
+	// Server Transport: grpc://127.0.0.1:9000
+	// Client Transport: discovery:///provider-demo
 	Endpoint() string
+	// Service full method selector generated by protobuf
+	// example: /helloworld.Greeter/SayHello
 	Operation() string
+	// http: http.Header
+	// grpc: metadata.MD
 	Header() Header
 }
 


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