Permalink
Browse files

added tests for JSONP transport and a --globals flag in Makefile

  • Loading branch information...
1 parent 0450cc4 commit 17e3e525829ae4a51deeca8371c39a1e07d658e4 @jphire jphire committed Feb 24, 2013
Showing with 159 additions and 0 deletions.
  1. +1 −0 Makefile
  2. +158 −0 test/server.js
View
@@ -9,6 +9,7 @@ test:
--reporter $(REPORTER) \
--slow 500ms \
--bail \
+ --globals ___eio,document \
$(TESTS)
test-cov: lib-cov
View
@@ -1121,4 +1121,162 @@ 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) {
+ function Script() {}
+
+ Script.prototype.__defineGetter__('parentNode', function () {
+ return document.body;
+ });
+
+ Script.prototype.__defineSetter__('src', function (uri) {
+ request.get(uri).end(function(res) {
+ eval(res.text);
+ });
+ });
+ return new Script();
+ }
+ else if ('form' == name) {
+ function Form() {
+ this.style = {};
+ this.action = '';
+ this.parentNode = { removeChild: function () {} }
+ this.removeChild = function () {}
+ this.setAttribute = function () {}
+ this.appendChild = function (elem) {
+ this.area = elem;
+ }
+ this.submit = function () {
+ request.post(this.action).type('form').send({ d: self.areaValue }).end(function (res) {});
+ }
+ }
+ return new Form();
+ }
+ else if ('textarea' == name) {
+ function Area() {}
+
+ //a hack to be able to access the area data when form is sent
+ Area.prototype.__defineSetter__('value', function (data) {
+ self.areaValue = data;
+ });
+ return new Area();
+ } 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 17e3e52

Please sign in to comment.