Skip to content

Commit f42b160

Browse files
jonchurchctcpip
andauthored
[v4] Deprecate res.clearCookie accepting options.maxAge and options.expires (#5672)
* add deprecation notice for res.clearCookie maxAge/expires * update History.md for clearCookie deprecation change * add tests to codify deprecated behavior Co-authored-by: Chris de Almeida <[email protected]> --------- Co-authored-by: Chris de Almeida <[email protected]>
1 parent 689073d commit f42b160

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Diff for: History.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ unreleased
33

44
* deps: encodeurl@~2.0.0
55
- Removes encoding of `\`, `|`, and `^` to align better with URL spec
6+
* Deprecate passing `options.maxAge` and `options.expires` to `res.clearCookie`
7+
- Will be ignored in v5, clearCookie will set a cookie with an expires in the past to instruct clients to delete the cookie
68

79
4.19.2 / 2024-03-25
810
==========

Diff for: lib/response.js

+8
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,14 @@ res.get = function(field){
822822
*/
823823

824824
res.clearCookie = function clearCookie(name, options) {
825+
if (options) {
826+
if (options.maxAge) {
827+
deprecate('res.clearCookie: Passing "options.maxAge" is deprecated. In v5.0.0 of Express, this option will be ignored, as res.clearCookie will automatically set cookies to expire immediately. Please update your code to omit this option.');
828+
}
829+
if (options.expires) {
830+
deprecate('res.clearCookie: Passing "options.expires" is deprecated. In v5.0.0 of Express, this option will be ignored, as res.clearCookie will automatically set cookies to expire immediately. Please update your code to omit this option.');
831+
}
832+
}
825833
var opts = merge({ expires: new Date(1), path: '/' }, options);
826834

827835
return this.cookie(name, '', opts);

Diff for: test/res.clearCookie.js

+32
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,37 @@ describe('res', function(){
3232
.expect('Set-Cookie', 'sid=; Path=/admin; Expires=Thu, 01 Jan 1970 00:00:00 GMT')
3333
.expect(200, done)
3434
})
35+
36+
it('should set expires when passed', function(done) {
37+
var expiresAt = new Date()
38+
var app = express();
39+
40+
app.use(function(req, res){
41+
res.clearCookie('sid', { expires: expiresAt }).end();
42+
});
43+
44+
request(app)
45+
.get('/')
46+
.expect('Set-Cookie', 'sid=; Path=/; Expires=' + expiresAt.toUTCString() )
47+
.expect(200, done)
48+
})
49+
50+
it('should set both maxAge and expires when passed', function(done) {
51+
var maxAgeInMs = 10000
52+
var expiresAt = new Date()
53+
var expectedExpires = new Date(expiresAt.getTime() + maxAgeInMs)
54+
var app = express();
55+
56+
app.use(function(req, res){
57+
res.clearCookie('sid', { expires: expiresAt, maxAge: maxAgeInMs }).end();
58+
});
59+
60+
request(app)
61+
.get('/')
62+
// yes, this is the behavior. When we set a max-age, we also set expires to a date 10 sec ahead of expires
63+
// even if we set max-age only, we will also set an expires 10 sec in the future
64+
.expect('Set-Cookie', 'sid=; Max-Age=10; Path=/; Expires=' + expectedExpires.toUTCString())
65+
.expect(200, done)
66+
})
3567
})
3668
})

0 commit comments

Comments
 (0)