| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More tests => 2; |
| 7 |
use Plack::Test; |
| 8 |
use Plack::Builder; |
| 9 |
use HTTP::Request::Common; |
| 10 |
|
| 11 |
{ |
| 12 |
package App; |
| 13 |
use Dancer2; |
| 14 |
|
| 15 |
get '/root' => sub { redirect '/'; }; |
| 16 |
get '/leading_slash' => sub { redirect '/expected'; }; |
| 17 |
get '/relative' => sub { redirect 'expected'; }; |
| 18 |
get '/relative/one-dot' => sub { redirect './expected'; }; |
| 19 |
get '/relative/two-dots' => sub { redirect '../expected'; }; |
| 20 |
get '/absolute' => sub { redirect 'http://expected' }; |
| 21 |
get '/schemeless' => sub { redirect '//expected' }; |
| 22 |
} |
| 23 |
|
| 24 |
my $app = builder { |
| 25 |
mount '/' => App->to_app(); |
| 26 |
mount '/other-mount-point' => App->to_app(); |
| 27 |
}; |
| 28 |
|
| 29 |
my @leading_slash_tests = ( |
| 30 |
# the expected result (last element) needs the mount prepended in the actual tests |
| 31 |
[ 'Relative with leading slash', '/leading_slash', '/expected' ], |
| 32 |
[ 'Relative root', '/root', '/' ], # Test for issue #1611 |
| 33 |
); |
| 34 |
|
| 35 |
my @common_tests = ( |
| 36 |
[ 'Relative redirect', '/relative', 'expected' ], |
| 37 |
[ 'Relative redirect with ./', '/relative/one-dot', './expected' ], |
| 38 |
[ 'Relative redirect with ../', '/relative/two-dots', '../expected' ], |
| 39 |
[ 'Absolute URL redirect', '/absolute', 'http://expected' ], |
| 40 |
[ 'Schemeless redirect', '/schemeless', '//expected' ], |
| 41 |
); |
| 42 |
|
| 43 |
subtest 'Testing app mounted to /' => sub { |
| 44 |
my $mount = ''; |
| 45 |
|
| 46 |
test_psgi $app, sub { |
| 47 |
my $cb = shift; |
| 48 |
|
| 49 |
for my $test (@leading_slash_tests) { |
| 50 |
my ($name, $url, $expected) = @$test; |
| 51 |
subtest $name => sub { |
| 52 |
my $res = $cb->( GET $url ); |
| 53 |
is($res->code, 302, 'Correct code'); |
| 54 |
is( |
| 55 |
$res->headers->header('Location'), |
| 56 |
$mount . $expected, |
| 57 |
'Correct location header' |
| 58 |
); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
foreach my $test (@common_tests) { |
| 63 |
my ($name, $url, $expected) = @$test; |
| 64 |
subtest $name => sub { |
| 65 |
my $res = $cb->( GET $url ); |
| 66 |
is($res->code, 302, 'Correct code'); |
| 67 |
is( |
| 68 |
$res->headers->header('Location'), |
| 69 |
$expected, |
| 70 |
'Correct location header' |
| 71 |
); |
| 72 |
} |
| 73 |
} |
| 74 |
}; |
| 75 |
}; |
| 76 |
|
| 77 |
subtest 'Testing app mounted to /other-mount-point' => sub { |
| 78 |
my $mount = '/other-mount-point'; |
| 79 |
|
| 80 |
test_psgi $app, sub { |
| 81 |
my $cb = shift; |
| 82 |
|
| 83 |
for my $test (@leading_slash_tests) { |
| 84 |
my ($name, $url, $expected) = @$test; |
| 85 |
$url = "$mount$url"; |
| 86 |
subtest $name => sub { |
| 87 |
my $res = $cb->( GET $url ); |
| 88 |
is($res->code, 302, 'Correct code'); |
| 89 |
is( |
| 90 |
$res->headers->header('Location'), |
| 91 |
"$mount$expected", # scriptname prepended to redirect path |
| 92 |
'Correct location header' |
| 93 |
); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
foreach my $test (@common_tests) { |
| 98 |
my ($name, $url, $expected) = @$test; |
| 99 |
$url = "$mount$url"; |
| 100 |
subtest $name => sub { |
| 101 |
my $res = $cb->( GET $url ); |
| 102 |
is($res->code, 302, 'Correct code'); |
| 103 |
is( |
| 104 |
$res->headers->header('Location'), |
| 105 |
$expected, |
| 106 |
'Correct location header' |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
}; |
| 111 |
}; |
| 112 |
|
| 113 |
note 'DONE!'; |