Skip to content

Commit

Permalink
新增测试覆盖度
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Jul 29, 2023
1 parent a45a987 commit 2602be3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
1 change: 0 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func (d *DialOption) handshake() (*http.Request, string, error) {
case d.u.Scheme == "ws":
d.u.Scheme = "http"
default:
// TODO 返回错误
return nil, "", fmt.Errorf("Unknown scheme, only supports ws:// or wss://: got %s", d.u.Scheme)
}

Expand Down
43 changes: 43 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,47 @@ func Test_Client_Dial(t *testing.T) {
fmt.Printf("err: %v\n", err)
}
})

t.Run("Dial: valid resp: Sec-WebSocket-Accept fail", func(t *testing.T) {
done := make(chan bool, 1)
run := int32(0)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&run, int32(1))
w.Header().Set("Upgrade", "websocket")
w.Header().Set("Connection", "Upgrade")
w.WriteHeader(101)
done <- true
}))

defer ts.Close()

rawURL := strings.ReplaceAll(ts.URL, "http", "ws")
_, err := Dial(rawURL)
if err == nil {
t.Fatal("should be error")
}
})

t.Run("DialConf: valid resp: Sec-WebSocket-Accept fail", func(t *testing.T) {
done := make(chan bool, 1)
run := int32(0)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&run, int32(1))
w.Header().Set("Upgrade", "websocket")
w.Header().Set("Connection", "Upgrade")
w.WriteHeader(101)
done <- true
}))

defer ts.Close()

cnf := ClientOptionToConf()
rawURL := strings.ReplaceAll(ts.URL, "http", "ws")
_, err := DialConf(rawURL, cnf)
if err == nil {
t.Fatal("should be error")
} else {
fmt.Printf("err: %v\n", err)
}
})
}

0 comments on commit 2602be3

Please sign in to comment.