File: 11_error_in_hook.t

package info (click to toggle)
libdancer-perl 1.3521%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,460 kB
  • sloc: perl: 7,436; xml: 2,211; sh: 54; makefile: 32; sql: 5
file content (56 lines) | stat: -rw-r--r-- 1,279 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use strict;
use warnings;

use Test::More tests => 10, import => ['!pass'];
use Dancer ':syntax';
use Dancer::Test;

hook before_template => sub {
    status 500;
    halt({error => "This is some error"});
};

set views => path( 't', '22_hooks', 'views' );

get '/' => sub {
    template 'index', { foo => 'baz' };
};

route_exists [ GET => '/' ];
response_content_like( [ GET => '/' ], qr/Unable to process your query/ );
response_status_is( [ GET => '/' ], 500 => "We get a 500 status" );



my $var = 5;

ok(
   hook ( after => sub { 
              $var = 42;
 } ),
   'after hook is defined'
  );

get '/error' => sub {
    send_error "FAIL";
    # should not be executed
    fail("This code should not be executed (1)");
};

route_exists [ GET => '/error' ];
response_status_is( [ GET => '/error' ], 500 => "We get a 500 status" );

is ($var, 42, "The after hook were called even after a send error");

get '/halt_me' => sub {
    halt({error => "This is some error"});
    # should not be executed
    fail("This code should not be executed (2)");
};

$var = 5;

route_exists [ GET => '/halt_me' ];
response_status_is( [ GET => '/halt_me' ], 500 => "We get a 200 status" );

is ($var, 5, "The after hook is bypassed if in a 'halt' state, as it was before version 1.3080");