1use strict;
2use warnings;
3use Test::More;
4use Plack::Test;
5use HTTP::Request::Common;
6use Ref::Util qw<is_coderef>;
7
8subtest '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->server,
44                "Perl Dancer2 " . Dancer2->VERSION,
45                '[/halt] Correct Server header',
46            );
47
48            is(
49                $res->headers->header('X-Foo'),
50                'foo',
51                '[/halt] Correct X-Foo header',
52            );
53        }
54    };
55
56};
57
58subtest 'halt in before hook' => sub {
59    {
60        package App;
61        use Dancer2;
62
63        hook before => sub {
64            response->content('I was halted');
65            halt if request->path eq '/shortcircuit';
66        };
67
68    }
69
70    my $app = App->to_app;
71    ok( is_coderef($app), 'Got app' );
72
73    test_psgi $app, sub {
74        my $cb  = shift;
75        my $res = $cb->( GET '/shortcircuit' );
76
77        is( $res->code, 200, '[/shortcircuit] Correct code with before hook' );
78        is(
79            $res->content,
80            'I was halted',
81            '[/shortcircuit] Correct content with before hook',
82        );
83    };
84};
85
86done_testing;
87