From a0afe04dda10ea9f2b147a6955e2f920988456a7 Mon Sep 17 00:00:00 2001 From: guonaihong Date: Tue, 22 Aug 2023 13:11:13 +0800 Subject: [PATCH] =?UTF-8?q?+=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common_options_test.go | 1 - conn_test.go | 43 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/common_options_test.go b/common_options_test.go index 27b575c..37748eb 100644 --- a/common_options_test.go +++ b/common_options_test.go @@ -1550,7 +1550,6 @@ func Test_CommonOption(t *testing.T) { WithClientWindowsParseMode(), WithClientDelayWriteInitBufferSize(4096), WithClientOnMessageFunc(func(c *Conn, op Opcode, payload []byte) { - // c.WriteMessageDelay(op, []byte("hello")) })) if err != nil { t.Error(err) diff --git a/conn_test.go b/conn_test.go index faf6e50..6ea4d95 100644 --- a/conn_test.go +++ b/conn_test.go @@ -537,7 +537,7 @@ func Test_WriteControl(t *testing.T) { }) } -// 测试ping pong close信息 +// 测试ping pong close control信息 func TestPingPongClose(t *testing.T) { // 写一个超过maxControlFrameSize的消息 t.Run("1.>maxControlFrameSize.fail", func(t *testing.T) { @@ -783,4 +783,45 @@ func TestPingPongClose(t *testing.T) { } } }) + + t.Run("8.fail-control", func(t *testing.T) { + run := int32(0) + data := make(chan string, 1) + upgrade := NewUpgrade(WithServerOnCloseFunc(func(c *Conn, err error) { + atomic.AddInt32(&run, 1) + data <- err.Error() + })) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + c, err := upgrade.Upgrade(w, r) + if err != nil { + t.Error(err) + } + c.StartReadLoop() + })) + + defer ts.Close() + + url := strings.ReplaceAll(ts.URL, "http", "ws") + con, err := Dial(url, WithClientDisableBufioClearHack(), + WithClientEnableUTF8Check(), WithClientOnCloseFunc(func(c *Conn, err error) { + })) + if err != nil { + t.Error(err) + } + defer con.Close() + // 这里必须要报错 + err = con.WriteMessage(4 /*这是rfc保留的frame*/, []byte("hello")) + if err != nil { + t.Error("not error") + } + con.StartReadLoop() + select { + case _ = <-data: + case <-time.After(500 * time.Millisecond): + } + + if atomic.LoadInt32(&run) != 1 { + t.Error("not run server:method fail") + } + }) }