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: http transport support get http.ResponseWriter #3555

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions transport/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ func RequestFromServerContext(ctx context.Context) (*http.Request, bool) {
return nil, false
}

// ResponseFromServerContext returns response from context.
func ResponseFromServerContext(ctx context.Context) (http.ResponseWriter, bool) {
if tr, ok := transport.FromServerContext(ctx); ok {
if tr, ok := tr.(*Transport); ok {
return tr.response, true
}
}
return nil, false
}

type headerCarrier http.Header

// Get returns the value associated with the passed key.
Expand Down
19 changes: 19 additions & 0 deletions transport/http/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,22 @@ func TestSetOperation(t *testing.T) {
t.Errorf("expect %v, got %v", "kratos", tr.operation)
}
}

func TestResponseFromServerContext(t *testing.T) {
tr := &Transport{}
ctx := transport.NewServerContext(context.Background(), tr)
_, ok := ResponseFromServerContext(ctx)
if ok {
t.Errorf("expect %v, got %v", false, ok)
}
res, ok := ResponseFromServerContext(ctx)
if !ok {
t.Errorf("expect %v, got %v", true, ok)
}
if res == nil {
t.Errorf("expect %v, got %v", "*http.ResponseWriter", res)
}
if !reflect.DeepEqual(res, tr.response) {
t.Errorf("expect %v, got %v", tr.response, res)
}
}