1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-03-19 21:18:07 +02:00

feat(transport/http): add unwrap method for returning underlying response writer. #3253 (#3265)

This commit is contained in:
kvii 2024-03-22 23:37:51 +08:00 committed by GitHub
parent 1fdaabbd48
commit 26d7d5fa00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 0 deletions

View File

@ -57,6 +57,7 @@ func (w *responseWriter) Write(data []byte) (int, error) {
w.w.WriteHeader(w.code) w.w.WriteHeader(w.code)
return w.w.Write(data) return w.w.Write(data)
} }
func (w *responseWriter) Unwrap() http.ResponseWriter { return w.w }
type wrapper struct { type wrapper struct {
router *Router router *Router

View File

@ -100,6 +100,34 @@ func TestContextResponse(t *testing.T) {
} }
} }
func TestResponseUnwrap(t *testing.T) {
res := httptest.NewRecorder()
f := func(rw http.ResponseWriter, r *http.Request, a interface{}) error {
u, ok := rw.(interface {
Unwrap() http.ResponseWriter
})
if !ok {
return errors.New("can not unwrap")
}
w := u.Unwrap()
if !reflect.DeepEqual(w, res) {
return errors.New("underlying response writer not equal")
}
return nil
}
w := wrapper{
router: &Router{srv: &Server{enc: f}},
req: nil,
res: res,
w: responseWriter{200, res},
}
err := w.Result(200, "ok")
if err != nil {
t.Errorf("expected %v, got %v", nil, err)
}
}
func TestContextBindQuery(t *testing.T) { func TestContextBindQuery(t *testing.T) {
w := wrapper{ w := wrapper{
router: testRouter, router: testRouter,