Skip to content

Commit

Permalink
refine: return error instead of panic when response writer doesn't im…
Browse files Browse the repository at this point in the history
…plement hijacker
  • Loading branch information
xieyuschen committed Aug 7, 2024
1 parent cc4e114 commit 44b40e4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 6 additions & 1 deletion response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package gin

import (
"bufio"
"errors"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -109,7 +110,11 @@ func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if w.size < 0 {
w.size = 0
}
return w.ResponseWriter.(http.Hijacker).Hijack()
hijacker, ok := w.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, errors.New("response writer does not support Hijack")
}
return hijacker.Hijack()
}

// CloseNotify implements the http.CloseNotifier interface.
Expand Down
6 changes: 2 additions & 4 deletions response_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ func TestResponseWriterHijack(t *testing.T) {
writer.reset(testWriter)
w := ResponseWriter(writer)

assert.Panics(t, func() {
_, _, err := w.Hijack()
require.NoError(t, err)
})
_, _, err := w.Hijack()
assert.Equal(t, "response writer does not support Hijack", err.Error())
assert.True(t, w.Written())

assert.Panics(t, func() {
Expand Down

0 comments on commit 44b40e4

Please sign in to comment.