-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
websockets_spec.coffee
242 lines (182 loc) · 6.82 KB
/
websockets_spec.coffee
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
require("../spec_helper")
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
ws = require("ws")
httpsAgent = require("https-proxy-agent")
evilDns = require("evil-dns")
Promise = require("bluebird")
socketIo = require("#{root}../socket")
httpsServer = require("#{root}../https-proxy/test/helpers/https_server")
config = require("#{root}lib/config")
Server = require("#{root}lib/server")
Automation = require("#{root}lib/automation")
Fixtures = require("#{root}/test/support/helpers/fixtures")
cyPort = 12345
otherPort = 5555
wsPort = 20000
wssPort = 8443
describe "Web Sockets", ->
beforeEach ->
Fixtures.scaffold()
@idsPath = Fixtures.projectPath("ids")
config.get(@idsPath, {port: cyPort})
.then (@cfg) =>
@ws = new ws.Server({port: wsPort})
@server = Server()
@server.open(@cfg)
.then =>
httpsServer.start(wssPort)
.then (httpsSrv) =>
@wss = new ws.Server({server: httpsSrv})
afterEach ->
Fixtures.remove()
evilDns.clear()
@ws.close()
@wss.close()
Promise.join(
@server.close(),
httpsServer.stop()
)
context "proxying external websocket requests", ->
it "ends the socket connection without remoteHost", (done) ->
@server._onDomainSet()
client = new ws("ws://localhost:#{cyPort}")
client.on "error", (err) ->
expect(err.code).to.eq("ECONNRESET")
done()
it "sends back ECONNRESET when error upgrading", (done) ->
agent = new httpsAgent("http://localhost:#{cyPort}")
@server._onDomainSet("http://localhost:#{otherPort}")
client = new ws("ws://localhost:#{otherPort}", {
agent: agent
})
client.on "error", (err) ->
expect(err.code).to.eq('ECONNRESET')
expect(err.message).to.eq('socket hang up')
done()
it "proxies https messages", (done) ->
@server._onDomainSet("https://localhost:#{wssPort}")
@wss.on "connection", (c) ->
c.on "message", (msg) ->
c.send "response:#{msg}"
client = new ws("ws://localhost:#{cyPort}")
client.on "message", (data) ->
expect(data).to.eq("response:foo")
done()
client.on "open", ->
client.send("foo")
it "proxies http messages through http proxy", (done) ->
## force node into legit proxy mode like a browser
agent = new httpsAgent("http://localhost:#{cyPort}")
@server._onDomainSet("http://localhost:#{wsPort}")
@ws.on "connection", (c) ->
c.on "message", (msg) ->
c.send "response:#{msg}"
client = new ws("ws://localhost:#{wsPort}", {
agent: agent
})
client.on "message", (data) ->
expect(data).to.eq("response:foo")
done()
client.on "open", ->
client.send("foo")
it "proxies https messages through http", (done) ->
## force node into legit proxy mode like a browser
agent = new httpsAgent({
host: "localhost"
port: cyPort
rejectUnauthorized: false
})
@server._onDomainSet("https://localhost:#{wssPort}")
@wss.on "connection", (c) ->
c.on "message", (msg) ->
c.send "response:#{msg}"
client = new ws("wss://localhost:#{wssPort}", {
agent: agent
})
client.on "message", (data) ->
expect(data).to.eq("response:foo")
done()
client.on "open", ->
client.send("foo")
it "proxies through subdomain by using host header", (done) ->
## we specifically only allow remote connections
## to ws.foobar.com since that is where the websocket
## server is mounted and this tests that we make
## a connection to the right host instead of the
## origin (which isnt ws.foobar.com)
nock.enableNetConnect("ws.foobar.com")
evilDns.add("ws.foobar.com", "127.0.0.1")
## force node into legit proxy mode like a browser
agent = new httpsAgent({
host: "localhost"
port: cyPort
rejectUnauthorized: false
})
@server._onDomainSet("https://foobar.com:#{wssPort}")
@wss.on "connection", (c) ->
c.on "message", (msg) ->
c.send "response:#{msg}"
client = new ws("wss://ws.foobar.com:#{wssPort}", {
agent: agent
})
client.on "message", (data) ->
expect(data).to.eq("response:foo")
done()
client.on "open", ->
client.send("foo")
context "socket.io handling", ->
beforeEach ->
@automation = Automation.create(@cfg.namespace, @cfg.socketIoCookie, @cfg.screenshotsFolder)
@server.startWebsockets(@automation, @cfg, {})
context "http", ->
beforeEach (done) ->
@wsClient = socketIo.client(@cfg.proxyUrl, {
path: @cfg.socketIoRoute
transports: ["websocket"]
parser: socketIo.circularParser
})
@wsClient.on "connect", -> done()
afterEach ->
@wsClient.disconnect()
it "continues to handle socket.io requests just fine", (done) ->
@wsClient.emit "backend:request", "get:fixture", "example.json", {}, (data) ->
expect(data.response).to.deep.eq({foo: "bar"})
done()
context "when http superDomain has been set", ->
beforeEach (done) ->
## force node into legit proxy mode like a browser
agent = new httpsAgent("http://localhost:#{cyPort}")
@server._onDomainSet("http://localhost:#{otherPort}")
@wsClient = socketIo.client("http://localhost:#{otherPort}", {
agent: agent
path: @cfg.socketIoRoute
transports: ["websocket"]
parser: socketIo.circularParser
})
@wsClient.on "connect", -> done()
afterEach ->
@wsClient.disconnect()
it "continues to handle socket.io requests just fine", (done) ->
@wsClient.emit "backend:request", "get:fixture", "example.json", {}, (data) ->
expect(data.response).to.deep.eq({foo: "bar"})
done()
context "when https superDomain has been set", ->
beforeEach (done) ->
## force node into legit proxy mode like a browser
agent = new httpsAgent("http://localhost:#{cyPort}")
@server._onDomainSet("https://localhost:#{wssPort}")
## must not use a direct websocket transport else we'll
## never make it to the /__socket.io route
@wsClient = socketIo.client("https://localhost:#{wssPort}", {
agent: agent
path: @cfg.socketIoRoute
parser: socketIo.circularParser
rejectUnauthorized: false
})
@wsClient.on "connect", -> done()
afterEach ->
@wsClient.disconnect()
it "continues to handle socket.io requests just fine", (done) ->
@wsClient.emit "backend:request", "get:fixture", "example.json", {}, (data) ->
expect(data.response).to.deep.eq({foo: "bar"})
done()