Debian Codegraph @

t/dsl/halt.t

raw
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 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 app->response->content('halted');
21 halt;
22 redirect '/'; # won't get executed as halt returns immediately.
23 };
24 }
25
26 my $app = App->to_app;
27 ok( is_coderef($app), 'Got app' );
28
29 test_psgi $app, sub {
30 my $cb = shift;
31
32 {
33 my $res = $cb->( GET '/shortcircuit' );
34 is( $res->code, 200, '[/shortcircuit] Correct status' );
35 is( $res->content, 'halted', '[/shortcircuit] Correct content' );
36
37 }
38
39 {
40 my $res = $cb->( GET '/halt' );
41
42 is(
43 $res->headers->header('X-Foo'),
44 'foo',
45 '[/halt] Correct X-Foo header',
46 );
47 }
48 };
49
50 };
51
52 subtest 'halt in before hook' => sub {
53 {
54 package App;
55 use Dancer2;
56
57 hook before => sub {
58 response->content('I was halted');
59 halt if request->path eq '/shortcircuit';
60 };
61
62 }
63
64 my $app = App->to_app;
65 ok( is_coderef($app), 'Got app' );
66
67 test_psgi $app, sub {
68 my $cb = shift;
69 my $res = $cb->( GET '/shortcircuit' );
70
71 is( $res->code, 200, '[/shortcircuit] Correct code with before hook' );
72 is(
73 $res->content,
74 'I was halted',
75 '[/shortcircuit] Correct content with before hook',
76 );
77 };
78 };
79
80 done_testing;