From b0b0d69ce48520e2f05662fe55cb996c22d1de01 Mon Sep 17 00:00:00 2001 From: James Coglan Date: Sat, 17 Oct 2015 21:44:36 +0100 Subject: [PATCH] Throw a more helpful error if a client driver is created with an invalid URL. --- lib/websocket/driver/client.js | 5 +++++ spec/websocket/driver/client_spec.js | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/websocket/driver/client.js b/lib/websocket/driver/client.js index 51aff2c..59cc959 100644 --- a/lib/websocket/driver/client.js +++ b/lib/websocket/driver/client.js @@ -20,6 +20,9 @@ var Client = function(_url, options) { var uri = url.parse(this.url), auth = uri.auth && new Buffer(uri.auth, 'utf8').toString('base64'); + if (this.VALID_PROTOCOLS.indexOf(uri.protocol) < 0) + throw new Error(this.url + ' is not a valid WebSocket URL'); + this._pathname = (uri.pathname || '/') + (uri.search || ''); this._headers.set('Host', uri.host); @@ -41,6 +44,8 @@ Client.generateKey = function() { }; var instance = { + VALID_PROTOCOLS: ['ws:', 'wss:'], + proxy: function(origin, options) { return new Proxy(this, origin, options); }, diff --git a/spec/websocket/driver/client_spec.js b/spec/websocket/driver/client_spec.js index 7221c6f..799447e 100644 --- a/spec/websocket/driver/client_spec.js +++ b/spec/websocket/driver/client_spec.js @@ -109,6 +109,16 @@ test.describe("Client", function() { with(this) { }}) }}) + describe("with an invalid URL", function() { with(this) { + define("url", function() { return "stream.wikimedia.org/rc" }) + + it("throws an error", function() { with(this) { + var message + try { driver() } catch (e) { message = e.message } + assertEqual( "stream.wikimedia.org/rc is not a valid WebSocket URL", message ) + }}) + }}) + describe("with custom headers", function() { with(this) { before(function() { with(this) { driver().setHeader("User-Agent", "Chrome")