-
Notifications
You must be signed in to change notification settings - Fork 571
/
engine.io.js
253 lines (216 loc) · 6.92 KB
/
engine.io.js
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
243
244
245
246
247
248
249
250
251
252
253
const net = require("net");
const { Server, protocol, attach } = require("..");
const listen = require("./common").listen;
const expect = require("expect.js");
const request = require("superagent");
const http = require("http");
/**
* Tests.
*/
describe("engine", () => {
it("should expose protocol number", () => {
expect(protocol).to.be.a("number");
});
it("should be the same version as client", () => {
const version = require("../package.json").version;
expect(version).to.be(require("engine.io-client/package.json").version);
});
describe("engine()", () => {
it("should create a Server when require called with no arguments", () => {
const engine = new Server();
expect(engine).to.be.an(Server);
expect(engine.ws).to.be.ok();
});
it("should pass options correctly to the Server", () => {
const engine = new Server({ cors: true });
expect(engine.opts).to.have.property("cors", true);
});
});
describe("listen", () => {
it("should open a http server that returns 501", function(done) {
if (process.env.EIO_WS_ENGINE === "uws") {
return this.skip();
}
listen(port => {
request.get(`http://localhost:${port}`, (err, res) => {
expect(err).to.be.an(Error);
expect(res.status).to.be(501);
done();
});
});
});
});
describe("attach()", () => {
it("should work from require()", () => {
const server = http.createServer();
const engine = new Server(server);
expect(engine).to.be.an(Server);
});
it("should return an engine.Server", () => {
const server = http.createServer();
const engine = attach(server);
expect(engine).to.be.an(Server);
});
it("should attach engine to an http server", done => {
const server = http.createServer();
attach(server);
server.listen(() => {
const uri = `http://localhost:${server.address().port}/engine.io/`;
request.get(uri, (err, res) => {
expect(err).to.be.an(Error);
expect(res.status).to.be(400);
expect(res.body.code).to.be(0);
expect(res.body.message).to.be("Transport unknown");
server.once("close", done);
server.close();
});
});
});
it("should destroy upgrades not handled by engine", done => {
const server = http.createServer();
attach(server, { destroyUpgradeTimeout: 50 });
server.listen(() => {
const client = net.createConnection(server.address().port);
client.setEncoding("ascii");
client.write(
[
"GET / HTTP/1.1",
"Connection: Upgrade",
"Upgrade: IRC/6.9",
"",
""
].join("\r\n")
);
const check = setTimeout(() => {
done(new Error("Client should have ended"));
}, 100);
client.on("end", () => {
clearTimeout(check);
done();
});
});
});
it("should not destroy unhandled upgrades with destroyUpgrade:false", done => {
const server = http.createServer();
attach(server, { destroyUpgrade: false, destroyUpgradeTimeout: 50 });
server.listen(() => {
const client = net.createConnection(server.address().port);
client.on("connect", () => {
client.setEncoding("ascii");
client.write(
[
"GET / HTTP/1.1",
"Connection: Upgrade",
"Upgrade: IRC/6.9",
"",
""
].join("\r\n")
);
setTimeout(() => {
client.removeListener("end", onEnd);
done();
}, 100);
function onEnd() {
done(new Error("Client should not end"));
}
client.on("end", onEnd);
});
});
});
it("should destroy unhandled upgrades with after a timeout", done => {
const server = http.createServer();
attach(server, { destroyUpgradeTimeout: 200 });
server.listen(() => {
const client = net.createConnection(server.address().port);
client.on("connect", () => {
client.setEncoding("ascii");
client.write(
[
"GET / HTTP/1.1",
"Connection: Upgrade",
"Upgrade: IRC/6.9",
"",
""
].join("\r\n")
);
// send from client to server
// tests that socket is still alive
// this will not keep the socket open as the server does not handle it
setTimeout(() => {
client.write("foo");
}, 100);
function onEnd() {
done();
}
client.on("end", onEnd);
});
});
});
it("should not destroy handled upgrades with after a timeout", done => {
const server = http.createServer();
attach(server, { destroyUpgradeTimeout: 100 });
// write to the socket to keep engine.io from closing it by writing before the timeout
server.on("upgrade", (req, socket) => {
socket.write("foo");
socket.on("data", chunk => {
expect(chunk.toString()).to.be("foo");
socket.end();
});
});
server.listen(() => {
const client = net.createConnection(server.address().port);
client.on("connect", () => {
client.setEncoding("ascii");
client.write(
[
"GET / HTTP/1.1",
"Connection: Upgrade",
"Upgrade: IRC/6.9",
"",
""
].join("\r\n")
);
// test that socket is still open by writing after the timeout period
setTimeout(() => {
client.write("foo");
}, 200);
client.on("data", data => {});
client.on("end", done);
});
});
});
it("should preserve original request listeners", done => {
let listeners = 0;
const server = http.createServer((req, res) => {
expect(req && res).to.be.ok();
listeners++;
});
server.on("request", (req, res) => {
expect(req && res).to.be.ok();
res.writeHead(200);
res.end("");
listeners++;
});
attach(server);
server.listen(() => {
const port = server.address().port;
request.get(
`http://localhost:${port}/engine.io/default/`,
(err, res) => {
expect(err).to.be.an(Error);
expect(res.status).to.be(400);
expect(res.body.code).to.be(0);
expect(res.body.message).to.be("Transport unknown");
request.get(`http://localhost:${port}/test`, (err, res) => {
expect(err).to.be(null);
expect(res.status).to.be(200);
expect(listeners).to.eql(2);
server.once("close", done);
server.close();
});
}
);
});
});
});
});