File tree 3 files changed +57
-1
lines changed
3 files changed +57
-1
lines changed Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ const assert = require ( 'assert' )
4
+ const context = require ( '../../test-helpers/context' )
5
+
6
+ describe ( 'ctx.back([alt])' , ( ) => {
7
+ it ( 'should redirect to Referrer' , ( ) => {
8
+ const ctx = context ( )
9
+ ctx . req . headers . referrer = '/login'
10
+ ctx . back ( )
11
+ assert . equal ( ctx . response . header . location , '/login' )
12
+ } )
13
+
14
+ it ( 'should redirect to Referer' , ( ) => {
15
+ const ctx = context ( )
16
+ ctx . req . headers . referer = '/login'
17
+ ctx . back ( )
18
+ assert . equal ( ctx . response . header . location , '/login' )
19
+ } )
20
+
21
+ it ( 'should default to alt' , ( ) => {
22
+ const ctx = context ( )
23
+ ctx . back ( '/index.html' )
24
+ assert . equal ( ctx . response . header . location , '/index.html' )
25
+ } )
26
+
27
+ it ( 'should default redirect to /' , ( ) => {
28
+ const ctx = context ( )
29
+ ctx . back ( )
30
+ assert . equal ( ctx . response . header . location , '/' )
31
+ } )
32
+ } )
Original file line number Diff line number Diff line change @@ -204,6 +204,7 @@ delegate(proto, 'response')
204
204
. method ( 'set' )
205
205
. method ( 'append' )
206
206
. method ( 'flushHeaders' )
207
+ . method ( 'back' )
207
208
. access ( 'status' )
208
209
. access ( 'message' )
209
210
. access ( 'body' )
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ const only = require('./only.js')
18
18
const util = require ( 'util' )
19
19
const encodeUrl = require ( 'encodeurl' )
20
20
const Stream = require ( 'stream' )
21
+ const deprecate = require ( 'depd' ) ( 'koa' )
21
22
22
23
/**
23
24
* Prototype.
@@ -293,7 +294,11 @@ module.exports = {
293
294
294
295
redirect ( url , alt ) {
295
296
// location
296
- if ( url === 'back' ) url = this . ctx . get ( 'Referrer' ) || alt || '/'
297
+ if ( url === 'back' ) {
298
+ deprecate ( 'Special-cased string "back" through redirect will be removed in v3, ' +
299
+ 'consider migrating usage to ctx.back() instead.' )
300
+ url = this . ctx . get ( 'Referrer' ) || alt || '/'
301
+ }
297
302
if ( / ^ h t t p s ? : \/ \/ / i. test ( url ) ) {
298
303
// formatting url again avoid security escapes
299
304
url = new URL ( url ) . toString ( )
@@ -316,6 +321,24 @@ module.exports = {
316
321
this . body = `Redirecting to ${ url } .`
317
322
} ,
318
323
324
+ /**
325
+ * Perform a special-cased "back" to provide Referrer support.
326
+ * When Referrer is not present, `alt` or "/" is used.
327
+ *
328
+ * Examples:
329
+ *
330
+ * ctx.back()
331
+ * ctx.back('/index.html')
332
+ *
333
+ * @param {String } [alt]
334
+ * @api public
335
+ */
336
+
337
+ back ( alt ) {
338
+ const url = this . ctx . get ( 'Referrer' ) || alt || '/'
339
+ this . redirect ( url )
340
+ } ,
341
+
319
342
/**
320
343
* Set Content-Disposition header to "attachment" with optional `filename`.
321
344
*
You can’t perform that action at this time.
0 commit comments