-
Notifications
You must be signed in to change notification settings - Fork 59
/
websocket.io.js
87 lines (70 loc) · 2.06 KB
/
websocket.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
/**
* Test dependencies.
*/
var ws = require('../lib/websocket.io')
, http = require('http')
/**
* Tests.
*/
describe('websocket.io', function () {
it('must expose version number', function () {
ws.version.should.match(/[0-9]+\.[0-9]+\.[0-9]+/);
});
it('must expose public constructors', function () {
ws.Socket.should.be.a('function');
ws.Server.should.be.a('function');
ws.protocols.drafts.should.be.a('function');
ws.protocols['7'].should.be.a('function');
ws.protocols['8'].should.be.a('function');
ws.protocols['13'].should.be.a('function');
});
it('must connect', function (done) {
listen(function (addr, server) {
var cl = client(addr);
cl.on('open', function () {
cl.close();
server.close();
done();
});
});
});
it('must handle upgrade requests of a normal http server', function (done) {
var httpServer = http.createServer(function (req, res) {
res.writeHead(200);
res.end('Hello World');
})
var server = ws.attach(httpServer);
httpServer.listen(function (err) {
if (err) throw err;
var addr = httpServer.address();
http.get({ host: addr.address, port: addr.port }, function (res) {
res.on('data', function (data) {
data.toString().should.equal('Hello World');
var cl = client(addr);
cl.on('open', function () {
cl.close();
httpServer.close();
done();
});
});
});
});
});
it('must listen on a port', function (done) {
var server = ws.listen(null, function () {
var addr = server.httpServer.address();
http.get({ host: addr.address, port: addr.port }, function (res) {
res.statusCode.should.equal(501);
res.on('data', function (data) {
data.toString().should.equal('Not Implemented');
var cl = client(addr);
cl.on('open', function () {
cl.close();
server.httpServer.close();
done();
});
});
});
});
});
});