Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated back #1146

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ delegate(proto, 'response')
.method('set')
.method('append')
.method('flushHeaders')
.method('back')
.access('status')
.access('message')
.access('body')
Expand Down
28 changes: 19 additions & 9 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,25 +240,17 @@ 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('back');
* this.redirect('back', '/index.html');
* this.redirect('/login');
* this.redirect('http://google.com');
*
* @param {String} url
* @param {String} [alt]
* @api public
*/

redirect(url, alt) {
redirect(url) {
// location
if ('back' == url) url = this.ctx.get('Referrer') || alt || '/';
this.set('Location', url);

// status
Expand All @@ -277,6 +269,24 @@ module.exports = {
this.body = `Redirecting to ${url}.`;
},

/**
* Perform a special-cased "back" to provide Referrer support.
* When Referrer is not present, `alt` or "/" is used.
*
* Examples:
*
* ctx.back()
* ctx.back('/index.html')
*
* @param {String} [alt]
* @api public
*/

back(alt) {
const url = this.ctx.get('Referrer') || alt || '/';
this.redirect(url);
},

/**
* Set Content-Disposition header to "attachment" with optional `filename`.
*
Expand Down
33 changes: 33 additions & 0 deletions test/response/back.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

'use strict';

const assert = require('assert');
const context = require('../helpers/context');

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

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

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

it('should default redirect to /', () => {
const ctx = context();
ctx.back();
assert.equal(ctx.response.header.location, '/');
});
});
28 changes: 0 additions & 28 deletions test/response/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,6 @@ 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