File tree 3 files changed +22
-1
lines changed
3 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ const extname = require('path').extname;
19
19
const vary = require ( 'vary' ) ;
20
20
const only = require ( 'only' ) ;
21
21
const util = require ( 'util' ) ;
22
+ const encodeUrl = require ( 'encodeurl' ) ;
22
23
23
24
/**
24
25
* Prototype.
@@ -260,7 +261,7 @@ module.exports = {
260
261
redirect ( url , alt ) {
261
262
// location
262
263
if ( 'back' == url ) url = this . ctx . get ( 'Referrer' ) || alt || '/' ;
263
- this . set ( 'Location' , url ) ;
264
+ this . set ( 'Location' , encodeUrl ( url ) ) ;
264
265
265
266
// status
266
267
if ( ! statuses . redirect [ this . status ] ) this . status = 302 ;
Original file line number Diff line number Diff line change 31
31
"delegates" : " ^1.0.0" ,
32
32
"depd" : " ^1.1.2" ,
33
33
"destroy" : " ^1.0.4" ,
34
+ "encodeurl" : " ^1.0.2" ,
34
35
"error-inject" : " ^1.0.0" ,
35
36
"escape-html" : " ^1.0.3" ,
36
37
"fresh" : " ~0.5.2" ,
Original file line number Diff line number Diff line change 2
2
'use strict' ;
3
3
4
4
const assert = require ( 'assert' ) ;
5
+ const request = require ( 'supertest' ) ;
5
6
const context = require ( '../helpers/context' ) ;
7
+ const Koa = require ( '../..' ) ;
6
8
7
9
describe ( 'ctx.redirect(url)' , ( ) => {
8
10
it ( 'should redirect to the given url' , ( ) => {
@@ -12,6 +14,23 @@ describe('ctx.redirect(url)', () => {
12
14
assert . equal ( ctx . status , 302 ) ;
13
15
} ) ;
14
16
17
+ it ( 'should auto fix not encode url' , done => {
18
+ const app = new Koa ( ) ;
19
+
20
+ app . use ( ctx => {
21
+ ctx . redirect ( 'http://google.com/😓' ) ;
22
+ } ) ;
23
+
24
+ request ( app . callback ( ) )
25
+ . get ( '/' )
26
+ . end ( ( err , res ) => {
27
+ if ( err ) return done ( err ) ;
28
+ assert . equal ( res . status , 302 ) ;
29
+ assert . equal ( res . headers . location , 'http://google.com/%F0%9F%98%93' ) ;
30
+ done ( ) ;
31
+ } ) ;
32
+ } ) ;
33
+
15
34
describe ( 'with "back"' , ( ) => {
16
35
it ( 'should redirect to Referrer' , ( ) => {
17
36
const ctx = context ( ) ;
You can’t perform that action at this time.
0 commit comments