Skip to content

Commit

Permalink
Merge c308a0a into 31c6a5e
Browse files Browse the repository at this point in the history
  • Loading branch information
magicdawn committed Sep 1, 2015
2 parents 31c6a5e + c308a0a commit c803c8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ function send(ctx, path, opts) {
ctx.set('Content-Length', stats.size);
ctx.set('Cache-Control', 'max-age=' + (maxage / 1000 | 0));
ctx.type = type(path);
ctx.body = fs.createReadStream(path);

// if fresh, 304
ctx.status = 200;
if (ctx.fresh) {
ctx.status = 304;
} else {
ctx.body = fs.createReadStream(path);
}

return path;
}
Expand Down
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,24 @@ describe('send(ctx, file)', function(){
.expect(200, done);
})
})

it('should return 304 when fresh',function(done){
var app = koa();
app.use(function *(){
yield send(this, '/test/fixtures/user.json');
});
app = app.callback();

request(app)
.get('/')
.expect(200)
.end(function(err,res){
request(app)
.get('/')
.set('if-modified-since',res.headers['last-modified'])
.expect(304, done);
})
})
})

it('should set the Content-Type', function(done){
Expand Down

0 comments on commit c803c8b

Please sign in to comment.