-
Notifications
You must be signed in to change notification settings - Fork 2
/
conn_test.go
122 lines (105 loc) · 2.29 KB
/
conn_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package gws
import (
"context"
"fmt"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/zaba505/gws/backoff"
"nhooyr.io/websocket"
)
func TestWithDialOptions(t *testing.T) {
aOpts := &websocket.AcceptOptions{
Subprotocols: []string{"graphql-ws"},
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
wc, err := websocket.Accept(w, req, aOpts)
if err != nil {
t.Fail()
return
}
wc.CloseRead(context.Background())
m := req.Header.Get("Hello")
if m != "World" {
t.Fail()
}
}))
defer srv.Close()
headers := make(http.Header)
headers.Add("Hello", "World")
opts := []DialOption{
WithHTTPClient(http.DefaultClient),
WithHeaders(headers),
WithCompression(CompressionDisabled, 0),
}
conn, err := Dial(context.Background(), "ws://"+srv.Listener.Addr().String(), opts...)
if err != nil {
t.Error(err)
return
}
conn.Close()
}
func TestTerminate(t *testing.T) {
srv := newTestServer(func(conn *Conn) {
defer conn.wc.CloseRead(context.Background())
b, err := conn.read(context.Background())
if err != nil {
t.Error(err)
return
}
msg := new(operationMessage)
err = msg.UnmarshalJSON(b)
if err != nil {
t.Error(err)
return
}
if msg.Type != gqlConnectionTerminate {
t.Log("wrong message:", msg)
t.Fail()
return
}
})
defer srv.Close()
conn, err := Dial(context.Background(), "ws://"+srv.Listener.Addr().String())
if err != nil {
t.Error(err)
return
}
conn.Close()
}
func TestDialBackoff(t *testing.T) {
ls, err := net.Listen("tcp", ":0")
if err != nil {
t.Error(err)
return
}
defer ls.Close()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
addr := ls.Addr().(*net.TCPAddr)
_, err = Dial(
ctx,
fmt.Sprintf("ws://localhost:%d", addr.Port),
WithConnectParams(ConnectParams{
Backoff: backoff.DefaultConfig,
MinConnectTimeout: 1 * time.Millisecond,
}),
)
if err == nil {
t.Fail()
return
}
}
func ExampleDial() {
conn, err := Dial(context.TODO(), "ws://example.com")
if err != nil {
// Make sure to handle the error
return
}
defer conn.Close()
// Create a single client with the connection.
// There is no need to create multiple connections or clients
// because it will all be managed for you.
}