Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

#112 Figure out optimal testing strategy for JSONP #151

Merged
merged 2 commits into from

2 participants

Janne Laukkanen Guillermo Rauch
Janne Laukkanen

Not sure whether these JSONP tests should be separated from other tests like this at the bottom, or should we put them among the other tests. This way though only need to override document{} functionality once.

test/server.js
@@ -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() {}
Guillermo Rauch Owner
rauchg added a note

You can use a simple {} and then call defineGetter on them.

Guillermo Rauch Owner
rauchg added a note

Same for other elements below

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Guillermo Rauch rauchg merged commit 5a2ee6c into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 24, 2013
  1. Janne Laukkanen
Commits on Feb 27, 2013
  1. Janne Laukkanen

    added style fix for #112

    jphire authored
This page is out of date. Refresh to see the latest.
Showing with 161 additions and 0 deletions.
  1. +1 −0  Makefile
  2. +160 −0 test/server.js
1  Makefile
View
@@ -9,6 +9,7 @@ test:
--reporter $(REPORTER) \
--slow 500ms \
--bail \
+ --globals ___eio,document \
$(TESTS)
test-cov: lib-cov
160 test/server.js
View
@@ -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);
+ });
+ });
+ });
+ });
+ });
+
});
Something went wrong with that request. Please try again.