18
18
19
19
func TestProxy (t * testing.T ) {
20
20
// websocket proxy
21
+ u , _ := url .Parse (backendURL )
21
22
supportedSubProtocols := []string {"test-protocol" }
22
23
upgrader := & websocket.Upgrader {
23
24
ReadBufferSize : 4096 ,
@@ -28,13 +29,12 @@ func TestProxy(t *testing.T) {
28
29
Subprotocols : supportedSubProtocols ,
29
30
}
30
31
31
- u , _ := url .Parse (backendURL )
32
32
proxy := NewProxy (u )
33
33
proxy .Upgrader = upgrader
34
34
35
- mux := http .NewServeMux ()
36
- mux .Handle ("/proxy" , proxy )
37
35
go func () {
36
+ mux := http .NewServeMux ()
37
+ mux .Handle ("/proxy" , proxy )
38
38
if err := http .ListenAndServe (":7777" , mux ); err != nil {
39
39
t .Fatal ("ListenAndServe: " , err )
40
40
}
@@ -43,6 +43,7 @@ func TestProxy(t *testing.T) {
43
43
time .Sleep (time .Millisecond * 100 )
44
44
45
45
// backend echo server
46
+ websocketMsgRcverCBackend := make (chan websocketMsg , 1 )
46
47
go func () {
47
48
mux2 := http .NewServeMux ()
48
49
mux2 .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
@@ -52,13 +53,16 @@ func TestProxy(t *testing.T) {
52
53
return
53
54
}
54
55
55
- messageType , p , err := conn .ReadMessage ()
56
- if err != nil {
57
- return
58
- }
56
+ for {
57
+ messageType , p , err := conn .ReadMessage ()
58
+ if err != nil {
59
+ websocketMsgRcverCBackend <- websocketMsg {messageType , p , err }
60
+ return
61
+ }
59
62
60
- if err = conn .WriteMessage (messageType , p ); err != nil {
61
- return
63
+ if err = conn .WriteMessage (messageType , p ); err != nil {
64
+ return
65
+ }
62
66
}
63
67
})
64
68
@@ -70,8 +74,8 @@ func TestProxy(t *testing.T) {
70
74
71
75
time .Sleep (time .Millisecond * 100 )
72
76
73
- // let's us define two subprotocols, only one is supported by the server
74
- clientSubProtocols := []string {"test-protocol" , "test- notsupported" }
77
+ // define subprotocols for client, appending one not supported by the server
78
+ clientSubProtocols := append ( supportedSubProtocols , []string {"test-notsupported" }... )
75
79
h := http.Header {}
76
80
for _ , subprot := range clientSubProtocols {
77
81
h .Add ("Sec-WebSocket-Protocol" , subprot )
@@ -102,8 +106,7 @@ func TestProxy(t *testing.T) {
102
106
t .Error ("test-notsupported should be not recevied from the server." )
103
107
}
104
108
105
- // now write a message and send it to the backend server (which goes trough
106
- // proxy..)
109
+ // send msg to the backend server which goes through proxy
107
110
msg := "hello kite"
108
111
err = conn .WriteMessage (websocket .TextMessage , []byte (msg ))
109
112
if err != nil {
@@ -123,5 +126,20 @@ func TestProxy(t *testing.T) {
123
126
t .Errorf ("expecting: %s, got: %s" , msg , string (p ))
124
127
}
125
128
129
+ // shutdown procedure
130
+ //
131
+ backendErrMsg := "websocketproxy: closing connection"
126
132
proxy .Shutdown (context .Background ())
133
+
134
+ wsErrBackend := <- websocketMsgRcverCBackend
135
+ e , ok := wsErrBackend .err .(* websocket.CloseError )
136
+ if ! ok {
137
+ t .Fatal ("backend error is not websocket.CloseError" )
138
+ }
139
+ if e .Code != websocket .CloseGoingAway {
140
+ t .Error ("backend error code is not websocket.CloseGoingAway" )
141
+ }
142
+ if e .Text != backendErrMsg {
143
+ t .Errorf ("backend error test expecting: %s, got: %s" , backendErrMsg , e .Text )
144
+ }
127
145
}
0 commit comments