| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More; |
| 7 |
use Plack::Test; |
| 8 |
use HTTP::Request::Common; |
| 9 |
use Ref::Util qw<is_coderef>; |
| 10 |
|
| 11 |
# Test to ensure that a longjump out of a template, caused by a call to redirect, |
| 12 |
# does not cause any future problems rendering a template. Whilst this test is |
| 13 |
# slightly obscure for reasons of simplicity, the exact use-case is in the use |
| 14 |
# of Plugin::LogReport. If it catches a fatal error (which could occur during |
| 15 |
# the render of a template) then it will redirect to another page. |
| 16 |
|
| 17 |
{ |
| 18 |
package MyApp; |
| 19 |
|
| 20 |
use Dancer2; |
| 21 |
|
| 22 |
set template => 'template_toolkit'; |
| 23 |
|
| 24 |
# Set up 2 routes to the same template. The first route will redirect when |
| 25 |
# the template renders, the second will not |
| 26 |
get '/redirect' => sub { |
| 27 |
my $redir = sub { |
| 28 |
redirect "somewhere"; |
| 29 |
}; |
| 30 |
template 'redirect', |
| 31 |
{ redirect_sub => $redir } |
| 32 |
}; |
| 33 |
|
| 34 |
get '/noredirect' => sub { |
| 35 |
template 'redirect'; |
| 36 |
}; |
| 37 |
} |
| 38 |
|
| 39 |
my $app = Dancer2->psgi_app; |
| 40 |
ok( is_coderef($app), 'Got app' ); |
| 41 |
|
| 42 |
test_psgi $app, sub { |
| 43 |
my $cb = shift; |
| 44 |
my $res = $cb->(GET '/redirect'); |
| 45 |
is $res->code, 302, 'Redirect template results in redirect'; |
| 46 |
|
| 47 |
$res = $cb->(GET '/noredirect'); |
| 48 |
is $res->code, 200, 'Successful subsequent request to normal template'; |
| 49 |
like $res->content, qr/foobar/, 'Correct content'; |
| 50 |
}; |
| 51 |
|
| 52 |
done_testing(); |