@@ -17,6 +17,41 @@ import (
17
17
"nhooyr.io/websocket"
18
18
)
19
19
20
+ func TestConn (t * testing.T ) {
21
+ t .Parallel ()
22
+
23
+ t .Run ("json" , func (t * testing.T ) {
24
+ s , closeFn := testServer (t , func (w http.ResponseWriter , r * http.Request ) {
25
+ c , err := websocket .Accept (w , r , & websocket.AcceptOptions {
26
+ Subprotocols : []string {"echo" },
27
+ InsecureSkipVerify : true ,
28
+ })
29
+ assert .Success (t , err )
30
+ defer c .Close (websocket .StatusInternalError , "" )
31
+
32
+ err = echoLoop (r .Context (), c )
33
+ assertCloseStatus (t , websocket .StatusNormalClosure , err )
34
+ }, false )
35
+ defer closeFn ()
36
+
37
+ wsURL := strings .Replace (s .URL , "http" , "ws" , 1 )
38
+
39
+ ctx , cancel := context .WithTimeout (context .Background (), time .Minute )
40
+ defer cancel ()
41
+
42
+ opts := & websocket.DialOptions {
43
+ Subprotocols : []string {"echo" },
44
+ }
45
+ opts .HTTPClient = s .Client ()
46
+
47
+ c , _ , err := websocket .Dial (ctx , wsURL , opts )
48
+ assert .Success (t , err )
49
+
50
+ assertJSONEcho (t , ctx , c , 2 )
51
+ })
52
+ }
53
+
54
+
20
55
func testServer (tb testing.TB , fn func (w http.ResponseWriter , r * http.Request ), tls bool ) (s * httptest.Server , closeFn func ()) {
21
56
h := http .HandlerFunc (fn )
22
57
if tls {
@@ -108,37 +143,3 @@ func echoLoop(ctx context.Context, c *websocket.Conn) error {
108
143
}
109
144
}
110
145
}
111
-
112
- func TestConn (t * testing.T ) {
113
- t .Parallel ()
114
-
115
- t .Run ("json" , func (t * testing.T ) {
116
- s , closeFn := testServer (t , func (w http.ResponseWriter , r * http.Request ) {
117
- c , err := websocket .Accept (w , r , & websocket.AcceptOptions {
118
- Subprotocols : []string {"echo" },
119
- InsecureSkipVerify : true ,
120
- })
121
- assert .Success (t , err )
122
- defer c .Close (websocket .StatusInternalError , "" )
123
-
124
- err = echoLoop (r .Context (), c )
125
- assertCloseStatus (t , websocket .StatusNormalClosure , err )
126
- }, false )
127
- defer closeFn ()
128
-
129
- wsURL := strings .Replace (s .URL , "http" , "ws" , 1 )
130
-
131
- ctx , cancel := context .WithTimeout (context .Background (), time .Minute )
132
- defer cancel ()
133
-
134
- opts := & websocket.DialOptions {
135
- Subprotocols : []string {"echo" },
136
- }
137
- opts .HTTPClient = s .Client ()
138
-
139
- c , _ , err := websocket .Dial (ctx , wsURL , opts )
140
- assert .Success (t , err )
141
-
142
- assertJSONEcho (t , ctx , c , 2 )
143
- })
144
- }
0 commit comments