Skip to content

Commit

Permalink
restore breaking changes, add depd message
Browse files Browse the repository at this point in the history
  • Loading branch information
fl0w committed Feb 14, 2018
1 parent 4b3b502 commit b340875
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const assert = require('assert');
const extname = require('path').extname;
const vary = require('vary');
const only = require('only');
const deprecate = require('depd')('koa');

/**
* Prototype.
Expand Down Expand Up @@ -240,6 +241,10 @@ module.exports = {
/**
* Perform a 302 redirect to `url`.
*
* The string "back" is special-cased
* to provide Referrer support, when Referrer
* is not present `alt` or "/" is used.
*
* Examples:
*
* this.redirect('/login');
Expand All @@ -249,8 +254,13 @@ module.exports = {
* @api public
*/

redirect(url) {
redirect(url, alt) {
// location
if ('back' == url) {
deprecate('Special-cased string "back" through redirect will be removed in v3,' +
'consider migrating usage to ctx.back() instead.')
url = this.ctx.get('Referrer') || alt || '/';
}
this.set('Location', url);

// status
Expand Down
28 changes: 28 additions & 0 deletions test/response/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,34 @@ describe('ctx.redirect(url)', () => {
assert.equal(ctx.status, 302);
});

describe('with "back"', () => {
it('should redirect to Referrer', () => {
const ctx = context();
ctx.req.headers.referrer = '/login';
ctx.redirect('back');
assert.equal(ctx.response.header.location, '/login');
});

it('should redirect to Referer', () => {
const ctx = context();
ctx.req.headers.referer = '/login';
ctx.redirect('back');
assert.equal(ctx.response.header.location, '/login');
});

it('should default to alt', () => {
const ctx = context();
ctx.redirect('back', '/index.html');
assert.equal(ctx.response.header.location, '/index.html');
});

it('should default redirect to /', () => {
const ctx = context();
ctx.redirect('back');
assert.equal(ctx.response.header.location, '/');
});
});

describe('when html is accepted', () => {
it('should respond with html', () => {
const ctx = context();
Expand Down

0 comments on commit b340875

Please sign in to comment.