| 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 |
# Tests for obscure rendering situations that may occur whilst undertaking |
| 12 |
# template processing |
| 13 |
|
| 14 |
{ |
| 15 |
package MyApp; |
| 16 |
|
| 17 |
use Dancer2; |
| 18 |
|
| 19 |
set template => 'template_toolkit'; |
| 20 |
|
| 21 |
hook on_route_exception => sub { |
| 22 |
status 200; |
| 23 |
send_as(plain => "Some plain text"); |
| 24 |
}; |
| 25 |
|
| 26 |
get '/bork' => sub { |
| 27 |
my $bork = sub { |
| 28 |
die "I've borked in the template"; |
| 29 |
}; |
| 30 |
template 'exec', |
| 31 |
{ exec_sub => $bork } |
| 32 |
}; |
| 33 |
|
| 34 |
get '/plain' => sub { |
| 35 |
my $plain = sub { |
| 36 |
send_as(plain => "Some plain text"); |
| 37 |
}; |
| 38 |
template 'exec', |
| 39 |
{ exec_sub => $plain } |
| 40 |
}; |
| 41 |
|
| 42 |
get '/html' => sub { |
| 43 |
my $html = sub { |
| 44 |
send_as(html => "<p>HTML text</p>"); |
| 45 |
}; |
| 46 |
template 'exec', |
| 47 |
{ exec_sub => $html } |
| 48 |
}; |
| 49 |
} |
| 50 |
|
| 51 |
my $app = Dancer2->psgi_app; |
| 52 |
ok( is_coderef($app), 'Got app' ); |
| 53 |
|
| 54 |
test_psgi $app, sub { |
| 55 |
my $cb = shift; |
| 56 |
|
| 57 |
my $res = $cb->(GET '/bork'); |
| 58 |
is $res->code, 200, 'Correct status code when overriding exception handling'; |
| 59 |
like $res->content, qr/plain text/, 'Correct content when overriding exception handling'; |
| 60 |
|
| 61 |
$res = $cb->(GET '/plain'); |
| 62 |
is $res->code, 200, 'Correct status when sending as plain during template render'; |
| 63 |
like $res->content, qr/plain text/, 'Correct content when sending as plain'; |
| 64 |
|
| 65 |
$res = $cb->(GET '/html'); |
| 66 |
is $res->code, 200, 'Correct status when sending as HTML during template render'; |
| 67 |
like $res->content, qr/HTML text/, 'Correct content when sending as HTML'; |
| 68 |
}; |
| 69 |
|
| 70 |
done_testing(); |