Skip to content

Commit

Permalink
Add failing test for #3555
Browse files Browse the repository at this point in the history
  • Loading branch information
dasilvacontin committed May 24, 2020
1 parent 8f90ba9 commit 5492655
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ Server.prototype.serveClient = function(v){
return this;
};

/**
* Empties cache of client code.
* @api public
*/
Server.prototype.clearClientCodeCache = function(){
clientSource = undefined;
clientSourceMap = undefined;
}

/**
* Old settings for backwards compatibility
*/
Expand Down
65 changes: 65 additions & 0 deletions test/fixtures/create-fake-socket.io-client-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var fs = require('fs');
var join = require('path').join;
var io = require('../../lib');

var testHadToCreateFakeModule = false
var fakeModuleLocation = '../socket.io-client'
var socketIOClientMainFilePath = fakeModuleLocation + '/dist/socket.io.js'

before(function(){
// set up at fake socket.io-client module sitting at ../.. (sibling to socket.io folder). Emulates being on the same level inside a node_modules folder
// http.Server should ignore the fake module, and use the one in socket.io/node_modules/socket.io-client
try {
fs.mkdirSync(fakeModuleLocation);
testHadToCreateFakeModule = true;
} catch (_) {}

// load fake package.json
var fakePackageFixture = fs.readFileSync('./test/fixtures/package.json');
// place fake package.json, if necessary
try {
fs.writeFileSync(fakeModuleLocation + '/package.json', fakePackageFixture, { flag: 'wx' });
// 'wx' mode fails if the file exists
} catch (_) {}

try {
fs.mkdirSync(fakeModuleLocation + '/dist');
} catch (_) {}

// temporarily put away the real main file, if it exists
try {
fs.renameSync(socketIOClientMainFilePath, socketIOClientMainFilePath + '.temp')
} catch (_) {}
// write in the fake main file
fs.writeFileSync(socketIOClientMainFilePath, '\'im a fake socket.io-client\'');

io().clearClientCodeCache();
})

after(function(){
// put the temp file back into place, if it exists
try {
fs.renameSync(socketIOClientMainFilePath + '.temp', socketIOClientMainFilePath)
} catch (_) {}

if (testHadToCreateFakeModule) {
// delete whole folder
var deleteFolderRecursive = function(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach((file, index) => {
var curPath = join(path, file);
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};

deleteFolderRecursive(fakeModuleLocation);
}

io().clearClientCodeCache();
});
8 changes: 8 additions & 0 deletions test/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "socket.io-client",
"version": "2.3.0",
"keywords": [
"this package.json is a fixture for a socket.io test"
],
"main": "./lib/index"
}
17 changes: 17 additions & 0 deletions test/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,23 @@ describe('socket.io', function(){
done();
});
});

describe('when serving the socket.io-client lib', function(){
require('./fixtures/create-fake-socket.io-client-module.js')

it('should serve the closest socket.io-client module it can find', function(done){
var srv = http();
io(srv);
request(srv)
.get('/socket.io/socket.io.js')
.buffer(true)
.end(function(err, res){
if (err) return done(err);
expect(res.text).to.not.match(/im a fake socket\.io-client/);
done();
})
});
});
});

describe('port', function(done){
Expand Down

0 comments on commit 5492655

Please sign in to comment.