Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failing test for #3555 + the fix itself #3604

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,9 @@ Server.prototype.serveClient = function(v){
if (!arguments.length) return this._serveClient;
this._serveClient = v;
var resolvePath = function(file){
var filepath = path.resolve(__dirname, './../../', file);
if (exists(filepath)) {
return filepath;
}
return require.resolve(file);
// resolve the client dep. The resulting string ends with `lib/index.js`
var clientPath = require.resolve('socket.io-client');
return path.normalize(path.join(path.dirname(clientPath), '..', '..', file));
};
if (v && !clientSource) {
clientSource = read(resolvePath( 'socket.io-client/dist/socket.io.js'), 'utf-8');
Expand All @@ -123,6 +121,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