forked from socketio/engine.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonp.js
232 lines (203 loc) · 6.54 KB
/
jsonp.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
/**
* Module dependencies.
*/
var http = require('http');
var eioc = require('engine.io-client');
var listen = require('./common').listen;
var expect = require('expect.js');
var request = require('superagent');
var WebSocket = require('ws');
describe('JSONP', function () {
before(function () {
// we have to override the browser's functionality for JSONP
document = {
body: {
appendChild: function(){},
removeChild: function(){}
}
};
document.createElement = function (name) {
var self = this;
if('script' == name) {
var script = {};
script.__defineGetter__('parentNode', function () {
return document.body;
});
script.__defineSetter__('src', function (uri) {
request.get(uri).end(function(res) {
eval(res.text);
});
});
return script;
} else if ('form' == name) {
var form = {
style: {},
action: '',
parentNode: { removeChild: function(){} },
removeChild: function(){},
setAttribute: function(){},
appendChild: function(){},
submit: function(){
request
.post(this.action)
.type('form')
.send({ d: self.areaValue })
.end(function(){});
}
};
return form;
} else if ('textarea' == name) {
var textarea = {};
//a hack to be able to access the area data when form is sent
textarea.__defineSetter__('value', function (data) {
self.areaValue = data;
});
return textarea;
} else if (~name.indexOf('iframe')) {
var iframe = {};
setTimeout(function() {
if (iframe.onload) iframe.onload();
}, 0);
return iframe;
} else {
return {};
}
}
document.getElementsByTagName = function (name) {
return [{
parentNode: {
insertBefore: function () {}
}
}]
}
});
after(function () {
delete document.getElementsByTagName
, document.createElement;
delete global.document;
});
describe('handshake', function () {
it('should open with polling JSONP when requested', function (done) {
var engine = listen( { allowUpgrades: false, transports: ['polling'] }, function (port) {
var socket = new eioc.Socket('ws://localhost:' + port
, { transports: ['polling'], forceJSONP: true, upgrade: false });
engine.on('connection', function (socket) {
expect(socket.transport.name).to.be('polling');
expect(socket.transport.head).to.be('___eio[0](');
done();
});
});
});
});
describe('messages', function () {
var engine, port, socket;
beforeEach(function(done) {
engine = listen( { allowUpgrades: false, transports: ['polling'] }, function (p) {
port = p;
socket = new eioc.Socket('ws://localhost:' + port
, { transports: ['polling'], forceJSONP: true, upgrade: false });
done();
});
});
it('should arrive from client to server and back (pollingJSONP)', function (done) {
engine.on('connection', function (conn) {
conn.on('message', function (msg) {
conn.send('a');
});
});
socket.on('open', function () {
socket.send('a');
socket.on('message', function (msg) {
expect(socket.transport.query.j).to.not.be(undefined);
expect(msg).to.be('a');
done();
});
});
});
it('should not fail JSON.parse for stringified messages', function (done) {
engine.on('connection', function(conn) {
conn.on('message', function(message) {
expect(JSON.parse(message)).to.be.eql({test : 'a\r\nb\n\n\n\nc'});
done();
});
});
socket.on('open', function () {
socket.send(JSON.stringify({test : 'a\r\nb\n\n\n\nc'}));
});
});
it('should parse newlines in message correctly', function (done) {
engine.on('connection', function(conn) {
conn.on('message', function(message) {
expect(message).to.be.equal('a\r\nb\n\n\n\nc');
done();
});
});
socket.on('open', function () {
socket.send('a\r\nb\n\n\n\nc');
});
});
it('should arrive from server to client and back with binary data (pollingJSONP)', function(done) {
var binaryData = new Buffer(5);
for (var i = 0; i < 5; i++) binaryData[i] = i;
engine.on('connection', function (conn) {
conn.on('message', function (msg) {
conn.send(msg);
});
});
socket.on('open', function() {
socket.send(binaryData);
socket.on('message', function (msg) {
for (var i = 0; i < msg.length; i++) expect(msg[i]).to.be(i);
done();
});
});
});
});
describe('close', function () {
it('should trigger when server closes a client', function (done) {
var engine = listen( { allowUpgrades: false, transports: ['polling'] }, function (port) {
var socket = new eioc.Socket('ws://localhost:' + port
, { transports: ['polling'], forceJSONP: true, upgrade: false })
, total = 2;
engine.on('connection', function (conn) {
conn.on('close', function (reason) {
expect(reason).to.be('forced close');
--total || done();
});
setTimeout(function () {
conn.close();
}, 10);
});
socket.on('open', function () {
socket.on('close', function (reason) {
expect(reason).to.be('transport close');
--total || done();
});
});
});
});
it('should trigger when client closes', function (done) {
var engine = listen( { allowUpgrades: false, transports: ['polling'] }, function (port) {
var socket = new eioc.Socket('ws://localhost:' + port
, { transports: ['polling'], forceJSONP: true, upgrade: false })
, total = 2;
engine.on('connection', function (conn) {
conn.on('close', function (reason) {
expect(reason).to.be('transport close');
--total || done();
});
});
socket.on('open', function () {
socket.send('a');
socket.on('close', function (reason) {
expect(reason).to.be('forced close');
--total || done();
});
setTimeout(function () {
socket.close();
}, 10);
});
});
});
});
});