1
1
'use strict'
2
2
3
3
var express = require ( '../' )
4
- , request = require ( 'supertest' ) ;
4
+ , request = require ( 'supertest' )
5
+ , url = require ( 'url' ) ;
5
6
6
7
describe ( 'res' , function ( ) {
7
8
describe ( '.location(url)' , function ( ) {
8
9
it ( 'should set the header' , function ( done ) {
9
10
var app = express ( ) ;
10
11
12
+ app . use ( function ( req , res ) {
13
+ res . location ( 'http://google.com/' ) . end ( ) ;
14
+ } ) ;
15
+
16
+ request ( app )
17
+ . get ( '/' )
18
+ . expect ( 'Location' , 'http://google.com/' )
19
+ . expect ( 200 , done )
20
+ } )
21
+
22
+ it ( 'should preserve trailing slashes when not present' , function ( done ) {
23
+ var app = express ( ) ;
24
+
11
25
app . use ( function ( req , res ) {
12
26
res . location ( 'http://google.com' ) . end ( ) ;
13
27
} ) ;
@@ -31,6 +45,36 @@ describe('res', function(){
31
45
. expect ( 200 , done )
32
46
} )
33
47
48
+ it ( 'should not encode bad "url"' , function ( done ) {
49
+ var app = express ( )
50
+
51
+ app . use ( function ( req , res ) {
52
+ // This is here to show a basic check one might do which
53
+ // would pass but then the location header would still be bad
54
+ if ( url . parse ( req . query . q ) . host !== 'google.com' ) {
55
+ res . status ( 400 ) . end ( 'Bad url' ) ;
56
+ }
57
+ res . location ( req . query . q ) . end ( ) ;
58
+ } ) ;
59
+
60
+ request ( app )
61
+ . get ( '/?q=http://google.com\\@apple.com' )
62
+ . expect ( 200 )
63
+ . expect ( 'Location' , 'http://google.com\\@apple.com' )
64
+ . end ( function ( err ) {
65
+ if ( err ) {
66
+ throw err ;
67
+ }
68
+
69
+ // This ensures that our protocol check is case insensitive
70
+ request ( app )
71
+ . get ( '/?q=HTTP://google.com\\@apple.com' )
72
+ . expect ( 200 )
73
+ . expect ( 'Location' , 'HTTP://google.com\\@apple.com' )
74
+ . end ( done )
75
+ } ) ;
76
+ } ) ;
77
+
34
78
it ( 'should not touch already-encoded sequences in "url"' , function ( done ) {
35
79
var app = express ( )
36
80
0 commit comments