Added res.cookie() and res.clearCookie()
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 6eeb425 · Browse files · 3 files·+54 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+54 added−5 removed
- tests:+18 added−0 removed
- other:+36 added−5 removed
Provenance
- Commit
041b7e8- Tree
ebfdfd7- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
6eeb425
3 files changed+54 added−5 removed
modified:examples/session/app.js+1 added-1 removed
12 unmodified lines1314151617171819202111 unmodified lines12 unmodified lines
// Populates: // - req.session // - req.sessionStore // - req.sessionHash (the SID fingerprint) // - req.sessionID express.session());
app.get('/', function(req, res){11 unmodified linesmodified:lib/express/response.js+35 added-4 removed
218 unmodified lines219 }220 };221 222 /**223+ * Clear cookie `name`.224+ *225+ * @param {String} name226+ * @api public227+ */228+229+http.ServerResponse.prototype.clearCookie = function(name){230+ this.cookie(name, '', { expires: new Date(1) });231+};232+233+/**234+ * Set cookie `name` to `val`.235+ *236+ * Examples:237+ *238+ * // "Remember Me" for 15 minutes239+ * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });240+ *241+ * @param {String} name242+ * @param {String} val243+ * @param {Options} options244+ * @api public245+ */246+247+http.ServerResponse.prototype.cookie = function(name, val, options){248+ var cookie = utils.serializeCookie(name, val, options);249+ this.header('Set-Cookie', cookie);250+};251+252+/**253 * Redirect to the given `url` with optional response `status`254 * defauling to 302.255 *256 * The given `url` can also be the name of a mapped url, for51 unmodified lines308 : map[url];309 310 // Perform redirect311 url = mapped || url;282- this.send('Redirecting to ' + url, {283- 'Content-Type': 'text/plain',284- 'Location': url285- }, status || 302);312+313+ // Respond314+ this.headers['Content-Type'] = 'text/plain';315+ this.headers['Location'] = url;316+ this.send('Redirecting to ' + url, this.headers, status || 302);317 };modified:test/response.test.js+18 added
235 unmodified lines236 { body: '{"name":"tj"}', status: 200, headers: {237 'Content-Type': 'application/json',238 'Content-Disposition': 'attachment; filename="account.json"'239 }});240+ },241+ 242+ 'test #cookie()': function(assert){243+ var app = express.createServer();244+ 245+ app.get('/', function(req, res){246+ res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });247+ res.redirect('/');248+ });249+ 250+ assert.response(app,251+ { url: '/' },252+ function(res){253+ assert.length(res.headers['set-cookie'], 1);254+ assert.length(255+ 'rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly',256+ res.headers['set-cookie'][0]);257+ });258 }259 };