| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
use Plack::Test; |
| 6 |
use Plack::Builder; |
| 7 |
use HTTP::Request::Common; |
| 8 |
use HTTP::Request; |
| 9 |
|
| 10 |
# Each concern gets its own Dancer2 application, because routes are registered |
| 11 |
# per package and mixing them would make the match order hard to follow. |
| 12 |
# 'logger => null' keeps the suite quiet; these tests assert on responses, not |
| 13 |
# on log output. |
| 14 |
|
| 15 |
{ |
| 16 |
package FlowPass; |
| 17 |
use Dancer2; |
| 18 |
set logger => 'null'; |
| 19 |
|
| 20 |
our @trace; |
| 21 |
|
| 22 |
# Matches first, hands off to the next route via pass(). |
| 23 |
get '/x/*' => sub { |
| 24 |
push @trace, 'splat=[' . join( ',', splat() ) . ']'; |
| 25 |
response->content('CONTENT-FROM-FIRST-ROUTE'); |
| 26 |
pass; |
| 27 |
}; |
| 28 |
|
| 29 |
# Deliberately has no wildcard of its own, so anything splat-shaped it |
| 30 |
# sees must have leaked from the route above. |
| 31 |
get '/x/one' => sub { |
| 32 |
push @trace, 'param_keys=[' . join( ',', sort keys %{ params() } ) . ']'; |
| 33 |
push @trace, 'content=[' . ( response->content // 'undef' ) . ']'; |
| 34 |
return 'SECOND'; |
| 35 |
}; |
| 36 |
} |
| 37 |
|
| 38 |
{ |
| 39 |
package FlowHalt; |
| 40 |
use Dancer2; |
| 41 |
set logger => 'null'; |
| 42 |
|
| 43 |
our @trace; |
| 44 |
|
| 45 |
hook after => sub { push @trace, 'after-hook' }; |
| 46 |
|
| 47 |
get '/halt' => sub { |
| 48 |
halt('HALTED-BODY'); |
| 49 |
push @trace, 'code-after-halt'; |
| 50 |
return 'NOT-THIS'; |
| 51 |
}; |
| 52 |
|
| 53 |
get '/normal' => sub { 'NORMAL' }; |
| 54 |
} |
| 55 |
|
| 56 |
{ |
| 57 |
package FlowForward; |
| 58 |
use Dancer2; |
| 59 |
set logger => 'null'; |
| 60 |
|
| 61 |
post '/plain' => sub { forward '/dst' }; |
| 62 |
post '/with-params' => sub { forward '/dst', { added => 'yes' } }; |
| 63 |
post '/as-get' => sub { forward '/dst', { added => 'yes' }, { method => 'GET' } }; |
| 64 |
|
| 65 |
# The body is reported through both accessors on purpose. They are carried |
| 66 |
# across a forward by two different lines of the request-cloning code, so |
| 67 |
# checking only one of them leaves the other free to regress unnoticed. |
| 68 |
sub _report { |
| 69 |
my $method = shift; |
| 70 |
join ' ', $method, |
| 71 |
'name=' . ( body_parameters->get('name') // 'undef' ), |
| 72 |
'bodyname=' . ( params('body')->{name} // 'undef' ), |
| 73 |
'added=' . ( params->{added} // 'undef' ); |
| 74 |
} |
| 75 |
|
| 76 |
post '/dst' => sub { _report('POST') }; |
| 77 |
get '/dst' => sub { _report('GET') }; |
| 78 |
} |
| 79 |
|
| 80 |
{ |
| 81 |
package FlowRedirect; |
| 82 |
use Dancer2; |
| 83 |
set logger => 'null'; |
| 84 |
set session => 'Simple'; |
| 85 |
|
| 86 |
get '/go' => sub { redirect '/target' }; |
| 87 |
get '/go-abs' => sub { redirect 'http://elsewhere.example/x' }; |
| 88 |
get '/go-301' => sub { redirect '/target', 301 }; |
| 89 |
get '/target' => sub { 'TARGET' }; |
| 90 |
|
| 91 |
get '/set-then-forward' => sub { session foo => 'bar'; forward '/read' }; |
| 92 |
get '/read' => sub { 'foo=' . ( session('foo') // 'undef' ) }; |
| 93 |
} |
| 94 |
|
| 95 |
subtest 'pass hands off without leaking state into the next route' => sub { |
| 96 |
my $test = Plack::Test->create( FlowPass->to_app ); |
| 97 |
|
| 98 |
@FlowPass::trace = (); |
| 99 |
my $res = $test->request( GET '/x/one' ); |
| 100 |
|
| 101 |
is( $res->code, 200, 'the second route produced the response' ); |
| 102 |
is( $res->content, 'SECOND', 'the body comes from the second route' ); |
| 103 |
|
| 104 |
is( $FlowPass::trace[0], 'splat=[one]', |
| 105 |
'the first route did capture a splat before passing' ); |
| 106 |
|
| 107 |
# These two are the point of the subtest: whatever the first route left |
| 108 |
# behind must not be visible to the second. |
| 109 |
is( $FlowPass::trace[1], 'param_keys=[]', |
| 110 |
'the first route\'s splat parameter does not leak into the second' ); |
| 111 |
is( $FlowPass::trace[2], 'content=[undef]', |
| 112 |
'the first route\'s content does not leak into the second' ); |
| 113 |
}; |
| 114 |
|
| 115 |
subtest 'halt stops the route and skips after hooks' => sub { |
| 116 |
my $test = Plack::Test->create( FlowHalt->to_app ); |
| 117 |
|
| 118 |
@FlowHalt::trace = (); |
| 119 |
my $normal = $test->request( GET '/normal' ); |
| 120 |
is( $normal->content, 'NORMAL', 'the ordinary route responds normally' ); |
| 121 |
is_deeply( \@FlowHalt::trace, ['after-hook'], |
| 122 |
'the after hook does run on an ordinary request' ); |
| 123 |
|
| 124 |
@FlowHalt::trace = (); |
| 125 |
my $halted = $test->request( GET '/halt' ); |
| 126 |
is( $halted->code, 200, 'halt keeps the status it was given' ); |
| 127 |
is( $halted->content, 'HALTED-BODY', 'the halted body is what is returned' ); |
| 128 |
|
| 129 |
# The comparison against the ordinary request above is what makes this |
| 130 |
# meaningful: the hook demonstrably fires, and demonstrably does not here. |
| 131 |
is_deeply( \@FlowHalt::trace, [], |
| 132 |
'neither the rest of the route nor the after hook runs after halt' ); |
| 133 |
}; |
| 134 |
|
| 135 |
subtest 'forward re-dispatches while preserving the request' => sub { |
| 136 |
my $test = Plack::Test->create( FlowForward->to_app ); |
| 137 |
|
| 138 |
is( |
| 139 |
$test->request( POST '/plain', [ name => 'ovid' ] )->content, |
| 140 |
'POST name=ovid bodyname=ovid added=undef', |
| 141 |
'the body survives the forward through both accessors, method unchanged', |
| 142 |
); |
| 143 |
|
| 144 |
is( |
| 145 |
$test->request( POST '/with-params', [ name => 'ovid' ] )->content, |
| 146 |
'POST name=ovid bodyname=ovid added=yes', |
| 147 |
'parameters added at forward time are visible to the target', |
| 148 |
); |
| 149 |
|
| 150 |
is( |
| 151 |
$test->request( POST '/as-get', [ name => 'ovid' ] )->content, |
| 152 |
'GET name=ovid bodyname=ovid added=yes', |
| 153 |
'method => GET reaches the GET route, still carrying the body', |
| 154 |
); |
| 155 |
}; |
| 156 |
|
| 157 |
subtest 'a session created before a forward is visible after it' => sub { |
| 158 |
my $test = Plack::Test->create( FlowRedirect->to_app ); |
| 159 |
|
| 160 |
is( $test->request( GET '/set-then-forward' )->content, 'foo=bar', |
| 161 |
'the forwarded-to route sees the session set before the forward' ); |
| 162 |
}; |
| 163 |
|
| 164 |
subtest 'redirect sets Location and honours the mount path' => sub { |
| 165 |
my $app = FlowRedirect->to_app; |
| 166 |
my $test = Plack::Test->create($app); |
| 167 |
|
| 168 |
my $res = $test->request( GET '/go' ); |
| 169 |
is( $res->code, 302, 'a redirect defaults to 302' ); |
| 170 |
is( $res->header('Location'), '/target', 'the destination is set verbatim at the root' ); |
| 171 |
|
| 172 |
is( $test->request( GET '/go-301' )->code, 301, |
| 173 |
'an explicit status is used instead of the default' ); |
| 174 |
|
| 175 |
is( |
| 176 |
$test->request( GET '/go-abs' )->header('Location'), |
| 177 |
'http://elsewhere.example/x', |
| 178 |
'an absolute URL is left alone', |
| 179 |
); |
| 180 |
|
| 181 |
# The same application, mounted somewhere other than '/'. This is the |
| 182 |
# assertion that fails if the mount path stops being prepended. |
| 183 |
my $mounted = Plack::Test->create( builder { mount '/sub' => $app; } ); |
| 184 |
is( |
| 185 |
$mounted->request( GET 'http://localhost/sub/go' )->header('Location'), |
| 186 |
'/sub/target', |
| 187 |
'a root-relative redirect is rewritten to include the mount path', |
| 188 |
); |
| 189 |
}; |
| 190 |
|
| 191 |
subtest 'an unsupported HTTP method is refused with 405' => sub { |
| 192 |
my $test = Plack::Test->create( FlowForward->to_app ); |
| 193 |
|
| 194 |
my $res = $test->request( HTTP::Request->new( 'FROB', '/dst' ) ); |
| 195 |
is( $res->code, 405, 'an unknown verb is rejected' ); |
| 196 |
like( $res->content, qr/Method Not Allowed/, 'the body says why' ); |
| 197 |
like( $res->content, qr/FROB/, 'the body names the offending method' ); |
| 198 |
|
| 199 |
is( $test->request( GET '/dst' )->code, 200, |
| 200 |
'a supported verb on the same path still works' ); |
| 201 |
}; |
| 202 |
|
| 203 |
done_testing(); |