@@ -8,6 +8,26 @@ var methods = {
88 get_restify_error : function ( req , res , next ) {
99 var error = new restify . InvalidArgumentError ( 'foo arg invalid' ) ;
1010 return responder . error ( res , error , next ) ;
11+ } ,
12+
13+ redirect_to_full_url : function ( req , res , next ) {
14+ var args = { url : 'http://google.com' } ;
15+ return responder . redirect ( req , res , args , next ) ;
16+ } ,
17+
18+ redirect_to_full_url_301 : function ( req , res , next ) {
19+ var args = { url : 'http://google.com' , status : 301 } ;
20+ return responder . redirect ( req , res , args , next ) ;
21+ } ,
22+
23+ redirect_to_full_path : function ( req , res , next ) {
24+ var args = { url : 'foo/bar' } ;
25+ return responder . redirect ( req , res , args , next ) ;
26+ } ,
27+
28+ redirect_to_relative_path : function ( req , res , next ) {
29+ var args = { url : './foo/bar' } ;
30+ return responder . redirect ( req , res , args , next ) ;
1131 }
1232} ;
1333
@@ -17,17 +37,43 @@ var routes = [
1737 url : "/test/errors/restify" ,
1838 func : methods . get_restify_error ,
1939 middleware : [ ]
40+ } ,
41+ {
42+ method : "get" ,
43+ url : "/test/redirects/full_url" ,
44+ func : methods . redirect_to_full_url ,
45+ middleware : [ ]
46+ } ,
47+ {
48+ method : "get" ,
49+ url : "/test/redirects/full_url_301" ,
50+ func : methods . redirect_to_full_url_301 ,
51+ middleware : [ ]
52+ } ,
53+ {
54+ method : "get" ,
55+ url : "/test/redirects/full_path" ,
56+ func : methods . redirect_to_full_path ,
57+ middleware : [ ]
58+ } ,
59+ {
60+ method : "get" ,
61+ url : "/test/redirects/relative_path" ,
62+ func : methods . redirect_to_relative_path ,
63+ middleware : [ ]
2064 }
2165] ;
2266
2367describe ( "functional - server/responder.js" , function ( ) {
2468 var server ;
2569 var http_client ;
70+ var http_string_client ;
2671
2772 before ( function ( ) {
2873 server = http . server . create ( routes ) ;
2974 server . start ( ) ;
3075 http_client = http . client ( ) ;
76+ http_string_client = http . string_client ( ) ;
3177 } ) ;
3278
3379 after ( function ( ) {
@@ -50,4 +96,51 @@ describe("functional - server/responder.js", function () {
5096 } ) ;
5197 } ) ;
5298 } ) ;
99+
100+ describe ( "redirect()" , function ( ) {
101+ context ( "when full URL passed in" , function ( ) {
102+ context ( "and no status passed in" , function ( ) {
103+ it ( "redirects to the full URL with status 302" , function ( done ) {
104+ http_client . get ( '/test/redirects/full_url' , function ( err , result , raw_res ) {
105+ assert . equal ( raw_res . headers . location , 'http://google.com' ) ;
106+ assert . equal ( raw_res . statusCode , 302 ) ;
107+ done ( ) ;
108+ } ) ;
109+ } ) ;
110+ } ) ;
111+
112+ context ( "and status 301 passed in" , function ( ) {
113+ it ( "redirects to the full URL with status 301" , function ( done ) {
114+ http_client . get ( '/test/redirects/full_url_301' , function ( err , result , raw_res ) {
115+ assert . equal ( raw_res . headers . location , 'http://google.com' ) ;
116+ assert . equal ( raw_res . statusCode , 301 ) ;
117+ done ( ) ;
118+ } ) ;
119+ } ) ;
120+ } ) ;
121+ } ) ;
122+
123+ context ( "when full path passed in without host and port" , function ( ) {
124+ it ( "redirects to the correct URL using request's host and port" , function ( done ) {
125+ http_client . get ( '/test/redirects/full_path' , function ( err , result , raw_res ) {
126+ var expected_location = http . host + ':' + http . port + '/foo/bar' ;
127+ assert . equal ( raw_res . headers . location , expected_location ) ;
128+ assert . equal ( raw_res . statusCode , 302 ) ;
129+ done ( ) ;
130+ } ) ;
131+ } ) ;
132+ } ) ;
133+
134+ context ( "when relative path passed in" , function ( ) {
135+ it ( "redirects to the correct URL using request's host and port" , function ( done ) {
136+ var starting_path = '/test/redirects/relative_path' ;
137+ http_client . get ( starting_path , function ( err , result , raw_res ) {
138+ var expected_location = http . host + ':' + http . port + starting_path + '/foo/bar' ;
139+ assert . equal ( raw_res . headers . location , expected_location ) ;
140+ assert . equal ( raw_res . statusCode , 302 ) ;
141+ done ( ) ;
142+ } ) ;
143+ } ) ;
144+ } ) ;
145+ } ) ;
53146} ) ;
0 commit comments