| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Plack::Test; |
| 5 |
use HTTP::Request::Common; |
| 6 |
use Ref::Util qw<is_coderef>; |
| 7 |
|
| 8 |
subtest 'halt with parameter within routes' => sub { |
| 9 |
{ |
| 10 |
|
| 11 |
package App; |
| 12 |
use Dancer2; |
| 13 |
|
| 14 |
get '/' => sub { 'hello' }; |
| 15 |
get '/halt' => sub { |
| 16 |
response_header 'X-Foo' => 'foo'; |
| 17 |
halt; |
| 18 |
}; |
| 19 |
get '/shortcircuit' => sub { |
| 20 |
halt('halted'); |
| 21 |
redirect '/'; # won't get executed as halt returns immediately. |
| 22 |
}; |
| 23 |
} |
| 24 |
|
| 25 |
my $app = App->to_app; |
| 26 |
ok( is_coderef($app), 'Got app' ); |
| 27 |
|
| 28 |
test_psgi $app, sub { |
| 29 |
my $cb = shift; |
| 30 |
|
| 31 |
{ |
| 32 |
my $res = $cb->( GET '/shortcircuit' ); |
| 33 |
is( $res->code, 200, '[/shortcircuit] Correct status' ); |
| 34 |
is( $res->content, 'halted', '[/shortcircuit] Correct content' ); |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
{ |
| 39 |
my $res = $cb->( GET '/halt' ); |
| 40 |
|
| 41 |
is( |
| 42 |
$res->headers->header('X-Foo'), |
| 43 |
'foo', |
| 44 |
'[/halt] Correct X-Foo header', |
| 45 |
); |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
}; |
| 50 |
|
| 51 |
subtest 'halt with parameter in before hook' => sub { |
| 52 |
{ |
| 53 |
package App; |
| 54 |
use Dancer2; |
| 55 |
|
| 56 |
hook before => sub { |
| 57 |
halt('I was halted') if request->path eq '/shortcircuit'; |
| 58 |
}; |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
my $app = App->to_app; |
| 63 |
ok( is_coderef($app), 'Got app' ); |
| 64 |
|
| 65 |
test_psgi $app, sub { |
| 66 |
my $cb = shift; |
| 67 |
my $res = $cb->( GET '/shortcircuit' ); |
| 68 |
|
| 69 |
is( $res->code, 200, '[/shortcircuit] Correct code with before hook' ); |
| 70 |
is( |
| 71 |
$res->content, |
| 72 |
'I was halted', |
| 73 |
'[/shortcircuit] Correct content with before hook', |
| 74 |
); |
| 75 |
}; |
| 76 |
}; |
| 77 |
|
| 78 |
done_testing; |