forked from coder/websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
54 lines (44 loc) · 1021 Bytes
/
server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"context"
"net/http/httptest"
"testing"
"time"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
)
// Test_echoServer tests the echoServer by sending it 5 different messages
// and ensuring the responses all match.
func Test_echoServer(t *testing.T) {
t.Parallel()
s := httptest.NewServer(echoServer{
logf: t.Logf,
})
defer s.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
c, _, err := websocket.Dial(ctx, s.URL, &websocket.DialOptions{
Subprotocols: []string{"echo"},
})
if err != nil {
t.Fatal(err)
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
for i := 0; i < 5; i++ {
err = wsjson.Write(ctx, c, map[string]int{
"i": i,
})
if err != nil {
t.Fatal(err)
}
v := map[string]int{}
err = wsjson.Read(ctx, c, &v)
if err != nil {
t.Fatal(err)
}
if v["i"] != i {
t.Fatalf("expected %v but got %v", i, v)
}
}
c.Close(websocket.StatusNormalClosure, "")
}