Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added new common.js tests (expresso-like), with browser-compliant sho…

…uld().
  • Loading branch information...
commit 629f2a189dc4975b9717fad0290927850f2d539b 1 parent 579d816
@rauchg rauchg authored
View
120 test/events.test.js
@@ -0,0 +1,120 @@
+
+/*!
+ * socket.io-node
+ * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
+ * MIT Licensed
+ */
+
+(function (module, io, should) {
+
+ module.exports = {
+
+ 'add listeners': function () {
+ var event = new io.EventEmitter
+ , calls = 0;
+
+ event.on('test', function (a, b) {
+ ++calls;
+ a.should().eql('a');
+ b.should().eql('b');
+ });
+
+ event.emit('test', 'a', 'b');
+ calls.should().eql(1);
+ event.on.should().eql(event.addListener);
+ },
+
+ 'remove listener': function () {
+ var event = new io.EventEmitter;
+ function empty () { }
+
+ event.on('test', empty);
+ event.on('test:more', empty);
+ event.removeAllListeners('test');
+
+ event.listeners('test').should().eql([]);
+ event.listeners('test:more').should().eql([empty]);
+ },
+
+ 'remove all listeners': function () {
+ var event = new io.EventEmitter;
+ function empty () { }
+
+ event.on('test', empty);
+ event.on('test:more', empty);
+ event.removeAllListeners();
+
+ event.listeners('test').should().eql([empty]);
+ event.listeners('test:more').should().eql([empty]);
+ },
+
+ 'remove listeners functions': function () {
+ var event = new io.EventEmitter
+ , calls = 0;
+
+ function one () { ++calls }
+ function two () { ++calls }
+ function three () { ++calls }
+
+ event.on('one', one);
+ event.removeListener('one', one);
+ event.listeners('one').should().eql([]);
+
+ event.on('two', two);
+ event.removeListener('two', one);
+ event.listeners('two').should().eql([two]);
+
+ event.on('three', three);
+ event.on('three', two);
+ event.removeListener('three', three);
+ event.listeners('three').should().eql([two]);
+ },
+
+ 'number of arguments': function () {
+ var event = new io.EventEmitter
+ , number = [];
+
+ event.on('test', function () {
+ number.push(arguments.length);
+ });
+
+ event.emit('test');
+ event.emit('test', null);
+ event.emit('test', null, null);
+ event.emit('test', null, null, null);
+ event.emit('test', null, null, null, null);
+ event.emit('test', null, null, null, null, null);
+
+ [0, 1, 2, 3, 4, 5].should().eql(number);
+ },
+
+ 'once': function () {
+ var event = new io.EventEmitter
+ , calls = 0;
+
+ event.once('test', function (a, b) {
+ ++calls;
+ });
+
+ event.emit('test', 'a', 'b');
+ event.emit('test', 'a', 'b');
+ event.emit('test', 'a', 'b');
+
+ function removed () {
+ should().fail('not removed');
+ };
+
+ event.once('test:removed', removed);
+ event.removeListener('test:removed', removed);
+ event.emit('test:removed');
+
+ calls.should().eql(1);
+ }
+
+ };
+
+})(
+ 'undefined' == typeof module ? module = {} : module
+ , 'undefined' == typeof io ? require('socket.io-client') : io
+ , 'undefined' == typeof should || !should.fail ? require('should') : should
+);
View
31 test/io.test.js
@@ -0,0 +1,31 @@
+
+/*!
+ * socket.io-node
+ * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
+ * MIT Licensed
+ */
+
+(function (module, io, should) {
+
+ module.exports = {
+
+ 'client version number': function () {
+ io.version.should().match(/([0-9]+)\.([0-9]+)\.([0-9]+)/);
+ },
+
+ 'socket.io protocol version': function () {
+ io.protocol.should().be.a('number');
+ io.protocol.toString().should().match(/^\d+$/);
+ },
+
+ 'socket.io available transports': function () {
+ (io.transports.length > 0).should().be_true;
+ }
+
+ };
+
+})(
+ 'undefined' == typeof module ? module = {} : module
+ , 'undefined' == typeof io ? require('socket.io-client') : io
+ , 'undefined' == typeof should ? require('should') : should
+);
View
351 test/parser.test.js
@@ -0,0 +1,351 @@
+
+/*!
+ * socket.io-node
+ * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
+ * MIT Licensed
+ */
+
+(function (module, io, should) {
+
+ var parser = io.parser;
+
+ module.exports = {
+
+ 'decoding error packet': function () {
+ parser.decodePacket('7:::').should().eql({
+ type: 'error'
+ , reason: ''
+ , advice: ''
+ , endpoint: ''
+ });
+ },
+
+ 'decoding error packet with reason': function () {
+ parser.decodePacket('7:::0').should().eql({
+ type: 'error'
+ , reason: 'transport not supported'
+ , advice: ''
+ , endpoint: ''
+ });
+ },
+
+ 'decoding error packet with reason and advice': function () {
+ parser.decodePacket('7:::2+0').should().eql({
+ type: 'error'
+ , reason: 'unauthorized'
+ , advice: 'reconnect'
+ , endpoint: ''
+ });
+ },
+
+ 'decoding error packet with endpoint': function () {
+ parser.decodePacket('7::/woot').should().eql({
+ type: 'error'
+ , reason: ''
+ , advice: ''
+ , endpoint: '/woot'
+ });
+ },
+
+ 'decoding ack packet': function () {
+ parser.decodePacket('6:::140').should().eql({
+ type: 'ack'
+ , ackId: '140'
+ , endpoint: ''
+ , args: []
+ });
+ },
+
+ 'decoding ack packet with args': function () {
+ parser.decodePacket('6:::12+' + JSON.stringify(['woot', 'wa'])).should().eql({
+ type: 'ack'
+ , ackId: '12'
+ , endpoint: ''
+ , args: ['woot', 'wa']
+ });
+ },
+
+ 'decoding ack packet with bad json': function () {
+ var thrown = false;
+
+ try {
+ parser.decodePacket('6:::1+{"++]').should().eql({
+ type: 'ack'
+ , ackId: '1'
+ , endpoint: ''
+ , args: []
+ });
+ } catch (e) {
+ thrown = true;
+ }
+
+ thrown.should().be_false;
+ },
+
+ 'decoding json packet': function () {
+ parser.decodePacket('4:::"2"').should().eql({
+ type: 'json'
+ , endpoint: ''
+ , data: '2'
+ });
+ },
+
+ 'decoding json packet with message id and ack data': function () {
+ parser.decodePacket('4:1+::{"a":"b"}').should().eql({
+ type: 'json'
+ , id: 1
+ , ack: 'data'
+ , endpoint: ''
+ , data: { a: 'b' }
+ });
+ },
+
+ 'decoding an event packet': function () {
+ parser.decodePacket('5:::woot').should().eql({
+ type: 'event'
+ , name: 'woot'
+ , endpoint: ''
+ , args: []
+ });
+ },
+
+ 'decoding an event packet with message id and ack': function () {
+ parser.decodePacket('5:1+::tobi').should().eql({
+ type: 'event'
+ , id: 1
+ , ack: 'data'
+ , endpoint: ''
+ , name: 'tobi'
+ , args: []
+ });
+ },
+
+ 'decoding an event packet with data': function () {
+ parser.decodePacket('5:::edwald\ufffd[{"a": "b"},2,"3"]').should().eql({
+ type: 'event'
+ , name: 'edwald'
+ , endpoint: ''
+ , args: [{a: 'b'}, 2, '3']
+ });
+ },
+
+ 'decoding a message packet': function () {
+ parser.decodePacket('3:::woot').should().eql({
+ type: 'message'
+ , endpoint: ''
+ , data: 'woot'
+ });
+ },
+
+ 'decoding a message packet with id and endpoint': function () {
+ parser.decodePacket('3:5:/tobi').should().eql({
+ type: 'message'
+ , id: 5
+ , ack: true
+ , endpoint: '/tobi'
+ , data: ''
+ });
+ },
+
+ 'decoding a heartbeat packet': function () {
+ parser.decodePacket('2:::').should().eql({
+ type: 'heartbeat'
+ , endpoint: ''
+ });
+ },
+
+ 'decoding a connection packet': function () {
+ parser.decodePacket('1::/tobi').should().eql({
+ type: 'connect'
+ , endpoint: '/tobi'
+ , qs: ''
+ });
+ },
+
+ 'decoding a connection packet with query string': function () {
+ parser.decodePacket('1::/test:?test=1').should().eql({
+ type: 'connect'
+ , endpoint: '/test'
+ , qs: '?test=1'
+ });
+ },
+
+ 'decoding a disconnection packet': function () {
+ parser.decodePacket('0::/woot').should().eql({
+ type: 'disconnect'
+ , endpoint: '/woot'
+ });
+ },
+
+ 'encoding error packet': function () {
+ parser.encodePacket({
+ type: 'error'
+ , reason: ''
+ , advice: ''
+ , endpoint: ''
+ }).should().eql('7::');
+ },
+
+ 'encoding error packet with reason': function () {
+ parser.encodePacket({
+ type: 'error'
+ , reason: 'transport not supported'
+ , advice: ''
+ , endpoint: ''
+ }).should().eql('7:::0');
+ },
+
+ 'encoding error packet with reason and advice': function () {
+ parser.encodePacket({
+ type: 'error'
+ , reason: 'unauthorized'
+ , advice: 'reconnect'
+ , endpoint: ''
+ }).should().eql('7:::2+0');
+ },
+
+ 'encoding error packet with endpoint': function () {
+ parser.encodePacket({
+ type: 'error'
+ , reason: ''
+ , advice: ''
+ , endpoint: '/woot'
+ }).should().eql('7::/woot');
+ },
+
+ 'encoding ack packet': function () {
+ parser.encodePacket({
+ type: 'ack'
+ , ackId: '140'
+ , endpoint: ''
+ , args: []
+ }).should().eql('6:::140');
+ },
+
+ 'encoding ack packet with args': function () {
+ parser.encodePacket({
+ type: 'ack'
+ , ackId: '12'
+ , endpoint: ''
+ , args: ['woot', 'wa']
+ }).should().eql('6:::12+' + JSON.stringify(['woot', 'wa']));
+ },
+
+ 'encoding json packet': function () {
+ parser.encodePacket({
+ type: 'json'
+ , endpoint: ''
+ , data: '2'
+ }).should().eql('4:::"2"');
+ },
+
+ 'encoding json packet with message id and ack data': function () {
+ parser.encodePacket({
+ type: 'json'
+ , id: 1
+ , ack: 'data'
+ , endpoint: ''
+ , data: { a: 'b' }
+ }).should().eql('4:1+::{"a":"b"}');
+ },
+
+ 'encoding an event packet': function () {
+ parser.encodePacket({
+ type: 'event'
+ , name: 'woot'
+ , endpoint: ''
+ , args: []
+ }).should().eql('5:::woot');
+ },
+
+ 'encoding an event packet with message id and ack': function () {
+ parser.encodePacket({
+ type: 'event'
+ , id: 1
+ , ack: 'data'
+ , endpoint: ''
+ , name: 'tobi'
+ , args: []
+ }).should().eql('5:1+::tobi');
+ },
+
+ 'encoding an event packet with data': function () {
+ parser.encodePacket({
+ type: 'event'
+ , name: 'edwald'
+ , endpoint: ''
+ , args: [{a: 'b'}, 2, '3']
+ }).should().eql('5:::edwald\ufffd[{"a":"b"},2,"3"]');
+ },
+
+ 'encoding a message packet': function () {
+ parser.encodePacket({
+ type: 'message'
+ , endpoint: ''
+ , data: 'woot'
+ }).should().eql('3:::woot');
+ },
+
+ 'encoding a message packet with id and endpoint': function () {
+ parser.encodePacket({
+ type: 'message'
+ , id: 5
+ , ack: true
+ , endpoint: '/tobi'
+ , data: ''
+ }).should().eql('3:5:/tobi');
+ },
+
+ 'encoding a heartbeat packet': function () {
+ parser.encodePacket({
+ type: 'heartbeat'
+ , endpoint: ''
+ }).should().eql('2::');
+ },
+
+ 'encoding a connection packet': function () {
+ parser.encodePacket({
+ type: 'connect'
+ , endpoint: '/tobi'
+ , qs: ''
+ }).should().eql('1::/tobi');
+ },
+
+ 'encoding a connection packet with query string': function () {
+ parser.encodePacket({
+ type: 'connect'
+ , endpoint: '/test'
+ , qs: '?test=1'
+ }).should().eql('1::/test:?test=1');
+ },
+
+ 'encoding a disconnection packet': function () {
+ parser.encodePacket({
+ type: 'disconnect'
+ , endpoint: '/woot'
+ }).should().eql('0::/woot');
+ },
+
+ 'test decoding a payload': function () {
+ parser.decodePayload('\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d'
+ + '\ufffd3\ufffd0::').should().eql([
+ { type: 'message', data: '5', endpoint: '' }
+ , { type: 'message', data: '53d', endpoint: '' }
+ , { type: 'disconnect', endpoint: '' }
+ ]);
+ },
+
+ 'test encoding a payload': function () {
+ parser.encodePayload([
+ parser.encodePacket({ type: 'message', data: '5', endpoint: '' })
+ , parser.encodePacket({ type: 'message', data: '53d', endpoint: '' })
+ ]).should().eql('\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d')
+ }
+
+ };
+
+})(
+ 'undefined' == typeof module ? module = {} : module
+ , 'undefined' == typeof io ? require('socket.io-client') : io
+ , 'undefined' == typeof should ? require('should') : should
+);
View
136 test/util.test.js
@@ -0,0 +1,136 @@
+
+/*!
+ * socket.io-node
+ * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
+ * MIT Licensed
+ */
+
+(function (module, io, should) {
+
+ module.exports = {
+
+ 'parse uri': function () {
+ var http = io.util.parseUri('http://google.com')
+ , https = io.util.parseUri('https://www.google.com:80')
+ , query = io.util.parseUri('google.com:8080/foo/bar?foo=bar');
+
+ http.protocol.should().eql('http');
+ http.port.should().eql('');
+ http.host.should().eql('google.com');
+ https.protocol.should().eql('https');
+ https.port.should().eql('80');
+ https.host.should().eql('www.google.com');
+ query.port.should().eql('8080');
+ query.query.should().eql('foo=bar');
+ query.path.should().eql('/foo/bar');
+ query.relative.should().eql('/foo/bar?foo=bar');
+ },
+
+ 'unique uri': function () {
+ var protocol = io.util.parseUri('http://google.com')
+ , noprotocol = io.util.parseUri('google.com')
+ , https = io.util.parseUri('https://google.com')
+ , path = io.util.parseUri('https://google.com/google.com/com/?foo=bar');
+
+ io.util.uniqueUri(protocol).should().eql('http://google.com:80');
+ io.util.uniqueUri(noprotocol).should().eql('http://google.com:80');
+ io.util.uniqueUri(https).should().eql('https://google.com:443');
+ io.util.uniqueUri(path).should().eql('https://google.com:443');
+ },
+
+ 'request': function () {
+ if ('undefined' == typeof window) {
+ should.equal(io.util.request(), null);
+ } else {
+ io.util.request().should().be.a('object');
+ }
+ },
+
+ 'is array': function () {
+ io.util.isArray([]).should().be_true;
+ io.util.isArray({}).should().be_false;
+ io.util.isArray('str').should().be_false;
+ io.util.isArray(new Date).should().be_false;
+ io.util.isArray(true).should().be_false;
+ io.util.isArray(arguments).should().be_false;
+ },
+
+ 'merge, deep merge': function () {
+ var start = {
+ foo: 'bar'
+ , bar: 'baz'
+ }
+ , duplicate = {
+ foo: 'foo'
+ , bar: 'bar'
+ }
+ , extra = {
+ ping: 'pong'
+ }
+ , deep = {
+ level1:{
+ foo: 'bar'
+ , level2: {
+ foo: 'bar'
+ , level3:{
+ foo: 'bar'
+ , rescursive: deep
+ }
+ }
+ }
+ }
+ // same structure, but changed names
+ , deeper = {
+ foo: 'bar'
+ , level1:{
+ foo: 'baz'
+ , level2: {
+ foo: 'foo'
+ , level3:{
+ foo: 'pewpew'
+ , rescursive: deep
+ }
+ }
+ }
+ };
+
+ io.util.merge(start, duplicate);
+
+ start.foo.should().eql('foo');
+ start.bar.should().eql('bar');
+
+ io.util.merge(start, extra);
+ start.ping.should().eql('pong');
+ start.foo.should().eql('foo');
+
+ io.util.merge(deep, deeper);
+
+ deep.foo.should().eql('bar');
+ deep.level1.foo.should().eql('baz');
+ deep.level1.level2.foo.should().eql('foo');
+ deep.level1.level2.level3.foo.should().eql('pewpew');
+ },
+
+ 'defer': function (next) {
+ var now = +new Date;
+
+ io.util.defer(function () {
+ ((new Date - now) >= ( io.util.webkit ? 100 : 0 )).should().be_true();
+ next();
+ })
+ },
+
+ 'indexOf': function () {
+ var data = ['socket', 2, 3, 4, 'socket', 5, 6, 7, 'io'];
+ io.util.indexOf(data, 'socket', 1).should().eql(4);
+ io.util.indexOf(data, 'socket').should().eql(0);
+ io.util.indexOf(data, 'waffles').should().eql(-1);
+ }
+
+ };
+
+})(
+ 'undefined' == typeof module ? module = {} : module
+ , 'undefined' == typeof io ? require('socket.io-client') : io
+ , 'undefined' == typeof should ? require('should') : should
+);
Please sign in to comment.
Something went wrong with that request. Please try again.