Skip to content

Commit 0a477f5

Browse files
committed
add test for shutdown procedure; minor refactoring
1 parent 624f194 commit 0a477f5

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

websocketproxy_test.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var (
1818

1919
func TestProxy(t *testing.T) {
2020
// websocket proxy
21+
u, _ := url.Parse(backendURL)
2122
supportedSubProtocols := []string{"test-protocol"}
2223
upgrader := &websocket.Upgrader{
2324
ReadBufferSize: 4096,
@@ -28,13 +29,12 @@ func TestProxy(t *testing.T) {
2829
Subprotocols: supportedSubProtocols,
2930
}
3031

31-
u, _ := url.Parse(backendURL)
3232
proxy := NewProxy(u)
3333
proxy.Upgrader = upgrader
3434

35-
mux := http.NewServeMux()
36-
mux.Handle("/proxy", proxy)
3735
go func() {
36+
mux := http.NewServeMux()
37+
mux.Handle("/proxy", proxy)
3838
if err := http.ListenAndServe(":7777", mux); err != nil {
3939
t.Fatal("ListenAndServe: ", err)
4040
}
@@ -43,6 +43,7 @@ func TestProxy(t *testing.T) {
4343
time.Sleep(time.Millisecond * 100)
4444

4545
// backend echo server
46+
websocketMsgRcverCBackend := make(chan websocketMsg, 1)
4647
go func() {
4748
mux2 := http.NewServeMux()
4849
mux2.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@@ -52,13 +53,16 @@ func TestProxy(t *testing.T) {
5253
return
5354
}
5455

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+
}
5962

60-
if err = conn.WriteMessage(messageType, p); err != nil {
61-
return
63+
if err = conn.WriteMessage(messageType, p); err != nil {
64+
return
65+
}
6266
}
6367
})
6468

@@ -70,8 +74,8 @@ func TestProxy(t *testing.T) {
7074

7175
time.Sleep(time.Millisecond * 100)
7276

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"}...)
7579
h := http.Header{}
7680
for _, subprot := range clientSubProtocols {
7781
h.Add("Sec-WebSocket-Protocol", subprot)
@@ -102,8 +106,7 @@ func TestProxy(t *testing.T) {
102106
t.Error("test-notsupported should be not recevied from the server.")
103107
}
104108

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
107110
msg := "hello kite"
108111
err = conn.WriteMessage(websocket.TextMessage, []byte(msg))
109112
if err != nil {
@@ -123,5 +126,20 @@ func TestProxy(t *testing.T) {
123126
t.Errorf("expecting: %s, got: %s", msg, string(p))
124127
}
125128

129+
// shutdown procedure
130+
//
131+
backendErrMsg := "websocketproxy: closing connection"
126132
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+
}
127145
}

0 commit comments

Comments
 (0)