Skip to content

Commit d33a619

Browse files
sebamarynissendarrachequesne
authored andcommittedJan 4, 2021
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 #3495
1 parent 3951a79 commit d33a619

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
 

‎lib/socket.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Socket.prototype.buildHandshake = function(query){
116116
function buildQuery(){
117117
var requestQuery = url.parse(self.request.url, true).query;
118118
//if socket-specific query exist, replace query strings in requestQuery
119-
return Object.assign({}, query, requestQuery);
119+
return Object.assign({}, requestQuery, query);
120120
}
121121
return {
122122
headers: this.request.headers,

‎test/socket.io.js

+17
Original file line numberDiff line numberDiff line change
@@ -1621,8 +1621,25 @@ describe('socket.io', function(){
16211621
expect(s.handshake.query.key2).to.be('&=bb');
16221622
done();
16231623
});
1624+
});
1625+
1626+
it('should see the query options sent in the Socket.IO handshake (specific to the given socket)', (done) => {
1627+
const srv = http();
1628+
const sio = io(srv);
1629+
const socket = client(srv, '/namespace',{ query: { key1: 'a', key2: 'b' }}); // manager-specific query option
1630+
socket.query = { key2: 'c' }; // socket-specific query option
16241631

1632+
const success = () => {
1633+
sio.close();
1634+
socket.close();
1635+
done();
1636+
}
16251637

1638+
sio.of('/namespace').on('connection', (s) => {
1639+
expect(s.handshake.query.key1).to.be('a'); // in the query params
1640+
expect(s.handshake.query.key2).to.be('c'); // in the Socket.IO handshake
1641+
success();
1642+
});
16261643
});
16271644

16281645
it('should handle very large json', function(done){

0 commit comments

Comments
 (0)
Please sign in to comment.