-
Notifications
You must be signed in to change notification settings - Fork 11
/
websockethandler_test.go
133 lines (114 loc) · 2.57 KB
/
websockethandler_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
123
124
125
126
127
128
129
130
131
132
133
package j8a
import (
"io"
"net"
"net/http"
"net/http/httptest"
"testing"
)
// this testHandler binds the mock HTTP server to proxyHandler.
type WebsocketHandler struct{}
func (h WebsocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
websocketHandler(w, r)
}
func TestWebSocketHandler(t *testing.T) {
Runner = mockRuntime()
h := &WebsocketHandler{}
server := httptest.NewServer(h)
defer server.Close()
c := &http.Client{}
resp, err := c.Get(server.URL)
if resp == nil {
t.Error("no HTTP resonse")
} else if resp.StatusCode != 502 {
t.Errorf("wanted 502 for bad gateway but got: %v", err)
} else if err != nil {
t.Errorf("got HTTP error: %v", err)
}
}
func TestUpgradeWebsocket(t *testing.T) {
Runner = mockRuntime()
p := mockProxyWS()
upgradeWebsocket(&p)
//test only nil pointer
}
func TestLogExitStatusNetOpError(t *testing.T) {
mpws := mockProxyWS()
x := WebsocketStatus{
DwnOpCode: 0,
DwnExit: &net.OpError{
Op: "",
Net: "",
Source: nil,
Addr: nil,
Err: io.EOF,
},
UpOpCode: 0,
UpExit: nil,
}
mpws.logWebsocketConnectionExitStatus(x)
}
func TestScaffoldHTTPUpgrader(t *testing.T) {
Runner = mockRuntime()
mpws := mockProxyWS()
scaffoldHTTPUpgrader(&mpws)
//coverage only
}
func TestReadUpWS(t *testing.T) {
Runner = mockRuntime()
Runner.Connection.Upstream.IdleTimeoutSeconds = 1
Runner.Connection.Downstream.IdleTimeoutSeconds = 1
mpws := mockProxyWS()
wss := make(chan WebsocketStatus)
server, client := net.Pipe()
go readUpWebsocket(server, client, &mpws, wss, &WebsocketTx{})
res := <-wss
if res.UpExit == nil {
t.Error("should have received upErr, got nil")
}
//coverage only
}
func TestReadDwnWS(t *testing.T) {
Runner = mockRuntime()
Runner.Connection.Upstream.IdleTimeoutSeconds = 1
Runner.Connection.Downstream.IdleTimeoutSeconds = 1
mpws := mockProxyWS()
wss := make(chan WebsocketStatus)
server, client := net.Pipe()
go readDwnWebsocket(server, client, &mpws, wss, &WebsocketTx{})
res := <-wss
if res.DwnExit == nil {
t.Error("should have received dwnErr, got nil")
}
//coverage only
}
func mockProxyWS() Proxy {
return Proxy{
XRequestID: "1",
XRequestInfo: false,
Up: Up{
Atmpt: &Atmpt{
URL: &URL{
Scheme: "ws",
Host: "localhost",
Port: "80",
},
},
},
Dwn: Down{
Req: &http.Request{
RemoteAddr: "10.1.1.1",
},
Resp: Resp{
Writer: &httptest.ResponseRecorder{},
},
Method: "GET",
Path: "/path",
},
Route: &Route{
Path: "/path",
CompiledPathRegex: nil,
Resource: "res",
},
}
}