Skip to content

Commit

Permalink
feat: adapt response GetProtocol func (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzekin authored May 5, 2023
1 parent 24c7527 commit 6117264
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/cloudwego/hertz/pkg/network/dialer"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/protocol/client"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/hertz-contrib/http2/config"
"github.com/hertz-contrib/http2/hpack"
"github.com/hertz-contrib/http2/internal/bytesconv"
Expand Down Expand Up @@ -253,6 +254,7 @@ func (cc *clientConn) RoundTrip(ctx context.Context, req *protocol.Request, rsp

handleResponseHeaders := func() error {
res := cs.res
res.Header.SetProtocol(consts.HTTP20)
if res.StatusCode() > 299 {
// On error or status code 3xx, 4xx, 5xx, etc abort any
// ongoing write, assuming that the server doesn't care
Expand Down
8 changes: 8 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ func TestHostClientH2c(t *testing.T) {
t.Fatal(err)
}

if rsp.Header.GetProtocol() != consts.HTTP20 {
t.Fatalf("response protocol go %v, want %vj", rsp.Header.GetProtocol(), consts.HTTP20)
}

body, err := ioutil.ReadAll(rsp.BodyStream())
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -289,6 +293,10 @@ func TestHostClient(t *testing.T) {
t.Errorf("%d: StatusCode = %v; want %v", i, g, w)
}

if res.Header.GetProtocol() != consts.HTTP20 {
t.Fatalf("response protocol go %v, want %vj", res.Header.GetProtocol(), consts.HTTP20)
}

wantHeader := http.Header{
"Content-Length": []string{"3"},
"Content-Type": []string{"text/plain; charset=utf-8"},
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/hertz-contrib/http2
go 1.18

require (
github.com/cloudwego/hertz v0.6.2
github.com/cloudwego/hertz v0.6.3
golang.org/x/net v0.7.0
)

Expand All @@ -12,7 +12,7 @@ require (
github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7 // indirect
github.com/bytedance/sonic v1.8.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/cloudwego/netpoll v0.3.1 // indirect
github.com/cloudwego/netpoll v0.3.2 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/henrylee2cn/ameda v1.4.10 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ github.com/bytedance/sonic v1.8.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZX
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/cloudwego/hertz v0.6.2 h1:8NM0yHbyv8B4dNYgICirk733S7monTNB+uR9as1It1Y=
github.com/cloudwego/hertz v0.6.2/go.mod h1:2em2hGREvCBawsTQcQxyWBGVlCeo+N1pp2q0HkkbwR0=
github.com/cloudwego/netpoll v0.3.1 h1:xByoORmCLIyKZ8gS+da06WDo3j+jvmhaqS2KeKejtBk=
github.com/cloudwego/netpoll v0.3.1/go.mod h1:1T2WVuQ+MQw6h6DpE45MohSvDTKdy2DlzCx2KsnPI4E=
github.com/cloudwego/hertz v0.6.3 h1:zbus6GEYcTBH8UbgA+ay52u9oV9Kg6EvHp92GF9eTwo=
github.com/cloudwego/hertz v0.6.3/go.mod h1:KhztQcZtMQ46gOjZcmCy557AKD29cbumGEV0BzwevwA=
github.com/cloudwego/netpoll v0.3.2 h1:/998ICrNMVBo4mlul4j7qcIeY7QnEfuCCPPwck9S3X4=
github.com/cloudwego/netpoll v0.3.2/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down

0 comments on commit 6117264

Please sign in to comment.