diff --git a/server_option_test.go b/server_option_test.go index 4e70944..e72bbbd 100644 --- a/server_option_test.go +++ b/server_option_test.go @@ -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 { @@ -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") + } + }) }