Debian Codegraph @

t/redirect.t

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