2014-04-19 01:25:11 +04:00
|
|
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
2013-10-26 17:52:30 +04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2018-08-18 05:50:34 +03:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2020-06-30 07:06:04 +03:00
|
|
|
"errors"
|
2018-08-18 05:50:34 +03:00
|
|
|
"net"
|
2013-10-26 17:52:30 +04:00
|
|
|
"net/http"
|
2020-06-30 07:06:04 +03:00
|
|
|
"net/http/httptest"
|
2013-10-26 17:52:30 +04:00
|
|
|
"reflect"
|
2018-08-18 05:50:34 +03:00
|
|
|
"strings"
|
2013-10-26 17:52:30 +04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var subprotocolTests = []struct {
|
|
|
|
h string
|
|
|
|
protocols []string
|
|
|
|
}{
|
|
|
|
{"", nil},
|
|
|
|
{"foo", []string{"foo"}},
|
|
|
|
{"foo,bar", []string{"foo", "bar"}},
|
|
|
|
{"foo, bar", []string{"foo", "bar"}},
|
|
|
|
{" foo, bar", []string{"foo", "bar"}},
|
|
|
|
{" foo, bar ", []string{"foo", "bar"}},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubprotocols(t *testing.T) {
|
|
|
|
for _, st := range subprotocolTests {
|
|
|
|
r := http.Request{Header: http.Header{"Sec-Websocket-Protocol": {st.h}}}
|
|
|
|
protocols := Subprotocols(&r)
|
|
|
|
if !reflect.DeepEqual(st.protocols, protocols) {
|
|
|
|
t.Errorf("SubProtocols(%q) returned %#v, want %#v", st.h, protocols, st.protocols)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-09 21:36:44 +03:00
|
|
|
|
|
|
|
var isWebSocketUpgradeTests = []struct {
|
|
|
|
ok bool
|
|
|
|
h http.Header
|
|
|
|
}{
|
|
|
|
{false, http.Header{"Upgrade": {"websocket"}}},
|
|
|
|
{false, http.Header{"Connection": {"upgrade"}}},
|
|
|
|
{true, http.Header{"Connection": {"upgRade"}, "Upgrade": {"WebSocket"}}},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsWebSocketUpgrade(t *testing.T) {
|
|
|
|
for _, tt := range isWebSocketUpgradeTests {
|
|
|
|
ok := IsWebSocketUpgrade(&http.Request{Header: tt.h})
|
|
|
|
if tt.ok != ok {
|
|
|
|
t.Errorf("IsWebSocketUpgrade(%v) returned %v, want %v", tt.h, ok, tt.ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-28 03:10:45 +03:00
|
|
|
|
|
|
|
var checkSameOriginTests = []struct {
|
|
|
|
ok bool
|
|
|
|
r *http.Request
|
|
|
|
}{
|
2018-08-25 00:03:26 +03:00
|
|
|
{false, &http.Request{Host: "example.org", Header: map[string][]string{"Origin": {"https://other.org"}}}},
|
|
|
|
{true, &http.Request{Host: "example.org", Header: map[string][]string{"Origin": {"https://example.org"}}}},
|
|
|
|
{true, &http.Request{Host: "Example.org", Header: map[string][]string{"Origin": {"https://example.org"}}}},
|
2017-11-28 03:10:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckSameOrigin(t *testing.T) {
|
|
|
|
for _, tt := range checkSameOriginTests {
|
|
|
|
ok := checkSameOrigin(tt.r)
|
|
|
|
if tt.ok != ok {
|
|
|
|
t.Errorf("checkSameOrigin(%+v) returned %v, want %v", tt.r, ok, tt.ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-18 05:50:34 +03:00
|
|
|
|
|
|
|
type reuseTestResponseWriter struct {
|
|
|
|
brw *bufio.ReadWriter
|
|
|
|
http.ResponseWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (resp *reuseTestResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
return fakeNetConn{strings.NewReader(""), &bytes.Buffer{}}, resp.brw, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var bufioReuseTests = []struct {
|
|
|
|
n int
|
|
|
|
reuse bool
|
|
|
|
}{
|
|
|
|
{4096, true},
|
|
|
|
{128, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBufioReuse(t *testing.T) {
|
|
|
|
for i, tt := range bufioReuseTests {
|
|
|
|
br := bufio.NewReaderSize(strings.NewReader(""), tt.n)
|
|
|
|
bw := bufio.NewWriterSize(&bytes.Buffer{}, tt.n)
|
|
|
|
resp := &reuseTestResponseWriter{
|
|
|
|
brw: bufio.NewReadWriter(br, bw),
|
|
|
|
}
|
|
|
|
upgrader := Upgrader{}
|
|
|
|
c, err := upgrader.Upgrade(resp, &http.Request{
|
|
|
|
Method: "GET",
|
|
|
|
Header: http.Header{
|
|
|
|
"Upgrade": []string{"websocket"},
|
|
|
|
"Connection": []string{"upgrade"},
|
|
|
|
"Sec-Websocket-Key": []string{"dGhlIHNhbXBsZSBub25jZQ=="},
|
|
|
|
"Sec-Websocket-Version": []string{"13"},
|
|
|
|
}}, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if reuse := c.br == br; reuse != tt.reuse {
|
|
|
|
t.Errorf("%d: buffered reader reuse=%v, want %v", i, reuse, tt.reuse)
|
|
|
|
}
|
|
|
|
writeBuf := bufioWriterBuffer(c.UnderlyingConn(), bw)
|
|
|
|
if reuse := &c.writeBuf[0] == &writeBuf[0]; reuse != tt.reuse {
|
|
|
|
t.Errorf("%d: write buffer reuse=%v, want %v", i, reuse, tt.reuse)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 07:06:04 +03:00
|
|
|
|
|
|
|
var negotiateSubprotocolTests = []struct {
|
|
|
|
*Upgrader
|
|
|
|
match bool
|
|
|
|
shouldErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&Upgrader{
|
|
|
|
NegotiateSubprotocol: func(r *http.Request) (s string, err error) { return "json", nil },
|
|
|
|
}, true, false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Upgrader{
|
|
|
|
Subprotocols: []string{"json"},
|
|
|
|
}, true, false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Upgrader{
|
|
|
|
Subprotocols: []string{"not-match"},
|
|
|
|
}, false, false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Upgrader{
|
|
|
|
NegotiateSubprotocol: func(r *http.Request) (s string, err error) { return "", errors.New("not-match") },
|
|
|
|
}, false, true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNegotiateSubprotocol(t *testing.T) {
|
|
|
|
for i := range negotiateSubprotocolTests {
|
|
|
|
upgrade := negotiateSubprotocolTests[i].Upgrader
|
|
|
|
|
|
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
upgrade.Upgrade(w, r, nil)
|
|
|
|
}))
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", s.URL, strings.NewReader(""))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("NewRequest retuened error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Connection", "upgrade")
|
|
|
|
req.Header.Set("Upgrade", "websocket")
|
|
|
|
req.Header.Set("Sec-Websocket-Version", "13")
|
|
|
|
req.Header.Set("Sec-Websocket-Protocol", "json")
|
|
|
|
req.Header.Set("Sec-Websocket-key", "dGhlIHNhbXBsZSBub25jZQ==")
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Do returned error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if negotiateSubprotocolTests[i].shouldErr && resp.StatusCode != http.StatusBadRequest {
|
|
|
|
t.Errorf("The expecred status code is %d,actual status code is %d", http.StatusBadRequest, resp.StatusCode)
|
|
|
|
} else {
|
|
|
|
if negotiateSubprotocolTests[i].match {
|
|
|
|
protocol := resp.Header.Get("Sec-Websocket-Protocol")
|
|
|
|
if protocol != "json" {
|
|
|
|
t.Errorf("Negotiation protocol failed,request protocol is json,reponese protocol is %s", protocol)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, ok := resp.Header["Sec-Websocket-Protocol"]; ok {
|
|
|
|
t.Errorf("Negotiation protocol failed,Sec-Websocket-Protocol field should be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Close()
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|