Skip to content

Commit

Permalink
Added more test cases and allow a more flexible constructor for addin…
Browse files Browse the repository at this point in the history
…g content
  • Loading branch information
3rd-Eden committed Jul 17, 2011
1 parent 4b94f2b commit 23e1422
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 10 deletions.
8 changes: 7 additions & 1 deletion lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Static.prototype.init = function () {
});

// generates dedicated build based on the available transports
this.add('/socket.io.js', {}, function (path, callback) {
this.add('/socket.io.js', function (path, callback) {
build(self.manager.get('transports'), callback);
});

Expand Down Expand Up @@ -236,6 +236,12 @@ Static.prototype.has = function (path) {

Static.prototype.add = function (path, options, callback) {
var extension = /(?:\.(\w{1,4}))$/.exec(path);

if (!callback && typeof options == 'function') {
callback = options;
options = {};
}

options.mime = options.mime || (extension ? mime[extension[1]] : false);

if (callback) options.callback = callback;
Expand Down
87 changes: 78 additions & 9 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

var sio = require('socket.io')
, cp = require('child_process')
, should = require('./common')
, ports = 15400;

Expand All @@ -20,6 +19,30 @@ var sio = require('socket.io')

module.exports = {

'test that the default static files are available': function (done) {
var port = ++ports
, io = sio.listen(port);

(!!io.static.has('/socket.io.js')).should.be.true;
(!!io.static.has('/socket.io+')).should.be.true;
(!!io.static.has('/static/flashsocket/WebSocketMain.swf')).should.be.true;
(!!io.static.has('/static/flashsocket/WebSocketMainInsecure.swf')).should.be.true;

io.server.close();
done();
},

'test that static files are correctly looked up': function (done) {
var port = ++ports
, io = sio.listen(port);

(!!io.static.has('/socket.io.js')).should.be.true;
(!!io.static.has('/invalidfilehereplease.js')).should.be.false;

io.server.close();
done();
},

'test that the client is served': function (done) {
var port = ++ports
, io = sio.listen(port)
Expand Down Expand Up @@ -124,7 +147,6 @@ module.exports = {
done();
});
});

},

'test that the client etag is served': function (done) {
Expand All @@ -147,6 +169,28 @@ module.exports = {
});
},

'test that the client etag is changed for new transports': function (done) {
var port = ++ports
, io = sio.listen(port)
, cl = client(port);

io.set('transports', ['websocket']);
io.enable('browser client etag');

cl.get('/socket.io/socket.io.js', function (res, data) {
var wsEtag = res.headers.etag;

io.set('transports', ['xhr-polling']);
cl.get('/socket.io/socket.io.js', function (res, data) {
res.headers.etag.should.not.equal(wsEtag);

cl.end();
io.server.close();
done();
});
});
},

'test that the client is served with gzip': function (done) {
var port = ++ports
, io = sio.listen(port)
Expand Down Expand Up @@ -204,7 +248,7 @@ module.exports = {
, io = sio.listen(port)
, cl = client(port);

io.static.add('/random.js', {}, function (path, callback) {
io.static.add('/random.js', function (path, callback) {
var random = Math.floor(Date.now() * Math.random()).toString();
callback(null, new Buffer(random));
});
Expand Down Expand Up @@ -275,13 +319,14 @@ module.exports = {
headers: {
'if-none-match': res.headers.etag
}
}, function (res, data) {
res.statusCode.should.eql(304);
}, function (res, data) {
res.statusCode.should.eql(304);

cl.end();
io.server.close();
done();
});
cl.end();
io.server.close();
done();
}
);
});
},

Expand Down Expand Up @@ -361,6 +406,30 @@ module.exports = {
});
},

'test that swf files are not served with gzip': function (done) {
var port = ++ports
, io = sio.listen(port)
, cl = client(port);

io.enable('browser client gzip');

cl.get('/socket.io/static/flashsocket/WebSocketMain.swf', {
headers: {
'accept-encoding': 'deflate, gzip'
}
}
, function (res, data) {
res.headers['content-type'].should.eql('application/x-shockwave-flash');
res.headers['content-length'].should.match(/([0-9]+)/);
should.strictEqual(res.headers['content-encoding'], undefined);

cl.end();
io.server.close();
done();
}
);
},

'test that you can serve custom clients': function (done) {
var port = ++ports
, io = sio.listen(port)
Expand Down

0 comments on commit 23e1422

Please sign in to comment.