1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
use strict;
use warnings;
use Test::More;
use Plack::Test;
use HTTP::Request::Common;
use Ref::Util qw<is_coderef>;
subtest 'basic redirects' => sub {
{
package App1;
use Dancer2;
get '/' => sub {'home'};
get '/bounce' => sub { redirect '/' };
get '/redirect' => sub { response_header 'X-Foo' => 'foo'; redirect '/'; };
get '/redirect_querystring' => sub { redirect '/login?failed=1' };
get '/redirect_uriescaped' => sub { redirect '?foo=bar+%26+baz' };
}
my $app = App1->to_app;
ok( is_coderef($app), 'Got app' );
test_psgi $app, sub {
my $cb = shift;
{
my $res = $cb->( GET '/' );
is( $res->code, 200, '[GET /] Correct code' );
is( $res->content, 'home', '[GET /] Correct content' );
is(
$res->headers->content_type,
'text/html',
'[GET /] Correct content-type',
);
is(
$cb->( GET '/bounce' )->code,
302,
'[GET /bounce] Correct code',
);
}
{
my $res = $cb->( GET '/redirect' );
is( $res->code, 302, '[GET /redirect] Correct code' );
is(
$res->headers->header('Location'),
'/',
'Correct Location header',
);
is(
$res->headers->header('X-Foo'),
'foo',
'Correct X-Foo header',
);
}
{
my $res = $cb->( GET '/redirect_querystring' );
is( $res->code, 302, '[GET /redirect_querystring] Correct code' );
is(
$res->headers->header('Location'),
'/login?failed=1',
'Correct Location header',
);
}
{
my $res = $cb->( GET '/redirect_uriescaped' );
is( $res->code, 302, '[GET /redirect_uriescaped] Correct code' );
is(
$res->headers->header('Location'),
'?foo=bar+%26+baz',
'Correct Location header',
);
}
};
};
# redirect absolute
subtest 'absolute and relative redirects' => sub {
{
package App2;
use Dancer2;
get '/absolute_with_host' =>
sub { redirect "http://foo.com/somewhere"; };
get '/absolute' => sub { redirect "/absolute"; };
get '/relative' => sub { redirect "somewhere/else"; };
}
my $app = App2->to_app;
ok( is_coderef($app), 'Got app' );
test_psgi $app, sub {
my $cb = shift;
{
my $res = $cb->( GET '/absolute_with_host' );
is(
$res->headers->header('Location'),
'http://foo.com/somewhere',
'Correct Location header',
);
}
{
my $res = $cb->( GET '/absolute' );
is(
$res->headers->header('Location'),
'/absolute',
'Correct Location header',
);
}
{
my $res = $cb->( GET '/relative' );
is(
$res->headers->header('Location'),
'somewhere/else',
'Correct Location header',
);
}
};
};
done_testing;
|