Skip to content

Commit

Permalink
Delete back as a magic string (#5933)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey authored Sep 10, 2024
1 parent 6c98f80 commit bdd81f8
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 76 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ unreleased
* will throw a `RangeError: Invalid status code: ${code}. Status code must be greater than 99 and less than 1000.` for inputs outside this range
* will throw a `TypeError: Invalid status code: ${code}. Status code must be an integer.` for non integer inputs
* deps: [email protected]
* `res.redirect('back')` and `res.location('back')` is no longer a supported magic string, explicitly use `req.get('Referrer') || '/'`.
* change:
- `res.clearCookie` will ignore user provided `maxAge` and `expires` options
* deps: cookie-signature@^1.2.1
Expand Down
2 changes: 1 addition & 1 deletion examples/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ app.post('/login', function (req, res, next) {
req.session.success = 'Authenticated as ' + user.name
+ ' click to <a href="/logout">logout</a>. '
+ ' You may now access <a href="/restricted">/restricted</a>.';
res.redirect('back');
res.redirect(req.get('Referrer') || '/');
});
} else {
req.session.error = 'Authentication failed, please check your '
Expand Down
4 changes: 2 additions & 2 deletions examples/cookies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ app.get('/', function(req, res){

app.get('/forget', function(req, res){
res.clearCookie('remember');
res.redirect('back');
res.redirect(req.get('Referrer') || '/');
});

app.post('/', function(req, res){
Expand All @@ -43,7 +43,7 @@ app.post('/', function(req, res){
res.cookie('remember', 1, { maxAge: minute })
}

res.redirect('back');
res.redirect(req.get('Referrer') || '/');
});

/* istanbul ignore next */
Expand Down
2 changes: 1 addition & 1 deletion examples/route-separation/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ exports.update = function(req, res){
var user = req.body.user;
req.user.name = user.name;
req.user.email = user.email;
res.redirect('back');
res.redirect(req.get('Referrer') || '/');
};
15 changes: 1 addition & 14 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,26 +785,13 @@ res.cookie = function (name, value, options) {
*/

res.location = function location(url) {
var loc;

// "back" is an alias for the referrer
if (url === 'back') {
loc = this.req.get('Referrer') || '/';
} else {
loc = String(url);
}

return this.set('Location', encodeUrl(loc));
return this.set('Location', encodeUrl(url));
};

/**
* Redirect to the given `url` with optional response `status`
* defaulting to 302.
*
* The resulting `url` is determined by `res.location()`, so
* it will play nicely with mounted apps, relative paths,
* `"back"` etc.
*
* Examples:
*
* res.redirect('/foo/bar');
Expand Down
58 changes: 0 additions & 58 deletions test/res.location.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,64 +46,6 @@ describe('res', function(){
.expect(200, done)
})

describe('when url is "back"', function () {
it('should set location from "Referer" header', function (done) {
var app = express()

app.use(function (req, res) {
res.location('back').end()
})

request(app)
.get('/')
.set('Referer', '/some/page.html')
.expect('Location', '/some/page.html')
.expect(200, done)
})

it('should set location from "Referrer" header', function (done) {
var app = express()

app.use(function (req, res) {
res.location('back').end()
})

request(app)
.get('/')
.set('Referrer', '/some/page.html')
.expect('Location', '/some/page.html')
.expect(200, done)
})

it('should prefer "Referrer" header', function (done) {
var app = express()

app.use(function (req, res) {
res.location('back').end()
})

request(app)
.get('/')
.set('Referer', '/some/page1.html')
.set('Referrer', '/some/page2.html')
.expect('Location', '/some/page2.html')
.expect(200, done)
})

it('should set the header to "/" without referrer', function (done) {
var app = express()

app.use(function (req, res) {
res.location('back').end()
})

request(app)
.get('/')
.expect('Location', '/')
.expect(200, done)
})
})

it('should encode data uri1', function (done) {
var app = express()
app.use(function (req, res) {
Expand Down

0 comments on commit bdd81f8

Please sign in to comment.