diff --git a/lib/response.js b/lib/response.js index a63245d..8384ccf 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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". diff --git a/test/response/etag.js b/test/response/etag.js new file mode 100644 index 0000000..b01fcf7 --- /dev/null +++ b/test/response/etag.js @@ -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"'); + }) +}) \ No newline at end of file diff --git a/test/response/last-modified.js b/test/response/last-modified.js new file mode 100644 index 0000000..14369a5 --- /dev/null +++ b/test/response/last-modified.js @@ -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)); + }) +}) \ No newline at end of file