Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#112 Figure out optimal testing strategy for JSONP #151

Merged
merged 2 commits into from
Mar 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test:
--reporter $(REPORTER) \ --reporter $(REPORTER) \
--slow 500ms \ --slow 500ms \
--bail \ --bail \
--globals ___eio,document \
$(TESTS) $(TESTS)


test-cov: lib-cov test-cov: lib-cov
Expand Down
160 changes: 160 additions & 0 deletions test/server.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1121,4 +1121,164 @@ 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);
});
});
});
});
});

}); });