diff --git a/__tests__/response/back.js b/__tests__/response/back.js new file mode 100644 index 000000000..cd28fd94f --- /dev/null +++ b/__tests__/response/back.js @@ -0,0 +1,32 @@ +'use strict' + +const assert = require('assert') +const context = require('../../test-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, '/') + }) +}) diff --git a/lib/response.js b/lib/response.js index a4e18489e..5cda6ce60 100644 --- a/lib/response.js +++ b/lib/response.js @@ -18,7 +18,7 @@ const only = require('./only.js') const util = require('util') const encodeUrl = require('encodeurl') const Stream = require('stream') -const deprecate = require('depd')('koa'); +const deprecate = require('depd')('koa') /** * Prototype. @@ -296,8 +296,8 @@ module.exports = { // location if (url === 'back') { 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 || '/'; + 'consider migrating usage to ctx.back() instead.') + url = this.ctx.get('Referrer') || alt || '/' } if (/^https?:\/\//i.test(url)) { // formatting url again avoid security escapes @@ -334,9 +334,9 @@ module.exports = { * @api public */ - back(alt) { - const url = this.ctx.get('Referrer') || alt || '/'; - this.redirect(url); + back (alt) { + const url = this.ctx.get('Referrer') || alt || '/' + this.redirect(url) }, /** diff --git a/test/response/back.js b/test/response/back.js deleted file mode 100644 index e6c30c378..000000000 --- a/test/response/back.js +++ /dev/null @@ -1,33 +0,0 @@ - -'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, '/'); - }); -});