11'use strict'
22
33var express = require ( '../' )
4- , request = require ( 'supertest' ) ;
4+ , request = require ( 'supertest' )
5+ , url = require ( 'url' ) ;
56
67describe ( 'res' , function ( ) {
78 describe ( '.location(url)' , function ( ) {
89 it ( 'should set the header' , function ( done ) {
910 var app = express ( ) ;
1011
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+
1125 app . use ( function ( req , res ) {
1226 res . location ( 'http://google.com' ) . end ( ) ;
1327 } ) ;
@@ -31,6 +45,36 @@ describe('res', function(){
3145 . expect ( 200 , done )
3246 } )
3347
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+
3478 it ( 'should not touch already-encoded sequences in "url"' , function ( done ) {
3579 var app = express ( )
3680
0 commit comments