[Pkg-javascript-commits] [sockjs-client] 133/350: Fix Error and Event inheritance. Fix tests
tonnerre at ancient-solutions.com
tonnerre at ancient-solutions.com
Fri Aug 5 01:03:55 UTC 2016
This is an automated email from the git hooks/post-receive script.
tonnerre-guest pushed a commit to branch upstream
in repository sockjs-client.
commit 4e0e82785c967f3dfb98ef13d8dbaa6051d8ced6
Author: Bryce Kahle <bkahle at gmail.com>
Date: Mon Oct 13 16:32:52 2014 -0400
Fix Error and Event inheritance. Fix tests
---
.zuul.yml | 4 +-
lib/error/invalidaccesserror.js | 7 +-
lib/error/invalidstateerror.js | 7 +-
lib/error/securityerror.js | 7 +-
lib/iframe-bootstrap.js | 4 +-
lib/main.js | 18 +-
lib/polyfills/event.js | 13 +-
lib/polyfills/eventtarget.js | 6 +-
lib/transport/iframe.js | 4 +-
lib/utils/browser-crypto.js | 4 +-
lib/utils/event.js | 10 +-
package.json | 3 +-
tests/browser.js | 8 +
tests/html/lib/alltests.js | 1 -
tests/html/lib/unittests.js | 369 -----------------------------------
tests/lib/main-node.js | 27 +++
tests/{ => lib}/main.js | 48 ++---
tests/{ => lib}/receivers.js | 8 +-
tests/{ => lib}/transports.js | 2 +-
tests/lib/utils.js | 134 +++++++++++++
tests/node.js | 6 +
tests/{ => support}/config.js | 0
tests/{ => support}/server.js | 0
tests/{ => support}/sockjs_app.js | 7 +-
tests/{ => support}/sockjs_server.js | 25 +--
25 files changed, 263 insertions(+), 459 deletions(-)
diff --git a/.zuul.yml b/.zuul.yml
index 4f6f7af..65eebe1 100644
--- a/.zuul.yml
+++ b/.zuul.yml
@@ -1,10 +1,10 @@
-ui: mocha-qunit
+ui: mocha-bdd
scripts:
- "static/jquery.min.js"
- "config.js"
- "domain.js"
- "lib/sockjs.js"
-server: ./tests/sockjs_server.js
+server: ./tests/support/sockjs_server.js
browsers:
- name: chrome
version: latest
diff --git a/lib/error/invalidaccesserror.js b/lib/error/invalidaccesserror.js
index 56cd232..892f58d 100644
--- a/lib/error/invalidaccesserror.js
+++ b/lib/error/invalidaccesserror.js
@@ -1,10 +1,9 @@
'use strict';
-var util = require('util');
-
function InvalidAccessError() {
- Error.call(this);
}
-util.inherits(InvalidAccessError, Error);
+
+InvalidAccessError.prototype = new Error();
+InvalidAccessError.prototype.constructor = InvalidAccessError;
module.exports = InvalidAccessError;
diff --git a/lib/error/invalidstateerror.js b/lib/error/invalidstateerror.js
index daead51..3455b29 100644
--- a/lib/error/invalidstateerror.js
+++ b/lib/error/invalidstateerror.js
@@ -1,10 +1,9 @@
'use strict';
-var util = require('util');
-
function InvalidStateError() {
- Error.call(this);
}
-util.inherits(InvalidStateError, Error);
+
+InvalidStateError.prototype = new Error();
+InvalidStateError.prototype.constructor = InvalidStateError;
module.exports = InvalidStateError;
diff --git a/lib/error/securityerror.js b/lib/error/securityerror.js
index 44561c8..b031b40 100644
--- a/lib/error/securityerror.js
+++ b/lib/error/securityerror.js
@@ -1,10 +1,9 @@
'use strict';
-var util = require('util');
-
function SecurityError() {
- Error.call(this);
}
-util.inherits(SecurityError, Error);
+
+SecurityError.prototype = new Error();
+SecurityError.prototype.constructor = SecurityError;
module.exports = SecurityError;
diff --git a/lib/iframe-bootstrap.js b/lib/iframe-bootstrap.js
index 9bd0642..a53483f 100644
--- a/lib/iframe-bootstrap.js
+++ b/lib/iframe-bootstrap.js
@@ -4,7 +4,7 @@ var originUtils = require('./utils/origin')
, eventUtils = require('./utils/event')
, JSON3 = require('json3')
, FacadeJS = require('./facade')
- , InfoIframeReceiver = require('./info-receiver-iframe')
+ , InfoIframeReceiver = require('./info-iframe-receiver')
, iframeUtils = require('./utils/iframe')
, loc = require('./polyfills/location')
;
@@ -73,7 +73,7 @@ module.exports = function (SockJS, facadeTransports) {
}
};
- eventUtils.attachMessage(onMessage);
+ eventUtils.attachEvent('message', onMessage);
// Start
iframeUtils.postMessage('s');
diff --git a/lib/main.js b/lib/main.js
index 55566b8..4e10189 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -205,19 +205,19 @@ SockJS.prototype._transportMessage = function(msg) {
case 'a':
payload = JSON3.parse(msg.slice(1) || '[]');
payload.forEach(function (p) {
- self._dispatchMessage(new TransportMessageEvent(p));
+ self.dispatchEvent(new TransportMessageEvent(p));
});
break;
case 'm':
payload = JSON3.parse(msg.slice(1) || 'null');
- this._dispatchMessage(new TransportMessageEvent(payload));
+ this.dispatchEvent(new TransportMessageEvent(payload));
break;
case 'c':
payload = JSON3.parse(msg.slice(1) || '[]');
this._close(payload[0], payload[1]);
break;
case 'h':
- this.dispatchEvent(new Event().initEvent('heartbeat'));
+ this.dispatchEvent(new Event('heartbeat'));
break;
}
};
@@ -244,7 +244,7 @@ SockJS.prototype._open = function() {
}
this.readyState = SockJS.OPEN;
this.transport = this._transport.transportName;
- this.dispatchEvent(new Event().initEvent('open'));
+ this.dispatchEvent(new Event('open'));
} else {
// The server might have been restarted, and lost track of our
// connection.
@@ -252,12 +252,6 @@ SockJS.prototype._open = function() {
}
};
-SockJS.prototype._dispatchMessage = function(data) {
- var e = new Event().initEvent('message');
- e.data = data;
- this.dispatchEvent(e);
-};
-
SockJS.prototype._close = function(code, reason, wasClean) {
var forceFail = false;
@@ -281,10 +275,10 @@ SockJS.prototype._close = function(code, reason, wasClean) {
this.readyState = SockJS.CLOSED;
if (forceFail) {
- this.dispatchEvent(new Event().initEvent('error'));
+ this.dispatchEvent(new Event('error'));
}
- var e = new CloseEvent();
+ var e = new CloseEvent('close');
e.wasClean = wasClean;
e.code = code;
e.reason = reason;
diff --git a/lib/polyfills/event.js b/lib/polyfills/event.js
index d2d9844..d235f73 100644
--- a/lib/polyfills/event.js
+++ b/lib/polyfills/event.js
@@ -1,6 +1,8 @@
'use strict';
-function Event() {}
+function Event(eventType) {
+ this.type = eventType;
+}
Event.prototype.initEvent = function(eventType, canBubble, cancelable) {
this.type = eventType;
@@ -10,4 +12,11 @@ Event.prototype.initEvent = function(eventType, canBubble, cancelable) {
return this;
};
-module.exports = global.Event || Event;
+Event.prototype.stopPropagation = function() {};
+Event.prototype.preventDefault = function() {};
+
+Event.CAPTURING_PHASE = 1;
+Event.AT_TARGET = 2;
+Event.BUBBLING_PHASE = 3;
+
+module.exports = Event;
diff --git a/lib/polyfills/eventtarget.js b/lib/polyfills/eventtarget.js
index 8f955cc..bcecc0f 100644
--- a/lib/polyfills/eventtarget.js
+++ b/lib/polyfills/eventtarget.js
@@ -4,9 +4,9 @@
* http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget
*/
-var EventTarget = function() {
+function EventTarget() {
this._listeners = {};
-};
+}
EventTarget.prototype.addEventListener = function (eventType, listener) {
if (!(eventType in this._listeners)) {
@@ -58,4 +58,4 @@ EventTarget.prototype.dispatchEvent = function (event) {
}
};
-module.exports = global.EventTarget || EventTarget;
+module.exports = EventTarget;
diff --git a/lib/transport/iframe.js b/lib/transport/iframe.js
index c53ae9e..a0964a9 100644
--- a/lib/transport/iframe.js
+++ b/lib/transport/iframe.js
@@ -36,14 +36,14 @@ function IframeTransport(transport, transUrl, baseUrl) {
});
this.onmessageCallback = this._message.bind(this);
- eventUtils.attachMessage(this.onmessageCallback);
+ eventUtils.attachEvent('message', this.onmessageCallback);
}
util.inherits(IframeTransport, EventEmitter);
IframeTransport.prototype.close = function() {
if (this.iframeObj) {
- eventUtils.detachMessage(this.onmessageCallback);
+ eventUtils.detachEvent('message', this.onmessageCallback);
try {
// When the iframe is not loaded, IE raises an exception
// on 'contentWindow'.
diff --git a/lib/utils/browser-crypto.js b/lib/utils/browser-crypto.js
index 32e1217..a3fc797 100644
--- a/lib/utils/browser-crypto.js
+++ b/lib/utils/browser-crypto.js
@@ -1,13 +1,13 @@
'use strict';
if (global.crypto && global.crypto.getRandomValues) {
- module.exports = function (length) {
+ module.exports.randomBytes = function (length) {
var bytes = new Uint8Array(length);
global.crypto.getRandomValues(bytes);
return bytes;
};
} else {
- module.exports = function (length) {
+ module.exports.randomBytes = function (length) {
var bytes = new Array(length);
for (var i = 0; i < length; i++) {
bytes[i] = Math.floor(Math.random() * 256);
diff --git a/lib/utils/event.js b/lib/utils/event.js
index aa54af4..6c3b93a 100644
--- a/lib/utils/event.js
+++ b/lib/utils/event.js
@@ -7,11 +7,7 @@ var onUnload = {}
;
module.exports = {
- attachMessage: function(listener) {
- this.attachEvent('message', listener);
- }
-
-, attachEvent: function(event, listener) {
+ attachEvent: function(event, listener) {
if (typeof global.addEventListener !== 'undefined') {
global.addEventListener(event, listener, false);
} else if (global.document && global.attachEvent) {
@@ -24,10 +20,6 @@ module.exports = {
}
}
-, detachMessage: function(listener) {
- this.detachEvent('message', listener);
- }
-
, detachEvent: function(event, listener) {
if (typeof global.addEventListener !== 'undefined') {
global.removeEventListener(event, listener, false);
diff --git a/package.json b/package.json
index fcc2bf6..86da61f 100644
--- a/package.json
+++ b/package.json
@@ -54,7 +54,8 @@
"url": "https://github.com/sockjs/sockjs-client.git"
},
"scripts": {
- "t": "node ./node_modules/mocha/bin/mocha tests/main.js tests/transports.js tests/receivers.js",
+ "t": "node ./node_modules/mocha/bin/mocha tests/node.js",
+ "b": "./node_modules/zuul/bin/zuul --local 9090 -- tests/browser.js",
"test": "./node_modules/zuul/bin/zuul --sauce-connect -- tests/html/lib/unittests.js tests/html/lib/domtests.js tests/html/lib/endtoendtests.js tests/html/lib/tests.js",
"test_ws": "./node_modules/zuul/bin/zuul --sauce-connect -- tests/html/lib/ws_test.js",
"test_local": "./node_modules/zuul/bin/zuul --local 9090 -- tests/html/lib/unittests.js tests/html/lib/domtests.js tests/html/lib/endtoendtests.js tests/html/lib/tests.js"
diff --git a/tests/browser.js b/tests/browser.js
new file mode 100644
index 0000000..4cbaeb8
--- /dev/null
+++ b/tests/browser.js
@@ -0,0 +1,8 @@
+'use strict';
+
+// prevent global leak warnings on this
+global._jp = {};
+
+require('./lib/main.js');
+require('./lib/utils.js');
+require('./lib/receivers.js');
diff --git a/tests/html/lib/alltests.js b/tests/html/lib/alltests.js
index 9c9c429..20042fb 100644
--- a/tests/html/lib/alltests.js
+++ b/tests/html/lib/alltests.js
@@ -1,5 +1,4 @@
'use strict';
-require('./unittests');
require('./domtests');
require('./endtoendtests');
require('./tests');
\ No newline at end of file
diff --git a/tests/html/lib/unittests.js b/tests/html/lib/unittests.js
deleted file mode 100644
index 1954d03..0000000
--- a/tests/html/lib/unittests.js
+++ /dev/null
@@ -1,369 +0,0 @@
-'use strict';
-/* global test, SockJS */
-
-var assert = require('assert');
-var u = require('../../../lib/utils');
-var protocols = require('../../../lib/protocols');
-
-test('randomString', function() {
- var i, _i, _len, _ref;
- assert.notEqual(u.randomString(8), u.randomString(8));
- _ref = [1, 2, 3, 128];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- i = _ref[_i];
- assert.equal(u.randomString(i).length, i);
- }
-});
-
-test('randomNumberString', function() {
- for (var i = 0; i <= 10; i++) {
- assert.equal(u.randomNumberString(10).length, 1);
- assert.equal(u.randomNumberString(100).length, 2);
- assert.equal(u.randomNumberString(1000).length, 3);
- assert.equal(u.randomNumberString(10000).length, 4);
- assert.equal(u.randomNumberString(100000).length, 5);
- }
-});
-
-test('getOrigin', function() {
- assert.equal(u.getOrigin('http://a.b/'), 'http://a.b:80');
- assert.equal(u.getOrigin('http://a.b/c'), 'http://a.b:80');
- assert.equal(u.getOrigin('http://a.b:123/c'), 'http://a.b:123');
- assert.equal(u.getOrigin('https://a.b/'), 'https://a.b:443');
- assert.equal(u.getOrigin('file://a.b/'), null);
-});
-
-test('isSameOriginUrl', function() {
- assert.ok(u.isSameOriginUrl('http://localhost', 'http://localhost/'));
- assert.ok(u.isSameOriginUrl('http://localhost', 'http://localhost/abc'));
- assert.ok(u.isSameOriginUrl('http://localhost/', 'http://localhost'));
- assert.ok(u.isSameOriginUrl('http://localhost', 'http://localhost'));
- assert.ok(u.isSameOriginUrl('http://localhost', 'http://localhost:8080') === false);
- assert.ok(u.isSameOriginUrl('http://localhost:8080', 'http://localhost') === false);
- assert.ok(u.isSameOriginUrl('http://localhost:8080', 'http://localhost:8080/'));
- assert.ok(u.isSameOriginUrl('http://127.0.0.1:80/', 'http://127.0.0.1:80/a'));
- assert.ok(u.isSameOriginUrl('http://127.0.0.1:80', 'http://127.0.0.1:80/a'));
- assert.ok(u.isSameOriginUrl('http://localhost', 'http://localhost:80'));
- assert.ok(u.isSameOriginUrl('http://127.0.0.1/', 'http://127.0.0.1:80/a'));
- assert.ok(u.isSameOriginUrl('http://127.0.0.1:9', 'http://127.0.0.1:9999') === false);
- assert.ok(u.isSameOriginUrl('http://127.0.0.1:99', 'http://127.0.0.1:9999') === false);
- assert.ok(u.isSameOriginUrl('http://127.0.0.1:999', 'http://127.0.0.1:9999') === false);
- assert.ok(u.isSameOriginUrl('http://127.0.0.1:9999', 'http://127.0.0.1:9999'));
- assert.ok(u.isSameOriginUrl('http://127.0.0.1:99999', 'http://127.0.0.1:9999') === false);
-});
-
-test("getParentDomain", function() {
- var domains, k;
- domains = {
- 'localhost': 'localhost',
- '127.0.0.1': '127.0.0.1',
- 'a.b.c.d': 'b.c.d',
- 'a.b.c.d.e': 'b.c.d.e',
- '[::1]': '[::1]',
- 'a.org': 'org',
- 'a2.a3.org': 'a3.org'
- };
- for (k in domains) {
- assert.equal(u.getParentDomain(k), domains[k]);
- }
-});
-
-test('objectExtend', function() {
- var a, b;
- assert.deepEqual(u.objectExtend({}, {}), {});
- a = {
- a: 1
- };
- assert.equal(u.objectExtend(a, {}), a);
- assert.equal(u.objectExtend(a, {
- b: 1
- }), a);
- a = {
- a: 1
- };
- b = {
- b: 2
- };
- assert.deepEqual(u.objectExtend(a, b), {
- a: 1,
- b: 2
- });
- assert.deepEqual(a, {
- a: 1,
- b: 2
- });
- assert.deepEqual(b, {
- b: 2
- });
-});
-
-test('bind', function() {
- var bound_fun, fun, o;
- o = {};
- fun = function() {
- return this;
- };
- assert.deepEqual(fun(), undefined);
- bound_fun = u.bind(fun, o);
- assert.deepEqual(bound_fun(), o);
-});
-
-test('amendUrl', function() {
- var dl, t;
- dl = document.location;
- assert.equal(u.amendUrl('//blah:1/abc'), dl.protocol + '//blah:1/abc');
- assert.equal(u.amendUrl('/abc'), dl.protocol + '//' + dl.host + '/abc');
- assert.equal(u.amendUrl('/'), dl.protocol + '//' + dl.host);
- assert.equal(u.amendUrl('http://a:1/abc'), 'http://a:1/abc');
- assert.equal(u.amendUrl('http://a:1/abc/'), 'http://a:1/abc');
- assert.equal(u.amendUrl('http://a:1/abc//'), 'http://a:1/abc');
- t = function() {
- return u.amendUrl('');
- };
- assert.throws(t, 'Wrong url');
- t = function() {
- return u.amendUrl(false);
- };
- assert.throws(t, 'Wrong url');
- t = function() {
- return u.amendUrl('http://abc?a=a');
- };
- assert.throws(t, 'Only basic urls are supported');
- t = function() {
- return u.amendUrl('http://abc#a');
- };
- assert.throws(t, 'Only basic urls are supported');
- assert.equal(u.amendUrl('http://a:80/abc'), 'http://a/abc');
- assert.equal(u.amendUrl('https://a:443/abc'), 'https://a/abc');
- assert.equal(u.amendUrl('https://a:80/abc'), 'https://a:80/abc');
- assert.equal(u.amendUrl('http://a:443/abc'), 'http://a:443/abc');
-});
-
-test('arrIndexOf', function() {
- var a;
- a = [1, 2, 3, 4, 5];
- assert.equal(u.arrIndexOf(a, 1), 0);
- assert.equal(u.arrIndexOf(a, 5), 4);
- assert.equal(u.arrIndexOf(a, null), -1);
- assert.equal(u.arrIndexOf(a, 6), -1);
-});
-
-test('arrSkip', function() {
- var a;
- a = [1, 2, 3, 4, 5];
- assert.deepEqual(u.arrSkip(a, 1), [2, 3, 4, 5]);
- assert.deepEqual(u.arrSkip(a, 2), [1, 3, 4, 5]);
- assert.deepEqual(u.arrSkip(a, 11), [1, 2, 3, 4, 5]);
- assert.deepEqual(u.arrSkip(a, 'a'), [1, 2, 3, 4, 5]);
- assert.deepEqual(u.arrSkip(a, '1'), [1, 2, 3, 4, 5]);
-});
-
-test('quote', function() {
- var all_chars, c, i;
- assert.equal(u.quote(''), '""');
- assert.equal(u.quote('a'), '"a"');
- assert.ok(u.arrIndexOf(['"\\t"', '"\\u0009"'], u.quote('\t')) !== -1);
- assert.ok(u.arrIndexOf(['"\\n"', '"\\u000a"'], u.quote('\n')) !== -1);
- assert.equal(u.quote('\x00\udfff\ufffe\uffff'), '"\\u0000\\udfff\\ufffe\\uffff"');
- assert.equal(u.quote('\ud85c\udff7\ud800\ud8ff'), '"\\ud85c\\udff7\\ud800\\ud8ff"');
- assert.equal(u.quote('\u2000\u2001\u0300\u0301'), '"\\u2000\\u2001\\u0300\\u0301"');
- c = (function() {
- var _results;
- _results = [];
- for (i = 0; i <= 65535; i++) {
- _results.push(String.fromCharCode(i));
- }
- return _results;
- })();
- all_chars = c.join('');
- assert.ok(JSON.parse(u.quote(all_chars)) === all_chars, "Quote/unquote all 64K chars.");
-});
-
-// test('detectProtocols', function() {
-// var chrome_probed, ie10_probed, ie6_probed, ie8_probed, opera_probed;
-// chrome_probed = {
-// 'websocket': true,
-// 'xdr-streaming': false,
-// 'xhr-streaming': true,
-// 'iframe-eventsource': true,
-// 'iframe-htmlfile': true,
-// 'xdr-polling': false,
-// 'xhr-polling': true,
-// 'iframe-xhr-polling': true,
-// 'jsonp-polling': true
-// };
-// assert.deepEqual(u.detectProtocols(chrome_probed, null, {}), ['websocket', 'xhr-streaming', 'xhr-polling']);
-// assert.deepEqual(u.detectProtocols(chrome_probed, null, {
-// websocket: false
-// }), ['xhr-streaming', 'xhr-polling']);
-// opera_probed = {
-// 'websocket': false,
-// 'xdr-streaming': false,
-// 'xhr-streaming': false,
-// 'iframe-eventsource': true,
-// 'iframe-htmlfile': true,
-// 'xdr-polling': false,
-// 'xhr-polling': false,
-// 'iframe-xhr-polling': true,
-// 'jsonp-polling': true
-// };
-// assert.deepEqual(u.detectProtocols(opera_probed, null, {}), ['iframe-eventsource', 'iframe-xhr-polling']);
-// ie6_probed = {
-// 'websocket': false,
-// 'xdr-streaming': false,
-// 'xhr-streaming': false,
-// 'iframe-eventsource': false,
-// 'iframe-htmlfile': false,
-// 'xdr-polling': false,
-// 'xhr-polling': false,
-// 'iframe-xhr-polling': false,
-// 'jsonp-polling': true
-// };
-// assert.deepEqual(u.detectProtocols(ie6_probed, null, {}), ['jsonp-polling']);
-// ie8_probed = {
-// 'websocket': false,
-// 'xdr-streaming': true,
-// 'xhr-streaming': false,
-// 'iframe-eventsource': false,
-// 'iframe-htmlfile': true,
-// 'xdr-polling': true,
-// 'xhr-polling': false,
-// 'iframe-xhr-polling': true,
-// 'jsonp-polling': true
-// };
-// assert.deepEqual(u.detectProtocols(ie8_probed, null, {}), ['xdr-streaming', 'xdr-polling']);
-// assert.deepEqual(u.detectProtocols(ie8_probed, null, {
-// cookie_needed: true
-// }), ['iframe-htmlfile', 'iframe-xhr-polling']);
-// ie10_probed = {
-// 'websocket': true,
-// 'xdr-streaming': true,
-// 'xhr-streaming': true,
-// 'iframe-eventsource': false,
-// 'iframe-htmlfile': true,
-// 'xdr-polling': true,
-// 'xhr-polling': true,
-// 'iframe-xhr-polling': true,
-// 'jsonp-polling': true
-// };
-// assert.deepEqual(u.detectProtocols(ie10_probed, null, {}), ['websocket', 'xhr-streaming', 'xhr-polling']);
-// assert.deepEqual(u.detectProtocols(ie10_probed, null, {
-// cookie_needed: true
-// }), ['websocket', 'xhr-streaming', 'xhr-polling']);
-// assert.deepEqual(u.detectProtocols(chrome_probed, null, {
-// null_origin: true
-// }), ['websocket', 'iframe-eventsource', 'iframe-xhr-polling']);
-// assert.deepEqual(u.detectProtocols(chrome_probed, null, {
-// websocket: false,
-// null_origin: true
-// }), ['iframe-eventsource', 'iframe-xhr-polling']);
-// assert.deepEqual(u.detectProtocols(opera_probed, null, {
-// null_origin: true
-// }), ['iframe-eventsource', 'iframe-xhr-polling']);
-// assert.deepEqual(u.detectProtocols(ie6_probed, null, {
-// null_origin: true
-// }), ['jsonp-polling']);
-// assert.deepEqual(u.detectProtocols(ie8_probed, null, {
-// null_origin: true
-// }), ['iframe-htmlfile', 'iframe-xhr-polling']);
-// assert.deepEqual(u.detectProtocols(ie10_probed, null, {
-// null_origin: true
-// }), ['websocket', 'iframe-htmlfile', 'iframe-xhr-polling']);
-// });
-
-test("EventEmitter", function() {
- var bluff, handler0, handler1, handler2, handler3, handler4, log, r, single;
- r = new SockJS('//1.2.3.4/wrongurl', null, {
- protocols_whitelist: []
- });
- r.addEventListener('message', function() {
- return assert.ok(true);
- });
- r.onmessage = function() {
- return assert.ok(false);
- };
- bluff = function() {
- return assert.ok(false);
- };
- r.addEventListener('message', bluff);
- r.removeEventListener('message', bluff);
- r.addEventListener('message', bluff);
- r.addEventListener('message', function() {
- return assert.ok(true);
- });
- r.onmessage = function() {
- return assert.ok(true);
- };
- r.removeEventListener('message', bluff);
- r.dispatchEvent({
- type: 'message'
- });
- handler0 = function() {
- return log.push(0);
- };
- handler1 = function() {
- log.push(1);
- r.removeEventListener('test', handler0);
- r.removeEventListener('test', handler2);
- r.addEventListener('test', handler3);
- return r.addEventListener('test', handler4);
- };
- handler2 = function() {
- return log.push(2);
- };
- handler3 = function() {
- log.push(3);
- r.removeEventListener('test', handler1);
- r.removeEventListener('test', handler3);
- return r.removeEventListener('test', handler4);
- };
- handler4 = function() {
- return log.push(4);
- };
- r.addEventListener('test', handler0);
- r.addEventListener('test', handler1);
- r.addEventListener('test', handler2);
- log = [];
- r.dispatchEvent({
- type: 'test'
- });
- assert.deepEqual(log, [0, 1, 2]);
- log = [];
- r.dispatchEvent({
- type: 'test'
- });
- assert.deepEqual(log, [1, 3, 4]);
- log = [];
- r.dispatchEvent({
- type: 'test'
- });
- assert.deepEqual(log, []);
- single = function() {
- return assert.ok(true);
- };
- r.addEventListener('close', single);
- r.addEventListener('close', single);
- r.dispatchEvent({
- type: 'close'
- });
- r.removeEventListener('close', single);
- r.dispatchEvent({
- type: 'close'
- });
- r.close();
-});
-
-test("NoConstructor", function() {
- var r;
- r = new SockJS('//1.2.3.4/blah', null, {
- protocols_whitelist: []
- });
- assert.ok(r instanceof SockJS);
- r.close();
- /* jshint newcap: false */
- r = SockJS('//1.2.3.4/blah', null, {
- protocols_whitelist: []
- });
- assert.ok(r instanceof SockJS);
- r.close();
-});
diff --git a/tests/lib/main-node.js b/tests/lib/main-node.js
new file mode 100644
index 0000000..92ea0b7
--- /dev/null
+++ b/tests/lib/main-node.js
@@ -0,0 +1,27 @@
+'use strict';
+
+var expect = require('expect.js')
+ , proxyquire = require('proxyquire')
+ , SecurityError = require('../../lib/error/securityerror')
+ , SockJS = require('../../lib/entry')
+ ;
+
+describe('SockJS', function() {
+ describe('Constructor', function () {
+
+ describe('WebSocket specification step #2', function () {
+ it('should throw SecurityError for an insecure url from a secure page', function () {
+ var main = proxyquire('../../lib/main', { './polyfills/location': {
+ protocol: 'https'
+ }});
+ var sjs = proxyquire('../../lib/entry', { './main': main });
+ expect(function () {
+ sjs('http://sockjs.org');
+ }).to.throwException(function (e) {
+ expect(e).to.be.a(SecurityError);
+ });
+ });
+ });
+
+ });
+});
\ No newline at end of file
diff --git a/tests/main.js b/tests/lib/main.js
similarity index 64%
rename from tests/main.js
rename to tests/lib/main.js
index aa7fec2..bb046ea 100644
--- a/tests/main.js
+++ b/tests/lib/main.js
@@ -1,9 +1,11 @@
+/* eslint new-cap: 0, no-new: 0 */
+/* jshint ignore: start */
'use strict';
var expect = require('expect.js')
- , proxyquire = require('proxyquire')
- , SecurityError = require('../lib/error/securityerror')
- , SockJS = require('../lib/entry')
+ // , proxyquire = require('proxyquire')
+ // , SecurityError = require('../../lib/error/securityerror')
+ , SockJS = require('../../lib/entry')
;
describe('SockJS', function() {
@@ -24,13 +26,13 @@ describe('SockJS', function() {
describe('WebSocket specification step #1', function () {
it('should throw SyntaxError for an invalid url', function () {
expect(function () {
- SockJS('//sockjs.org');
+ new SockJS('//sockjs.org');
}).to.throwException(function (e) {
expect(e).to.be.a(SyntaxError);
});
expect(function () {
- SockJS('http://');
+ new SockJS('http://');
}).to.throwException(function (e) {
expect(e).to.be.a(SyntaxError);
});
@@ -38,13 +40,13 @@ describe('SockJS', function() {
it('should throw SyntaxError when the url contains a querystring or fragment', function () {
expect(function () {
- SockJS('http://sockjs.org/?test');
+ new SockJS('http://sockjs.org/?test');
}).to.throwException(function (e) {
expect(e).to.be.a(SyntaxError);
});
expect(function () {
- SockJS('http://sockjs.org/#test');
+ new SockJS('http://sockjs.org/#test');
}).to.throwException(function (e) {
expect(e).to.be.a(SyntaxError);
});
@@ -52,35 +54,35 @@ describe('SockJS', function() {
it('should throw SyntaxError for an invalid protocol', function () {
expect(function () {
- SockJS('ftp://sockjs.org');
+ new SockJS('ftp://sockjs.org');
}).to.throwException(function (e) {
expect(e).to.be.a(SyntaxError);
});
});
});
- describe('WebSocket specification step #2', function () {
- it('should throw SecurityError for an insecure url from a secure page', function () {
- var main = proxyquire('../lib/main', { './polyfills/location': {
- protocol: 'https'
- }});
- var sjs = proxyquire('../lib/entry', { './main': main });
- expect(function () {
- sjs('http://sockjs.org');
- }).to.throwException(function (e) {
- expect(e).to.be.a(SecurityError);
- });
- });
- });
+ // describe('WebSocket specification step #2', function () {
+ // it('should throw SecurityError for an insecure url from a secure page', function () {
+ // var main = proxyquire('../../lib/main', { './polyfills/location': {
+ // protocol: 'https'
+ // }});
+ // var sjs = proxyquire('../../lib/entry', { './main': main });
+ // expect(function () {
+ // sjs('http://sockjs.org');
+ // }).to.throwException(function (e) {
+ // expect(e).to.be.a(SecurityError);
+ // });
+ // });
+ // });
describe('WebSocket specification step #5', function () {
it('should throw SyntaxError for duplicated protocols', function () {
expect(function () {
- SockJS('http://sockjs.org', ['test', 'test']);
+ new SockJS('http://sockjs.org', ['test', 'test']);
}).to.throwException(function (e) {
expect(e).to.be.a(SyntaxError);
});
});
});
});
-});
\ No newline at end of file
+});
diff --git a/tests/receivers.js b/tests/lib/receivers.js
similarity index 95%
rename from tests/receivers.js
rename to tests/lib/receivers.js
index c8cd611..d6c44e8 100644
--- a/tests/receivers.js
+++ b/tests/lib/receivers.js
@@ -1,10 +1,10 @@
'use strict';
var expect = require('expect.js')
- , JsonpReceiver = require('../lib/transport/receiver/jsonp')
- , XhrReceiver = require('../lib/transport/receiver/xhr')
- , XhrFake = require('../lib/transport/sender/xhr-fake')
- , utils = require('../lib/utils/iframe')
+ , JsonpReceiver = require('../../lib/transport/receiver/jsonp')
+ , XhrReceiver = require('../../lib/transport/receiver/xhr')
+ , XhrFake = require('../../lib/transport/sender/xhr-fake')
+ , utils = require('../../lib/utils/iframe')
;
describe('Receivers', function () {
diff --git a/tests/transports.js b/tests/lib/transports.js
similarity index 94%
rename from tests/transports.js
rename to tests/lib/transports.js
index 9c4ca20..83d3fa7 100644
--- a/tests/transports.js
+++ b/tests/lib/transports.js
@@ -1,7 +1,7 @@
'use strict';
var expect = require('expect.js')
- , transportList = require('../lib/transport-list')
+ , transportList = require('../../lib/transport-list')
;
describe('Transports', function () {
diff --git a/tests/lib/utils.js b/tests/lib/utils.js
new file mode 100644
index 0000000..c8a7e9c
--- /dev/null
+++ b/tests/lib/utils.js
@@ -0,0 +1,134 @@
+'use strict';
+
+var expect = require('expect.js')
+ , JSON3 = require('json3')
+ ;
+
+describe('utils', function () {
+ describe('random', function () {
+ var random = require('../../lib/utils/random');
+ describe('string', function () {
+ it('should generate unique outputs', function () {
+ expect(random.string(8)).not.to.equal(random.string(8));
+ });
+
+ it('should have the correct length', function () {
+ var lengths = [1, 2, 3, 128];
+ lengths.forEach(function (len) {
+ expect(random.string(len).length).to.equal(len);
+ });
+ });
+ });
+
+ describe('numberString', function () {
+ it('should have the correct length based on the max', function () {
+ expect(random.numberString(10).length).to.equal(1);
+ expect(random.numberString(100).length).to.equal(2);
+ expect(random.numberString(1000).length).to.equal(3);
+ expect(random.numberString(10000).length).to.equal(4);
+ expect(random.numberString(100000).length).to.equal(5);
+ });
+ });
+ });
+
+ describe('origin', function () {
+ var origin = require('../../lib/utils/origin');
+ it('getOrigin', function () {
+ expect(origin.getOrigin('http://a.b/')).to.equal('http://a.b:80');
+ expect(origin.getOrigin('http://a.b/c')).to.equal('http://a.b:80');
+ expect(origin.getOrigin('http://a.b:123/c')).to.equal('http://a.b:123');
+ expect(origin.getOrigin('https://a.b/')).to.equal('https://a.b:443');
+ expect(origin.getOrigin('file://a.b/')).to.equal(null);
+ });
+
+ it('isSameOriginUrl', function () {
+ expect(origin.isSameOriginUrl('http://localhost', 'http://localhost/')).to.be.ok();
+ expect(origin.isSameOriginUrl('http://localhost', 'http://localhost/abc')).to.be.ok();
+ expect(origin.isSameOriginUrl('http://localhost/', 'http://localhost')).to.be.ok();
+ expect(origin.isSameOriginUrl('http://localhost', 'http://localhost')).to.be.ok();
+ expect(origin.isSameOriginUrl('http://localhost', 'http://localhost:8080')).to.not.be.ok();
+ expect(origin.isSameOriginUrl('http://localhost:8080', 'http://localhost')).to.not.be.ok();
+ expect(origin.isSameOriginUrl('http://localhost:8080', 'http://localhost:8080/')).to.be.ok();
+ expect(origin.isSameOriginUrl('http://127.0.0.1:80/', 'http://127.0.0.1:80/a')).to.be.ok();
+ expect(origin.isSameOriginUrl('http://127.0.0.1:80', 'http://127.0.0.1:80/a')).to.be.ok();
+ expect(origin.isSameOriginUrl('http://localhost', 'http://localhost:80')).to.be.ok();
+ expect(origin.isSameOriginUrl('http://127.0.0.1/', 'http://127.0.0.1:80/a')).to.be.ok();
+ expect(origin.isSameOriginUrl('http://127.0.0.1:9', 'http://127.0.0.1:9999')).to.not.be.ok();
+ expect(origin.isSameOriginUrl('http://127.0.0.1:99', 'http://127.0.0.1:9999')).to.not.be.ok();
+ expect(origin.isSameOriginUrl('http://127.0.0.1:999', 'http://127.0.0.1:9999')).to.not.be.ok();
+ expect(origin.isSameOriginUrl('http://127.0.0.1:9999', 'http://127.0.0.1:9999')).to.be.ok();
+ expect(origin.isSameOriginUrl('http://127.0.0.1:99999', 'http://127.0.0.1:9999')).to.not.be.ok();
+ });
+
+ it('isSameOriginScheme', function () {
+ expect(origin.isSameOriginScheme('http://localhost', 'http://localhost/')).to.be.ok();
+ expect(origin.isSameOriginScheme('http://localhost', 'https://localhost/')).to.not.be.ok();
+ expect(origin.isSameOriginScheme('http://localhost', 'file://localhost/')).to.not.be.ok();
+ });
+ });
+
+ describe('escape', function () {
+ var escape = require('../../lib/utils/escape');
+ describe('quote', function () {
+ it('handles empty string', function () {
+ expect(escape.quote('')).to.equal('""');
+ });
+
+ it('handles non-empty string', function () {
+ expect(escape.quote('a')).to.equal('"a"');
+ });
+
+ it('handles tab and newline', function () {
+ expect(['"\\t"', '"\\u0009"']).to.contain(escape.quote('\t'));
+ expect(['"\\n"', '"\\u000a"']).to.contain(escape.quote('\n'));
+ });
+
+ it('handles unicode', function () {
+ expect(escape.quote('\x00\udfff\ufffe\uffff')).to.equal('"\\u0000\\udfff\\ufffe\\uffff"');
+ expect(escape.quote('\ud85c\udff7\ud800\ud8ff')).to.equal('"\\ud85c\\udff7\\ud800\\ud8ff"');
+ expect(escape.quote('\u2000\u2001\u0300\u0301')).to.equal('"\\u2000\\u2001\\u0300\\u0301"');
+ });
+
+ it('handles all 64K characters round-trip', function () {
+ var c = [];
+ for (var i = 0; i <= 65536; i++) {
+ c.push(String.fromCharCode(i));
+ }
+ var allChars = c.join('');
+ expect(JSON3.parse(escape.quote(allChars))).to.equal(allChars);
+ });
+ });
+ });
+
+ describe('object', function () {
+ var objectUtils = require('../../lib/utils/object');
+ it('extend', function () {
+ var a, b;
+ expect(objectUtils.extend({}, {})).to.eql({});
+ a = {
+ a: 1
+ };
+ expect(objectUtils.extend(a, {})).to.eql(a);
+ expect(objectUtils.extend(a, {
+ b: 1
+ })).to.eql(a);
+ a = {
+ a: 1
+ };
+ b = {
+ b: 2
+ };
+ expect(objectUtils.extend(a, b)).to.eql({
+ a: 1,
+ b: 2
+ });
+ expect(a).to.eql({
+ a: 1,
+ b: 2
+ });
+ expect(b).to.eql({
+ b: 2
+ });
+ });
+ });
+});
diff --git a/tests/node.js b/tests/node.js
new file mode 100644
index 0000000..3ba7bc3
--- /dev/null
+++ b/tests/node.js
@@ -0,0 +1,6 @@
+'use strict';
+
+require('./lib/main.js');
+require('./lib/main-node.js');
+require('./lib/utils.js');
+require('./lib/receivers.js');
diff --git a/tests/config.js b/tests/support/config.js
similarity index 100%
rename from tests/config.js
rename to tests/support/config.js
diff --git a/tests/server.js b/tests/support/server.js
similarity index 100%
rename from tests/server.js
rename to tests/support/server.js
diff --git a/tests/sockjs_app.js b/tests/support/sockjs_app.js
similarity index 94%
rename from tests/sockjs_app.js
rename to tests/support/sockjs_app.js
index c4af0eb..f46cf81 100644
--- a/tests/sockjs_app.js
+++ b/tests/support/sockjs_app.js
@@ -1,3 +1,4 @@
+/* eslint camelcase: 0 */
'use strict';
var sockjs = require('sockjs');
@@ -11,7 +12,7 @@ exports.install = function(opts, server) {
conn.on('data', function(m) {
var d = JSON.stringify(m);
console.log(' [ ] echo message ' + conn,
- d.slice(0,64)+
+ d.slice(0,64) +
((d.length > 64) ? '...' : ''));
conn.write(m);
});
@@ -20,7 +21,7 @@ exports.install = function(opts, server) {
var sjs_close = sockjs.createServer(opts);
sjs_close.on('connection', function(conn) {
console.log(' [+] clos open ' + conn);
- conn.close(3000, "Go away!");
+ conn.close(3000, 'Go away!');
conn.on('close', function() {
console.log(' [-] clos close ' + conn);
});
@@ -68,7 +69,7 @@ exports.install = function(opts, server) {
var n = Math.floor(Number(m));
n = (n > 0 && n < 19) ? n : 1;
console.log(' [ ] amp message: 2^' + n);
- conn.write(new Array(Math.pow(2, n)+1).join('x'));
+ conn.write(new Array(Math.pow(2, n) + 1).join('x'));
});
});
diff --git a/tests/sockjs_server.js b/tests/support/sockjs_server.js
similarity index 76%
rename from tests/sockjs_server.js
rename to tests/support/sockjs_server.js
index 7348e7e..71b17c0 100644
--- a/tests/sockjs_server.js
+++ b/tests/support/sockjs_server.js
@@ -1,13 +1,16 @@
+/* eslint camelcase: 0 */
'use strict';
+
var http = require('http');
-var node_static = require('node-static');
-var sockjs_app = require('./sockjs_app');
+var nodeStatic = require('node-static');
+var sockjs = require('./sockjs_app');
var url = require('url');
+var path = require('path');
var port = process.env.ZUUL_PORT || 8081;
-var client_opts = {
+var clientOptions = {
// Address of a sockjs test server.
- url: 'http://localhost:'+port,
+ url: 'http://localhost:' + port,
sockjs_opts: {
devel: true,
debug: true,
@@ -16,7 +19,7 @@ var client_opts = {
}
};
-var static_directory = new node_static.Server(__dirname + '/html');
+var staticDir = new nodeStatic.Server(path.join(__dirname, '../html'));
var server = http.createServer();
server.addListener('request', function(req, res) {
@@ -43,28 +46,28 @@ server.addListener('request', function(req, res) {
} else if (req.url === '/config.js') {
if (req.headers.referer) {
var parsedOrigin = url.parse(req.headers.referer);
- client_opts.url = parsedOrigin.protocol + '//' + parsedOrigin.hostname + ':' + port;
+ clientOptions.url = parsedOrigin.protocol + '//' + parsedOrigin.hostname + ':' + port;
}
res.setHeader('content-type', 'application/javascript');
res.writeHead(200);
res.end('var client_opts = ' +
- JSON.stringify(client_opts) + ';');
+ JSON.stringify(clientOptions) + ';');
} else if (req.url === '/domain.js') {
res.setHeader('content-type', 'application/javascript');
res.writeHead(200);
res.end('document.domain = document.domain;');
} else {
- static_directory.serve(req, res);
+ staticDir.serve(req, res);
}
});
server.addListener('upgrade', function(req, res){
res.end();
});
-sockjs_app.install({
+sockjs.install({
sockjs_url: '/lib/sockjs.js',
websocket: true
}, server);
-console.log(" [*] Listening on", port);
-server.listen(port);
\ No newline at end of file
+console.log(' [*] Listening on', port);
+server.listen(port);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git
More information about the Pkg-javascript-commits
mailing list