Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading