Permalink
Browse files

Added Buffer ETag support to res.send()

  • Loading branch information...
1 parent b79853e commit c0a68fcd0d42a65674fc22a9393b6994c9e1f6d7 @tj tj committed Apr 29, 2012
Showing with 41 additions and 7 deletions.
  1. +8 −6 lib/response.js
  2. +33 −1 test/res.send.js
View
@@ -18,7 +18,7 @@ var fs = require('fs')
, normalizeTypes = require('./utils').normalizeTypes
, statusCodes = http.STATUS_CODES
, send = connect.static.send
- , crc32 = require('crc').crc32
+ , crc = require('crc')
, mime = require('mime')
, basename = path.basename
, extname = path.extname
@@ -104,15 +104,17 @@ res.send = function(body){
// populate Content-Length
if (undefined !== body && !this.get('Content-Length')) {
- len = Buffer.isBuffer(body) ? body.length : Buffer.byteLength(body);
- this.set('Content-Length', len);
+ this.set('Content-Length', len = Buffer.isBuffer(body)
+ ? body.length
+ : Buffer.byteLength(body));
}
// ETag support
// TODO: W/ support
- // TODO: Buffer support for crc32
- if (len > 1024 && 'string' == typeof body) {
- this.set('ETag', crc32(body));
+ if (len > 1024) {
+ this.set('ETag', Buffer.isBuffer(body)
+ ? crc.buffer.crc32(body)
+ : crc.crc32(body));
if (req.fresh) this.statusCode = 304;
}
View
@@ -90,6 +90,22 @@ describe('res', function(){
done();
})
})
+
+ it('should set ETag', function(done){
+ var app = express();
+
+ app.use(function(req, res){
+ var str = Array(1024 * 2).join('-');
+ res.send(str);
+ });
+
+ request(app)
+ .get('/')
+ .end(function(res){
+ res.headers.should.have.property('etag', '-1498647312');
+ done();
+ })
+ })
it('should not override Content-Type', function(done){
var app = express();
@@ -162,7 +178,23 @@ describe('res', function(){
done();
})
})
-
+
+ it('should set ETag', function(done){
+ var app = express();
+
+ app.use(function(req, res){
+ var str = Array(1024 * 2).join('-');
+ res.send(new Buffer(str));
+ });
+
+ request(app)
+ .get('/')
+ .end(function(res){
+ res.headers.should.have.property('etag', '-1498647312');
+ done();
+ })
+ })
+
it('should not override Content-Type', function(done){
var app = express();

0 comments on commit c0a68fc

Please sign in to comment.