diff --git a/client_server_test.go b/client_server_test.go index 7d72fd9..05a7888 100644 --- a/client_server_test.go +++ b/client_server_test.go @@ -56,11 +56,6 @@ func newTLSServer(t *testing.T) *cstServer { } func (t cstHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - t.Logf("method %s not allowed", r.Method) - http.Error(w, "method not allowed", 405) - return - } subprotos := Subprotocols(r) if !reflect.DeepEqual(subprotos, cstDialer.Subprotocols) { t.Logf("subprotols=%v, want %v", subprotos, cstDialer.Subprotocols) @@ -287,6 +282,26 @@ func TestDialBadHeader(t *testing.T) { } } +func TestBadMethod(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ws, err := cstUpgrader.Upgrade(w, r, nil) + if err == nil { + t.Errorf("handshake succeeded, expect fail") + ws.Close() + } + })) + defer s.Close() + + resp, err := http.PostForm(s.URL, url.Values{}) + if err != nil { + t.Fatalf("PostForm returned error %v", err) + } + resp.Body.Close() + if resp.StatusCode != http.StatusMethodNotAllowed { + t.Errorf("Status = %d, want %d", resp.StatusCode, http.StatusMethodNotAllowed) + } +} + func TestHandshake(t *testing.T) { s := newServer(t) defer s.Close() diff --git a/examples/chat/conn.go b/examples/chat/conn.go index ae25b75..22816f0 100644 --- a/examples/chat/conn.go +++ b/examples/chat/conn.go @@ -90,10 +90,6 @@ func (c *connection) writePump() { // serveWs handles websocket requests from the peer. func serveWs(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - http.Error(w, "Method not allowed", 405) - return - } ws, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Println(err) diff --git a/examples/command/main.go b/examples/command/main.go index 73ac7e9..f3f022e 100644 --- a/examples/command/main.go +++ b/examples/command/main.go @@ -95,11 +95,6 @@ func internalError(ws *websocket.Conn, msg string, err error) { var upgrader = websocket.Upgrader{} func serveWs(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) - return - } - ws, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Println("upgrade:", err) diff --git a/examples/echo/server.go b/examples/echo/server.go index 8f18b3f..8923690 100644 --- a/examples/echo/server.go +++ b/examples/echo/server.go @@ -23,10 +23,6 @@ func echo(w http.ResponseWriter, r *http.Request) { http.Error(w, "Not found", 404) return } - if r.Method != "GET" { - http.Error(w, "Method not allowed", 405) - return - } c, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Print("upgrade:", err) diff --git a/server.go b/server.go index e56a004..3a9805f 100644 --- a/server.go +++ b/server.go @@ -93,6 +93,9 @@ func (u *Upgrader) selectSubprotocol(r *http.Request, responseHeader http.Header // request. Use the responseHeader to specify cookies (Set-Cookie) and the // application negotiated subprotocol (Sec-Websocket-Protocol). func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*Conn, error) { + if r.Method != "GET" { + return u.returnError(w, r, http.StatusMethodNotAllowed, "websocket: method not GET") + } if values := r.Header["Sec-Websocket-Version"]; len(values) == 0 || values[0] != "13" { return u.returnError(w, r, http.StatusBadRequest, "websocket: version != 13") }