Skip to content

Commit

Permalink
Changed: res.send() always checks freshness
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 15, 2012
1 parent f25aaf1 commit 640cf4c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
4 changes: 3 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ res.send = function(body){
if (!this.get('ETag')) this.set('ETag', Buffer.isBuffer(body)
? crc.buffer.crc32(body)
: crc.crc32(body));
if (req.fresh) this.statusCode = 304;
}

// freshness
if (req.fresh) this.statusCode = 304;

// strip irrelevant headers
if (204 == this.statusCode || 304 == this.statusCode) {
this.removeHeader('Content-Type');
Expand Down
5 changes: 1 addition & 4 deletions test/req.fresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ describe('req', function(){
request(app)
.get('/')
.set('If-None-Match', '12345')
.end(function(res){
res.body.should.equal('true');
done();
});
.expect(304, done);
})

it('should return false when the resource is modified', function(done){
Expand Down
5 changes: 1 addition & 4 deletions test/req.stale.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ describe('req', function(){
request(app)
.get('/')
.set('If-None-Match', '12345')
.end(function(res){
res.body.should.equal('false');
done();
});
.expect(304, done);
})

it('should return true when the resource is modified', function(done){
Expand Down
14 changes: 14 additions & 0 deletions test/res.send.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,20 @@ describe('res', function(){
})
})

it('should always check freshness', function(done){
var app = express();

app.use(function(req, res, next){
res.set('ETag', 'asdf');
res.send('hey');
});

request(app)
.get('/')
.set('If-None-Match', 'asdf')
.expect(304, done);
})

it('should respond with 304 Not Modified when fresh', function(done){
var app = express();

Expand Down

0 comments on commit 640cf4c

Please sign in to comment.