Added res.cache() method
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent caec590 · Browse files · 2 files·+64 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+64 added−7 removed
- tests:+54 added−3 removed
- other:+10 added−4 removed
Provenance
- Commit
e21a79d- Tree
09ea8bb- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
caec590
2 files changed+64 added−7 removed
modified:lib/response.js+10 added-4 removed
40 unmodified lines41 return this;42 };43 44 /**45- * Set Cache-Control to the given `str`.45+ * Set Cache-Control to the given `type` and `options`.46 *47- * @param {String} str47+ * @param {String} type48+ * @param {Object} options49 * @return {ServerResponse}50 * @api public51 */52 52-res.cache = function(str){53- return this.set('Cache-Control', str);53+res.cache = function(type, options){54+ var val = type;55+ options = options || {};56+ if (options.maxAge) val += ', max-age=' + (options.maxAge / 1000); 57+ if (options.maxStale) val += ', max-stale=' + (options.maxStale / 1000); 58+ if (options.minFresh) val += ', min-fresh=' + (options.minFresh / 1000); 59+ return this.set('Cache-Control', val);60 };61 62 /**63 * Send a response.531 unmodified linesmodified:test/res.cache.js+54 added-3 removed
123456678910111112131415161717181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
var express = require('../') , request = require('./support/http');
describe('res', function(){ describe('.cache(str)', function(){ describe('.cache(type)', function(){ it('should set Cache-Control', function(done){ var app = express();
app.use(function(req, res){ res.cache('public, max-age=3000').end('whoop'); res.cache('public').end('whoop'); });
request(app) .get('/') .end(function(res){ res.headers['cache-control'].should.equal('public, max-age=3000'); res.headers['cache-control'].should.equal('public'); res.body.should.equal('whoop'); done(); }) }) describe('maxAge option', function(){ it('should accept milliseconds', function(done){ var app = express();
app.use(function(req, res){ res.cache('private', { maxAge: 60 * 1000 }).end(); });
request(app) .get('/') .end(function(res){ res.headers['cache-control'].should.equal('private, max-age=60'); done(); }) }) }) describe('maxStale option', function(){ it('should accept milliseconds', function(done){ var app = express();
app.use(function(req, res){ res.cache('private', { maxStale: 60 * 1000 }).end(); });
request(app) .get('/') .end(function(res){ res.headers['cache-control'].should.equal('private, max-stale=60'); done(); }) }) }) describe('minFresh option', function(){ it('should accept milliseconds', function(done){ var app = express();
app.use(function(req, res){ res.cache('private', { minFresh: 60 * 1000 }).end(); });
request(app) .get('/') .end(function(res){ res.headers['cache-control'].should.equal('private, min-fresh=60'); done(); }) }) }) })})