[Pkg-javascript-commits] [sockjs-client] 192/350: Fix #72 add support for query strings on url

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:04:20 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 4fd79a94b10163ece36199492c9fb3f8ce544088
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Mon Oct 20 13:31:15 2014 -0400

    Fix #72 add support for query strings on url
---
 Makefile                              | 9 ++++++---
 lib/info-receiver.js                  | 5 +++--
 lib/main.js                           | 6 +++---
 lib/transport/browser/abstract-xhr.js | 3 ++-
 lib/transport/iframe.js               | 4 ++--
 lib/transport/lib/ajax-based.js       | 4 +++-
 lib/transport/lib/sender-receiver.js  | 6 ++++--
 lib/transport/receiver/jsonp.js       | 3 ++-
 lib/transport/sender/jsonp.js         | 3 ++-
 lib/transport/websocket.js            | 3 ++-
 lib/utils/url.js                      | 9 +++++++++
 tests/lib/echo-tests.js               | 9 +++++++--
 tests/lib/main.js                     | 8 +-------
 tests/lib/transports.js               | 1 +
 14 files changed, 47 insertions(+), 26 deletions(-)

diff --git a/Makefile b/Makefile
index 78b1b20..d8060b7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
 
-tests/html/lib/sockjs.js:
+testbundle:
 	@./node_modules/.bin/gulp testbundle
 
-test: tests/html/lib/sockjs.js
+test: testbundle
 	@if [ "x$(BROWSER_NAME)" = "x" ]; then make test-node; else make test-zuul; fi
 
 test-node:
@@ -23,4 +23,7 @@ test-zuul:
 		tests/browser.js; \
 	fi
 
-.PHONY: test test-node test-zuul
+test-local: testbundle
+	@./node_modules/.bin/zuul --local 9090 -- tests/browser.js
+
+.PHONY: test test-node test-zuul test-local testbundle
diff --git a/lib/info-receiver.js b/lib/info-receiver.js
index 23590e0..73ea0fe 100644
--- a/lib/info-receiver.js
+++ b/lib/info-receiver.js
@@ -2,6 +2,7 @@
 
 var EventEmitter = require('events').EventEmitter
   , util = require('util')
+  , urlUtils = require('./utils/url')
   , XDR = require('./transport/sender/xdr')
   , XHRCors = require('./transport/sender/xhr-cors')
   , XHRLocal = require('./transport/sender/xhr-local')
@@ -47,10 +48,10 @@ InfoReceiver._getReceiver = function(url, urlInfo) {
 };
 
 InfoReceiver.prototype.doXhr = function(baseUrl, urlInfo) {
-  debug('doXhr', baseUrl);
   var self = this
-    , url = baseUrl + '/info'
+    , url = urlUtils.addPath(baseUrl, '/info')
     ;
+  debug('doXhr', url);
 
   this.xo = InfoReceiver._getReceiver(url, urlInfo);
 
diff --git a/lib/main.js b/lib/main.js
index 2dd66a6..d946ca1 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -55,8 +55,8 @@ function SockJS(url, protocols, transportsWhitelist) {
   var parsedUrl = u.parse(url);
   if (!parsedUrl.host || !parsedUrl.pathname || !parsedUrl.protocol) {
     throw new SyntaxError("The URL '" + url + "' is invalid");
-  } else if (parsedUrl.search || parsedUrl.hash) {
-    throw new SyntaxError('The URL must not contain a query string or fragment');
+  } else if (parsedUrl.hash) {
+    throw new SyntaxError('The URL must not contain a fragment');
   } else if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {
     throw new SyntaxError("The URL's scheme must be either 'http:' or 'https:'. '" + parsedUrl.protocol + "' is not allowed.");
   }
@@ -207,7 +207,7 @@ SockJS.prototype._connect = function() {
     this._transportTimeoutId = setTimeout(this._transportTimeout.bind(this), timeoutMs);
     debug('using timeout ', timeoutMs);
 
-    var transportUrl = this._transUrl + '/' + this._server + '/' + random.string(8);
+    var transportUrl = urlUtils.addPath(this._transUrl, '/' + this._server + '/' + random.string(8));
     debug('transport url: %s', transportUrl);
     var transport = new Transport(transportUrl, this._transUrl);
     transport.on('message', this._transportMessage.bind(this));
diff --git a/lib/transport/browser/abstract-xhr.js b/lib/transport/browser/abstract-xhr.js
index bb53625..6738e32 100644
--- a/lib/transport/browser/abstract-xhr.js
+++ b/lib/transport/browser/abstract-xhr.js
@@ -3,6 +3,7 @@
 var EventEmitter = require('events').EventEmitter
   , util = require('util')
   , utils = require('../../utils/event')
+  , urlUtils = require('../../utils/url')
   , XHR = global.XMLHttpRequest
   ;
 
@@ -30,7 +31,7 @@ function AbstractXHRObject(method, url, payload, opts) {
   if (global.ActiveXObject || global.XDomainRequest) {
     debug('adding to url to prevent POST cache');
     // IE8 caches even POSTs
-    url += ((url.indexOf('?') === -1) ? '?' : '&') + 't=' + Date.now();
+    url = urlUtils.addQuery(url, 't=' + Date.now());
   }
 
   // Explorer tends to keep connection open, even after the
diff --git a/lib/transport/iframe.js b/lib/transport/iframe.js
index 94d6dc4..0f104a0 100644
--- a/lib/transport/iframe.js
+++ b/lib/transport/iframe.js
@@ -24,7 +24,6 @@ if (process.env.NODE_ENV !== 'production') {
 }
 
 function IframeTransport(transport, transUrl, baseUrl) {
-  debug(transport, transUrl, baseUrl);
   EventEmitter.call(this);
 
   var self = this;
@@ -34,7 +33,8 @@ function IframeTransport(transport, transUrl, baseUrl) {
   this.transport = transport;
   this.windowId = random.string(8);
 
-  var iframeUrl = baseUrl + '/iframe.html#' + this.windowId;
+  var iframeUrl = urlUtils.addPath(baseUrl, '/iframe.html') + '#' + this.windowId;
+  debug(transport, transUrl, iframeUrl);
 
   this.iframeObj = iframeUtils.createIframe(iframeUrl, function(r) {
     debug('err callback');
diff --git a/lib/transport/lib/ajax-based.js b/lib/transport/lib/ajax-based.js
index f73ca5f..38c4fc0 100644
--- a/lib/transport/lib/ajax-based.js
+++ b/lib/transport/lib/ajax-based.js
@@ -1,6 +1,7 @@
 'use strict';
 
 var util = require('util')
+  , urlUtils = require('../../utils/url')
   , SenderReceiver = require('./sender-receiver')
   ;
 
@@ -16,7 +17,8 @@ function createAjaxSender(AjaxObject) {
     if (typeof payload === 'string') {
       opt.headers = {'Content-type':'text/plain'};
     }
-    var xo = new AjaxObject('POST', url + '/xhr_send', payload, opt);
+    var ajaxUrl = urlUtils.addPath(url, '/xhr_send');
+    var xo = new AjaxObject('POST', ajaxUrl, payload, opt);
     xo.once('finish', function(status) {
       debug('finish', status);
       if (status !== 200 && status !== 204) {
diff --git a/lib/transport/lib/sender-receiver.js b/lib/transport/lib/sender-receiver.js
index 8c5d249..4b54563 100644
--- a/lib/transport/lib/sender-receiver.js
+++ b/lib/transport/lib/sender-receiver.js
@@ -1,6 +1,7 @@
 'use strict';
 
 var util = require('util')
+  , urlUtils = require('../../utils/url')
   , BufferedSender = require('./buffered-sender')
   , Polling = require('./polling')
   ;
@@ -11,11 +12,12 @@ if (process.env.NODE_ENV !== 'production') {
 }
 
 function SenderReceiver(transUrl, urlSuffix, senderFunc, Receiver, AjaxObject) {
-  debug(transUrl, urlSuffix);
+  var pollUrl = urlUtils.addPath(transUrl, urlSuffix);
+  debug(pollUrl);
   var self = this;
   BufferedSender.call(this, transUrl, senderFunc);
 
-  this.poll = new Polling(Receiver, transUrl + urlSuffix, AjaxObject);
+  this.poll = new Polling(Receiver, pollUrl, AjaxObject);
   this.poll.on('message', function(msg) {
     debug('poll message', msg);
     self.emit('message', msg);
diff --git a/lib/transport/receiver/jsonp.js b/lib/transport/receiver/jsonp.js
index 4db9960..b3dad2e 100644
--- a/lib/transport/receiver/jsonp.js
+++ b/lib/transport/receiver/jsonp.js
@@ -3,6 +3,7 @@
 var utils = require('../../utils/iframe')
   , random = require('../../utils/random')
   , browser = require('../../utils/browser')
+  , urlUtils = require('../../utils/url')
   , util = require('util')
   , EventEmitter = require('events').EventEmitter
   ;
@@ -20,7 +21,7 @@ function JsonpReceiver(url) {
   utils.polluteGlobalNamespace();
 
   this.id = 'a' + random.string(6);
-  var urlWithId = url + '?c=' + encodeURIComponent(utils.WPrefix + '.' + this.id);
+  var urlWithId = urlUtils.addQuery(url, 'c=' + encodeURIComponent(utils.WPrefix + '.' + this.id));
 
   global[utils.WPrefix][this.id] = this._callback.bind(this);
   this._createScript(urlWithId);
diff --git a/lib/transport/sender/jsonp.js b/lib/transport/sender/jsonp.js
index d9d55b0..a0326ad 100644
--- a/lib/transport/sender/jsonp.js
+++ b/lib/transport/sender/jsonp.js
@@ -1,6 +1,7 @@
 'use strict';
 
 var random = require('../../utils/random')
+  , urlUtils = require('../../utils/url')
   ;
 
 var debug = function() {};
@@ -45,7 +46,7 @@ module.exports = function(url, payload, callback) {
   }
   var id = 'a' + random.string(8);
   form.target = id;
-  form.action = url + '/jsonp_send?i=' + id;
+  form.action = urlUtils.addQuery(urlUtils.addPath(url, '/jsonp_send'), 'i=' + id);
 
   var iframe = createIframe(id);
   iframe.id = id;
diff --git a/lib/transport/websocket.js b/lib/transport/websocket.js
index c1c5d3b..f6ea2ce 100644
--- a/lib/transport/websocket.js
+++ b/lib/transport/websocket.js
@@ -1,6 +1,7 @@
 'use strict';
 
 var utils = require('../utils/event')
+  , urlUtils = require('../utils/url')
   , util = require('util')
   , EventEmitter = require('events').EventEmitter
   , WebsocketDriver = require('./driver/websocket')
@@ -16,7 +17,7 @@ function WebSocketTransport(transUrl) {
   debug('constructor', transUrl);
 
   var self = this;
-  var url = transUrl + '/websocket';
+  var url = urlUtils.addPath(transUrl, '/websocket');
   if (url.slice(0, 5) === 'https') {
     url = 'wss' + url.slice(5);
   } else {
diff --git a/lib/utils/url.js b/lib/utils/url.js
index 4744f5d..31e09cf 100644
--- a/lib/utils/url.js
+++ b/lib/utils/url.js
@@ -35,4 +35,13 @@ module.exports = {
 , isSchemeEqual: function(a, b) {
     return (a.split(':')[0] === b.split(':')[0]);
   }
+
+, addPath: function (url, path) {
+    var qs = url.split('?');
+    return qs[0] + path + (qs[1] ? '?' + qs[1] : '');
+  }
+
+, addQuery: function (url, q) {
+    return url + (url.indexOf('?') === -1 ? ('?' + q) : ('&' + q));
+  }
 };
diff --git a/tests/lib/echo-tests.js b/tests/lib/echo-tests.js
index cf40b50..197fcfc 100644
--- a/tests/lib/echo-tests.js
+++ b/tests/lib/echo-tests.js
@@ -5,14 +5,14 @@ var expect = require('expect.js')
   , debug = require('debug')('sockjs-client:tests:echo')
   ;
 
-function echoFactory(transport, messages) {
+function echoFactory(transport, messages, url) {
   return function (done) {
     var title = this.runnable().fullTitle();
     debug('start', title);
     this.timeout(10000);
     var msgs = messages.slice(0);
 
-    var sjs = testUtils.newSockJs('/echo', transport);
+    var sjs = testUtils.newSockJs(url || '/echo', transport);
     sjs.onopen = function () {
       sjs.send(msgs[0]);
     };
@@ -40,6 +40,11 @@ module.exports.echoBasic = function echoBasic(transport) {
   it('echo basic', echoFactory(transport, messages));
 };
 
+module.exports.echoQueryString = function echoBasic(transport) {
+  var messages = ['data'];
+  it('echo querystring', echoFactory(transport, messages, '/echo?testqs=1'));
+};
+
 module.exports.echoRich = function echoRich(transport) {
   var messages = [
     [1, 2, 3, 'data'], null, false, 'data', 1, 12.0, {
diff --git a/tests/lib/main.js b/tests/lib/main.js
index 5302b68..36cebb2 100644
--- a/tests/lib/main.js
+++ b/tests/lib/main.js
@@ -52,13 +52,7 @@ describe('SockJS', function() {
         });
       });
 
-      it('should throw SyntaxError when the url contains a querystring or fragment', function () {
-        expect(function () {
-          new SockJS('http://localhost/?test');
-        }).to.throwException(function (e) {
-          expect(e).to.be.a(SyntaxError);
-        });
-
+      it('should throw SyntaxError when the url contains a fragment', function () {
          expect(function () {
           new SockJS('http://localhost/#test');
         }).to.throwException(function (e) {
diff --git a/tests/lib/transports.js b/tests/lib/transports.js
index e609d64..bc90e51 100644
--- a/tests/lib/transports.js
+++ b/tests/lib/transports.js
@@ -40,6 +40,7 @@ describe('Transports', function () {
 
       var transport = Trans.transportName;
       echoTests.echoBasic(transport);
+      echoTests.echoQueryString(transport);
       echoTests.echoRich(transport);
       echoTests.echoUnicode(transport);
       echoTests.echoSpecialChars(transport);

-- 
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