Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Sep 4, 2023
1 parent 31d1df4 commit 6ec85ff
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion server_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

func Test_ServerOption(t *testing.T) {
t.Run("2. Subprotocol", func(t *testing.T) {
t.Run("2.1 Subprotocol", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := Upgrade(w, r, WithServerSubprotocols([]string{"crud", "im"}))
if err != nil {
Expand All @@ -45,4 +45,55 @@ func Test_ServerOption(t *testing.T) {
t.Error("header fail")
}
})

t.Run("2.2 Subprotocol", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := Upgrade(w, r, WithServerSubprotocols([]string{"crud", "im"}))
if err != nil {
t.Error(err)
}
}))

defer ts.Close()

url := strings.ReplaceAll(ts.URL, "http", "ws")
h := make(http.Header)
con, err := Dial(url, WithClientBindHTTPHeader(&h), WithClientHTTPHeader(http.Header{
"Sec-WebSocket-Protocol": []string{"crud"},
}))
if err != nil {
t.Error(err)
}
defer con.Close()

if h["Sec-Websocket-Protocol"][0] != "crud" {
t.Error("header fail")
}
})

t.Run("2.3 Subprotocol", func(t *testing.T) {
upgrade := NewUpgrade(WithServerSubprotocols([]string{"crud", "im"}))
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := upgrade.Upgrade(w, r)
if err != nil {
t.Error(err)
}
}))

defer ts.Close()

url := strings.ReplaceAll(ts.URL, "http", "ws")
h := make(http.Header)
con, err := Dial(url, WithClientBindHTTPHeader(&h), WithClientHTTPHeader(http.Header{
"Sec-WebSocket-Protocol": []string{"crud"},
}))
if err != nil {
t.Error(err)
}
defer con.Close()

if h["Sec-Websocket-Protocol"][0] != "crud" {
t.Error("header fail")
}
})
}

0 comments on commit 6ec85ff

Please sign in to comment.