From d33a619905a4905c153d4fec337c74da5b533a9e Mon Sep 17 00:00:00 2001 From: Sebastiaan Marynissen Date: Tue, 15 Oct 2019 09:20:00 +0200 Subject: [PATCH] fix: properly overwrite the query sent in the handshake The `query` option of the Manager had the priority over the one of the Socket instance, which meant updating the Socket#query object on the client-side was not reflected in the Socket#handshake object on the server-side. Please note that the behavior of the `query` option is still a bit weird in Socket.IO v2, as it only applies to non-default namespace. This is fixed in v3: - https://socket.io/docs/v3/migrating-from-2-x-to-3-0/#Add-a-clear-distinction-between-the-Manager-query-option-and-the-Socket-query-option - https://socket.io/docs/v3/middlewares/#Sending-credentials Fixes https://github.com/socketio/socket.io/issues/3495 --- lib/socket.js | 2 +- test/socket.io.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/socket.js b/lib/socket.js index 9a96929728..4e97eb55dd 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -116,7 +116,7 @@ Socket.prototype.buildHandshake = function(query){ function buildQuery(){ var requestQuery = url.parse(self.request.url, true).query; //if socket-specific query exist, replace query strings in requestQuery - return Object.assign({}, query, requestQuery); + return Object.assign({}, requestQuery, query); } return { headers: this.request.headers, diff --git a/test/socket.io.js b/test/socket.io.js index 337f2ebd4a..6cc085b798 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -1621,8 +1621,25 @@ describe('socket.io', function(){ expect(s.handshake.query.key2).to.be('&=bb'); done(); }); + }); + + it('should see the query options sent in the Socket.IO handshake (specific to the given socket)', (done) => { + const srv = http(); + const sio = io(srv); + const socket = client(srv, '/namespace',{ query: { key1: 'a', key2: 'b' }}); // manager-specific query option + socket.query = { key2: 'c' }; // socket-specific query option + const success = () => { + sio.close(); + socket.close(); + done(); + } + sio.of('/namespace').on('connection', (s) => { + expect(s.handshake.query.key1).to.be('a'); // in the query params + expect(s.handshake.query.key2).to.be('c'); // in the Socket.IO handshake + success(); + }); }); it('should handle very large json', function(done){