Skip to content

Commit

Permalink
ignore query parameters when attaching a websocket (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
sechel authored and thoov committed Jul 24, 2019
1 parent 1c1e777 commit 0026b6d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/network-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class NetworkBridge {
* @param {string} url
*/
attachWebSocket(websocket, url) {
const connectionLookup = this.urlMap[url];
const queryIndex = url.indexOf('?');
const serverURL = queryIndex >= 0 ? url.slice(0, queryIndex) : url;
const connectionLookup = this.urlMap[serverURL];

if (connectionLookup && connectionLookup.server && connectionLookup.websockets.indexOf(websocket) === -1) {
connectionLookup.websockets.push(websocket);
Expand Down
13 changes: 11 additions & 2 deletions tests/unit/network-bridge.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import test from 'ava';

import networkBridge from '../../src/network-bridge';

const fakeObject = { foo: 'bar' };
Expand All @@ -7,11 +8,19 @@ test.afterEach(() => {
networkBridge.urlMap = {};
});

test('that network bridge has no connections be defualt', t => {
test('that url query parameters are ignored', t => {
networkBridge.attachServer(fakeObject, 'wss://not-real/');
networkBridge.attachWebSocket({}, 'wss://not-real/?foo=42');
networkBridge.attachWebSocket({}, 'wss://not-real/?foo=0');
const connection = networkBridge.urlMap['wss://not-real/'];
t.is(connection.websockets.length, 2, 'two websockets have been attached to the same connection');
});

test('that network bridge has no connections by default', t => {
t.deepEqual(networkBridge.urlMap, {}, 'Url map is empty by default');
});

test('that network bridge has no connections be defualt', t => {
test('that network bridge has no connections by default', t => {
const result = networkBridge.attachWebSocket(fakeObject, 'ws://localhost:8080');

t.truthy(!result, 'no server was returned as a server must be added first');
Expand Down

0 comments on commit 0026b6d

Please sign in to comment.