response.etag and response.lastModified
This commit is contained in:
parent
5467f98558
commit
2f74207e6c
3 changed files with 110 additions and 0 deletions
|
@ -295,6 +295,61 @@ Response.prototype = {
|
|||
this.set('Content-Type', type);
|
||||
},
|
||||
|
||||
/**
|
||||
* Set the Last-Modified date using a string or a Date.
|
||||
*
|
||||
* this.response.lastModified = new Date();
|
||||
* this.response.lastModified = '2013-09-13';
|
||||
*
|
||||
* @param {String|Date} type
|
||||
* @api public
|
||||
*/
|
||||
|
||||
set lastModified(val) {
|
||||
if ('string' == typeof val) val = new Date(val);
|
||||
this.set('Last-Modified', val.toUTCString());
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the Last-Modified date in Date form, if it exists.
|
||||
*
|
||||
* @return {Date}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
get lastModified() {
|
||||
var date = this.get('last-modified');
|
||||
if (date) return new Date(date);
|
||||
},
|
||||
|
||||
/**
|
||||
* Set the ETag of a response.
|
||||
* This will normalize the quotes if necessary.
|
||||
*
|
||||
* this.response.etag = 'md5hashsum';
|
||||
* this.response.etag = '"md5hashsum"';
|
||||
* this.response.etag = 'W/"123456789"';
|
||||
*
|
||||
* @param {String} etag
|
||||
* @api public
|
||||
*/
|
||||
|
||||
set etag(val) {
|
||||
if (!/^(W\/)?".*"$/.test(val)) val = '"' + val + '"';
|
||||
this.set('ETag', val);
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the ETag of a response.
|
||||
*
|
||||
* @return {String}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
get etag() {
|
||||
return this.get('etag');
|
||||
},
|
||||
|
||||
/**
|
||||
* Return the request mime type void of
|
||||
* parameters such as "charset".
|
||||
|
|
28
test/response/etag.js
Normal file
28
test/response/etag.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
|
||||
var response = require('../response');
|
||||
|
||||
describe('res.etag', function(){
|
||||
it('should not modify an etag with quotes', function(){
|
||||
var res = response();
|
||||
res.etag = '"asdf"';
|
||||
res.header.etag.should.equal('"asdf"');
|
||||
})
|
||||
|
||||
it('should not modify a weak etag', function(){
|
||||
var res = response();
|
||||
res.etag = 'W/"asdf"';
|
||||
res.header.etag.should.equal('W/"asdf"');
|
||||
})
|
||||
|
||||
it('should add quotes around an etag if necessary', function(){
|
||||
var res = response();
|
||||
res.etag = 'asdf';
|
||||
res.header.etag.should.equal('"asdf"');
|
||||
})
|
||||
|
||||
it('should get the etag', function(){
|
||||
var res = response();
|
||||
res.set('etag', '"asdf"');
|
||||
res.etag.should.equal('"asdf"');
|
||||
})
|
||||
})
|
27
test/response/last-modified.js
Normal file
27
test/response/last-modified.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
|
||||
var response = require('../response');
|
||||
|
||||
describe('res.lastModified', function(){
|
||||
it('should set the header as a UTCString', function(){
|
||||
var res = response();
|
||||
var date = new Date();
|
||||
res.lastModified = date;
|
||||
res.header['last-modified'].should.equal(date.toUTCString());
|
||||
})
|
||||
|
||||
it('should work with date strings', function(){
|
||||
var res = response();
|
||||
var date = new Date();
|
||||
res.lastModified = date.toString();
|
||||
res.header['last-modified'].should.equal(date.toUTCString());
|
||||
})
|
||||
|
||||
it('should get the header as a Date', function(){
|
||||
// Note: Date() removes milliseconds, but it's practically important.
|
||||
var res = response();
|
||||
var date = new Date();
|
||||
res.lastModified = date;
|
||||
(res.lastModified.getTime() / 1000)
|
||||
.should.equal(Math.floor(date.getTime() / 1000));
|
||||
})
|
||||
})
|
Loading…
Reference in a new issue