Skip to content

Commit

Permalink
return error if the conn is not a h2 conn
Browse files Browse the repository at this point in the history
  • Loading branch information
wzekin committed Apr 28, 2023
1 parent dcd6829 commit 0c85780
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 3 additions & 3 deletions response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,11 @@ func (w *extWriter) Finalize() error {
return nil
}

func NewResponseWriter(conn network.Conn) network.ExtWriter {
func NewResponseWriter(conn network.Conn) (network.ExtWriter, error) {
c, ok := conn.(*h2ServerConn)
if !ok {
panic("the conn is not the H2 Conn!")
return nil, errors.New("http2: the conn is not the H2 Conn!")
}

return &extWriter{c.rw}
return &extWriter{c.rw}, nil
}
6 changes: 4 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3680,7 +3680,8 @@ func TestProtocolErrorAfterGoAway(t *testing.T) {

func TestServer_HijackWriter(t *testing.T) {
testServerResponse(t, func(c context.Context, ctx *app.RequestContext) error {
ctx.Response.HijackWriter(NewResponseWriter(ctx.GetConn()))
writer, _ := NewResponseWriter(ctx.GetConn())
ctx.Response.HijackWriter(writer)
ctx.Write([]byte("Hello"))
ctx.Flush()
ctx.Write([]byte("World"))
Expand Down Expand Up @@ -3721,7 +3722,8 @@ func TestServer_HijackWriter(t *testing.T) {
func TestServer_HijackWriter_Flush(t *testing.T) {
ch := make(chan struct{})
testServerResponse(t, func(c context.Context, ctx *app.RequestContext) error {
ctx.Response.HijackWriter(NewResponseWriter(ctx.GetConn()))
writer, _ := NewResponseWriter(ctx.GetConn())
ctx.Response.HijackWriter(writer)
ctx.Write([]byte("Hello"))
ctx.Flush()

Expand Down

0 comments on commit 0c85780

Please sign in to comment.