fix: websocket test failed (#11)
This commit is contained in:
parent
cb568a3e5c
commit
2b7082d296
|
@ -581,7 +581,7 @@ var marshalErrorItems = []testItemType{
|
|||
{&structWithDupKeys{},
|
||||
"Duplicated key 'name' in struct bson_test.structWithDupKeys"},
|
||||
{bson.Raw{0x0A, []byte{}},
|
||||
"Attempted to unmarshal Raw kind 10 as a document"},
|
||||
"Attempted to marshal Raw kind 10 as a document"},
|
||||
{&inlineCantPtr{&struct{ A, B int }{1, 2}},
|
||||
"Option ,inline needs a struct value or map field"},
|
||||
{&inlineDupName{1, struct{ A, B int }{2, 3}},
|
||||
|
@ -1429,9 +1429,9 @@ func (s *S) TestObjectIdJSONUnmarshaling(c *C) {
|
|||
func (s *S) TestObjectIdJSONUnmarshalingError(c *C) {
|
||||
v := jsonType{}
|
||||
err := json.Unmarshal([]byte(`{"Id":"4d88e15b60f486e428412dc9A"}`), &v)
|
||||
c.Assert(err, ErrorMatches, `Invalid ObjectId in JSON: "4d88e15b60f486e428412dc9A"`)
|
||||
c.Assert(err, ErrorMatches, `invalid ObjectId in JSON: "4d88e15b60f486e428412dc9A"`)
|
||||
err = json.Unmarshal([]byte(`{"Id":"4d88e15b60f486e428412dcZ"}`), &v)
|
||||
c.Assert(err, ErrorMatches, `Invalid ObjectId in JSON: "4d88e15b60f486e428412dcZ" .*`)
|
||||
c.Assert(err, ErrorMatches, `invalid ObjectId in JSON: "4d88e15b60f486e428412dcZ" .*`)
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -11,45 +12,48 @@ import (
|
|||
)
|
||||
|
||||
func TestWSClient(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
|
||||
http.HandleFunc("/test/client", func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
t.Log("server: wg.Done")
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
conn, err := websocket.Upgrade(w, r, nil, 1024, 1024)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
t.Log("websocket.Upgrade")
|
||||
|
||||
conn.SetPingHandler(func(d string) error {
|
||||
t.Log("receive from client: ", d)
|
||||
conn.WriteMessage(websocket.PongMessage, []byte("server.Pong"))
|
||||
return nil
|
||||
})
|
||||
|
||||
msgType, msg, err := conn.ReadMessage()
|
||||
conn.WriteMessage(websocket.TextMessage, msg)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
if msgType != websocket.TextMessage {
|
||||
t.Fatal("invalid msg type", msgType)
|
||||
}
|
||||
|
||||
msgType, msg, err = conn.ReadMessage()
|
||||
err = conn.WriteMessage(websocket.TextMessage, msg)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
if msgType != websocket.PingMessage {
|
||||
t.Fatal("invalid msg type", msgType)
|
||||
}
|
||||
|
||||
conn.WriteMessage(websocket.PongMessage, []byte{})
|
||||
|
||||
conn.WriteMessage(websocket.PingMessage, []byte{})
|
||||
|
||||
msgType, msg, err = conn.ReadMessage()
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
println(msgType)
|
||||
if msgType != websocket.PongMessage {
|
||||
|
||||
if msgType != websocket.TextMessage {
|
||||
t.Fatal("invalid msg type", msgType)
|
||||
}
|
||||
conn.WriteMessage(websocket.PongMessage, []byte("server.Pong"))
|
||||
})
|
||||
|
||||
go http.ListenAndServe(":65500", nil)
|
||||
|
@ -61,7 +65,7 @@ func TestWSClient(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
ws, _, err := NewClient(conn, &url.URL{Host: "127.0.0.1:65500", Path: "/test/client"}, nil)
|
||||
ws, _, err := NewClient(conn, &url.URL{Scheme: "ws", Host: "127.0.0.1:65501", Path: "/test/client"}, nil)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
|
@ -72,9 +76,12 @@ func TestWSClient(t *testing.T) {
|
|||
payload[i] = 'x'
|
||||
}
|
||||
|
||||
ws.WriteString(payload)
|
||||
err = ws.WriteString(payload)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
msgType, msg, err := ws.Read()
|
||||
msgType, msg, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
@ -84,11 +91,14 @@ func TestWSClient(t *testing.T) {
|
|||
|
||||
if string(msg) != string(payload) {
|
||||
t.Fatal("invalid msg", string(msg))
|
||||
|
||||
}
|
||||
|
||||
//test ping
|
||||
ws.Ping([]byte{})
|
||||
err = ws.Ping([]byte("client.Ping"))
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
msgType, msg, err = ws.ReadMessage()
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
|
@ -97,4 +107,8 @@ func TestWSClient(t *testing.T) {
|
|||
t.Fatal("invalid msg type", msgType)
|
||||
}
|
||||
|
||||
ws.WriteMessage(websocket.TextMessage, []byte("done"))
|
||||
|
||||
// ws.Close()
|
||||
wg.Wait()
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ func TestWSServer(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
ws, _, err := websocket.NewClient(conn, &url.URL{Host: "127.0.0.1:65500", Path: "/test/server"}, nil, 1024, 1024)
|
||||
ws, _, err := websocket.NewClient(conn, &url.URL{Scheme: "ws", Host: "127.0.0.1:65500", Path: "/test/server"}, nil, 1024, 1024)
|
||||
|
||||
ws.SetPongHandler(func(string) error {
|
||||
println("pong")
|
||||
|
|
Loading…
Reference in New Issue