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

Upgrader.Upgrade: use http.ResposnseController #871

Merged
merged 2 commits into from
Jan 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
7 changes: 1 addition & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,7 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
}
}

h, ok := w.(http.Hijacker)
if !ok {
return u.returnError(w, r, http.StatusInternalServerError, "websocket: response does not implement http.Hijacker")
}
var brw *bufio.ReadWriter
netConn, brw, err := h.Hijack()
netConn, brw, err := http.NewResponseController(w).Hijack()
if err != nil {
return u.returnError(w, r, http.StatusInternalServerError, err.Error())
}
Expand Down
22 changes: 22 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import (
"bufio"
"bytes"
"errors"
"net"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -41,9 +43,9 @@
ok bool
h http.Header
}{
{false, http.Header{"Upgrade": {"websocket"}}},

Check failure on line 46 in server_test.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

missing type in composite literal (typecheck)
{false, http.Header{"Connection": {"upgrade"}}},

Check failure on line 47 in server_test.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

missing type in composite literal (typecheck)
{true, http.Header{"Connection": {"upgRade"}, "Upgrade": {"WebSocket"}}},

Check failure on line 48 in server_test.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

missing type in composite literal (typecheck)
}

func TestIsWebSocketUpgrade(t *testing.T) {
Expand Down Expand Up @@ -152,3 +154,23 @@
}
}
}

func TestHijack_NotSupported(t *testing.T) {
t.Parallel()
AlexVulaj marked this conversation as resolved.
Show resolved Hide resolved

req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
req.Header.Set("Upgrade", "websocket")
req.Header.Set("Connection", "upgrade")
req.Header.Set("Sec-Websocket-Key", "dGhlIHNhbXBsZSBub25jZQ==")
req.Header.Set("Sec-Websocket-Version", "13")

recorder := httptest.NewRecorder()

upgrader := Upgrader{}
_, err := upgrader.Upgrade(recorder, req, nil)

if want := (HandshakeError{}); !errors.As(err, &want) || recorder.Code != http.StatusInternalServerError {
t.Errorf("want %T and status_code=%d", want, http.StatusInternalServerError)
t.Fatalf("got err=%T and status_code=%d", err, recorder.Code)
}
}
Loading