Skip to content

Commit fcaabb2

Browse files
pablobirukovthoov
authored andcommitted
Trim query part from URL where it's necessary. Fixes thoov#274 (thoov#275)
1 parent 0273ef8 commit fcaabb2

File tree

3 files changed

+93
-8
lines changed

3 files changed

+93
-8
lines changed

src/network-bridge.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { reject } from './helpers/array-helpers';
22

3+
function trimQueryPartFromURL(url) {
4+
const queryIndex = url.indexOf('?');
5+
return queryIndex >= 0 ? url.slice(0, queryIndex) : url;
6+
}
7+
38
/*
49
* The network bridge is a way for the mock websocket object to 'communicate' with
510
* all available servers. This is a singleton object so it is important that you
@@ -18,8 +23,7 @@ class NetworkBridge {
1823
* @param {string} url
1924
*/
2025
attachWebSocket(websocket, url) {
21-
const queryIndex = url.indexOf('?');
22-
const serverURL = queryIndex >= 0 ? url.slice(0, queryIndex) : url;
26+
const serverURL = trimQueryPartFromURL(url);
2327
const connectionLookup = this.urlMap[serverURL];
2428

2529
if (connectionLookup && connectionLookup.server && connectionLookup.websockets.indexOf(websocket) === -1) {
@@ -32,7 +36,7 @@ class NetworkBridge {
3236
* Attaches a websocket to a room
3337
*/
3438
addMembershipToRoom(websocket, room) {
35-
const connectionLookup = this.urlMap[websocket.url];
39+
const connectionLookup = this.urlMap[trimQueryPartFromURL(websocket.url)];
3640

3741
if (connectionLookup && connectionLookup.server && connectionLookup.websockets.indexOf(websocket) !== -1) {
3842
if (!connectionLookup.roomMemberships[room]) {
@@ -70,7 +74,8 @@ class NetworkBridge {
7074
* @param {string} url - the url to use to find which server is running on it
7175
*/
7276
serverLookup(url) {
73-
const connectionLookup = this.urlMap[url];
77+
const serverURL = trimQueryPartFromURL(url);
78+
const connectionLookup = this.urlMap[serverURL];
7479

7580
if (connectionLookup) {
7681
return connectionLookup.server;
@@ -85,8 +90,9 @@ class NetworkBridge {
8590
* @param {class} broadcaster - socket that is broadcasting and is to be excluded from the lookup
8691
*/
8792
websocketsLookup(url, room, broadcaster) {
93+
const serverURL = trimQueryPartFromURL(url);
8894
let websockets;
89-
const connectionLookup = this.urlMap[url];
95+
const connectionLookup = this.urlMap[serverURL];
9096

9197
websockets = connectionLookup ? connectionLookup.websockets : [];
9298

@@ -104,7 +110,7 @@ class NetworkBridge {
104110
* @param {string} url
105111
*/
106112
removeServer(url) {
107-
delete this.urlMap[url];
113+
delete this.urlMap[trimQueryPartFromURL(url)];
108114
}
109115

110116
/*
@@ -114,7 +120,8 @@ class NetworkBridge {
114120
* @param {string} url
115121
*/
116122
removeWebSocket(websocket, url) {
117-
const connectionLookup = this.urlMap[url];
123+
const serverURL = trimQueryPartFromURL(url);
124+
const connectionLookup = this.urlMap[serverURL];
118125

119126
if (connectionLookup) {
120127
connectionLookup.websockets = reject(connectionLookup.websockets, socket => socket === websocket);
@@ -125,7 +132,7 @@ class NetworkBridge {
125132
* Removes a websocket from a room
126133
*/
127134
removeMembershipFromRoom(websocket, room) {
128-
const connectionLookup = this.urlMap[websocket.url];
135+
const connectionLookup = this.urlMap[trimQueryPartFromURL(websocket.url)];
129136
const memberships = connectionLookup.roomMemberships[room];
130137

131138
if (connectionLookup && memberships !== null) {

tests/functional/websockets.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,23 @@ test.cb('that the server gets called when the client sends a message', t => {
114114
};
115115
});
116116

117+
test.cb('that the server gets called when the client sends a message using URL with query parameters', t => {
118+
const testServer = new Server('ws://localhost:8080');
119+
120+
testServer.on('connection', socket => {
121+
socket.on('message', data => {
122+
t.is(data, 'Testing', 'on message fires as expected');
123+
t.end();
124+
});
125+
});
126+
127+
const mockSocket = new WebSocket('ws://localhost:8080?foo=bar');
128+
129+
mockSocket.onopen = function open() {
130+
this.send('Testing');
131+
};
132+
});
133+
117134
test.cb('that the onopen function will only be called once for each client', t => {
118135
const socketUrl = 'ws://localhost:8080';
119136
const mockServer = new Server(socketUrl);

tests/unit/network-bridge.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ test('that attachWebSocket will add a websocket to the url map', t => {
5858
t.is(connection.websockets.length, 1, 'websocket property contains only the websocket object');
5959
});
6060

61+
test('that attachWebSocket will add a websocket with query params to the url map', t => {
62+
const resultServer = networkBridge.attachServer(fakeObject, 'ws://localhost:8080');
63+
const resultWebSocket = networkBridge.attachWebSocket(fakeObject, 'ws://localhost:8080?foo=bar');
64+
const connection = networkBridge.urlMap['ws://localhost:8080'];
65+
66+
t.deepEqual(resultServer, fakeObject, 'server returned because it was successfully added to the url map');
67+
t.deepEqual(resultWebSocket, fakeObject, 'server returned as the websocket was successfully added to the map');
68+
t.deepEqual(connection.websockets[0], fakeObject, 'fakeObject was added to the websockets array');
69+
t.is(connection.websockets.length, 1, 'websocket property contains only the websocket object');
70+
});
71+
6172
test('that attachWebSocket will add the same websocket only once', t => {
6273
const resultServer = networkBridge.attachServer(fakeObject, 'ws://localhost:8080');
6374
const resultWebSocket = networkBridge.attachWebSocket(fakeObject, 'ws://localhost:8080');
@@ -83,6 +94,18 @@ test('that server and websocket lookups return the correct objects', t => {
8394
t.deepEqual(websocketLookup.length, 1, 'the correct number of websockets are returned');
8495
});
8596

97+
test('that server and websocket lookups ignore query params', t => {
98+
networkBridge.attachServer(fakeObject, 'ws://localhost:8080');
99+
networkBridge.attachWebSocket(fakeObject, 'ws://localhost:8080?foo=bar');
100+
101+
const serverLookup = networkBridge.serverLookup('ws://localhost:8080?foo1=1');
102+
const websocketLookup = networkBridge.websocketsLookup('ws://localhost:8080?foo2=2');
103+
104+
t.deepEqual(serverLookup, fakeObject, 'server correctly returned');
105+
t.deepEqual(websocketLookup, [fakeObject], 'websockets correctly returned');
106+
t.deepEqual(websocketLookup.length, 1, 'the correct number of websockets are returned');
107+
});
108+
86109
test('that removing server and websockets works correctly', t => {
87110
networkBridge.attachServer(fakeObject, 'ws://localhost:8080');
88111
networkBridge.attachWebSocket(fakeObject, 'ws://localhost:8080');
@@ -99,6 +122,22 @@ test('that removing server and websockets works correctly', t => {
99122
t.deepEqual(networkBridge.urlMap, {}, 'Url map is back in its default state');
100123
});
101124

125+
test('that removing server and websockets works correctly with query params', t => {
126+
networkBridge.attachServer(fakeObject, 'ws://localhost:8080');
127+
networkBridge.attachWebSocket(fakeObject, 'ws://localhost:8080?foo=bar');
128+
129+
let websocketLookup = networkBridge.websocketsLookup('ws://localhost:8080?anything=else');
130+
t.deepEqual(websocketLookup.length, 1, 'the correct number of websockets are returned');
131+
132+
networkBridge.removeWebSocket(fakeObject, 'ws://localhost:8080?arbitraryParameter');
133+
134+
websocketLookup = networkBridge.websocketsLookup('ws://localhost:8080?one=more');
135+
t.deepEqual(websocketLookup.length, 0, 'the correct number of websockets are returned');
136+
137+
networkBridge.removeServer('ws://localhost:8080?please');
138+
t.deepEqual(networkBridge.urlMap, {}, 'Url map is back in its default state');
139+
});
140+
102141
test('a socket can join and leave a room', t => {
103142
const fakeSocket = { url: 'ws://roomy' };
104143

@@ -120,3 +159,25 @@ test('a socket can join and leave a room', t => {
120159
inRoom = networkBridge.websocketsLookup('ws://roomy', 'room');
121160
t.is(inRoom.length, 0, 'there are no sockets in the room after leaving');
122161
});
162+
163+
test('a socket with query params can join and leave a room', t => {
164+
const fakeSocket = { url: 'ws://roomy?foo=bar' };
165+
166+
networkBridge.attachServer(fakeObject, 'ws://roomy');
167+
networkBridge.attachWebSocket(fakeSocket, 'ws://roomy');
168+
169+
let inRoom;
170+
inRoom = networkBridge.websocketsLookup('ws://roomy', 'room');
171+
t.is(inRoom.length, 0, 'there are no sockets in the room to start with');
172+
173+
networkBridge.addMembershipToRoom(fakeSocket, 'room');
174+
175+
inRoom = networkBridge.websocketsLookup('ws://roomy', 'room');
176+
t.is(inRoom.length, 1, 'there is 1 socket in the room after joining');
177+
t.deepEqual(inRoom[0], fakeSocket);
178+
179+
networkBridge.removeMembershipFromRoom(fakeSocket, 'room');
180+
181+
inRoom = networkBridge.websocketsLookup('ws://roomy', 'room');
182+
t.is(inRoom.length, 0, 'there are no sockets in the room after leaving');
183+
});

0 commit comments

Comments
 (0)