Skip to content

Commit

Permalink
test: moved jsonp tests into own file
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg committed Mar 17, 2013
1 parent 97ea406 commit 88071ee
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 160 deletions.
162 changes: 162 additions & 0 deletions test/jsonp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*global eio,eioc,listen,request,expect*/

var http = require('http');
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 (elem) { area: elem; }
, submit: function () {
request.post(this.action).type('form').send({ d: self.areaValue }).end(function (res) {});
}
}
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 {
return {};
}
}

document.getElementsByTagName = function (name) {
return [{
parentNode: {
insertBefore: function () {}
}
}]
}
});

after(function () {
delete document.getElementsByTagName
, document.createElement
, 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 () {
it('should arrive from client to server and back (pollingJSONP)', 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 (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();
});
});
});
});
});

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);
});
});
});
});
});
160 changes: 0 additions & 160 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1152,164 +1152,4 @@ describe('server', function () {
});
});

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 (elem) { area: elem; }
, submit: function () {
request.post(this.action).type('form').send({ d: self.areaValue }).end(function (res) {});
}
}
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 {
return {}
}
}

document.getElementsByTagName = function (name) {
return [{
parentNode: {
insertBefore: function () {}
}
}]
}
});

after(function () {
delete document.getElementsByTagName
, document.createElement
, 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 () {
it('should arrive from client to server and back (pollingJSONP)', 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 (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();
});
});
});
});
});

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);
});
});
});
});
});

});

0 comments on commit 88071ee

Please sign in to comment.