Skip to content

Commit

Permalink
feat(transport/http): add unwrap method for returning underlying resp…
Browse files Browse the repository at this point in the history
…onse writer. #3253 (#3265)
  • Loading branch information
kvii authored Mar 22, 2024
1 parent 1fdaabb commit 26d7d5f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions transport/http/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (w *responseWriter) Write(data []byte) (int, error) {
w.w.WriteHeader(w.code)
return w.w.Write(data)
}
func (w *responseWriter) Unwrap() http.ResponseWriter { return w.w }

type wrapper struct {
router *Router
Expand Down
28 changes: 28 additions & 0 deletions transport/http/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
w := wrapper{
router: testRouter,
Expand Down

0 comments on commit 26d7d5f

Please sign in to comment.