File: error_template.t

package info (click to toggle)
libdancer2-perl 0.152000%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,820 kB
  • ctags: 536
  • sloc: perl: 8,034; sh: 51; makefile: 2
file content (71 lines) | stat: -rw-r--r-- 1,641 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use strict;
use warnings;

use Test::More;
use Plack::Test;
use HTTP::Request::Common;

{

    package PrettyError;

    use Dancer2;

    engine('template')->views('t/corpus/pretty');
    $ENV{DANCER_PUBLIC} = 't/corpus/pretty_public';

    get '/error' => sub {
        send_error "oh my", 505;
    };

    get '/public' => sub {
        send_error "static", 510;
    };

    get '/no_template' => sub {
        local $ENV{DANCER_PUBLIC} = undef;

        send_error "oopsie", 404;
    };

}

my $app = PrettyError->to_app;
is( ref $app, 'CODE', 'Got app' );

test_psgi $app, sub {
    my $cb = shift;

    subtest "/error" => sub {
        my $r = $cb->( GET '/error' );

        is $r->code, 505, 'send_error sets the status to 505';
        like $r->content, qr{Template selected}, 'Error message looks good';
        like $r->content, qr{message: oh my};
        like $r->content, qr{status: 505};
    };

    subtest "/public" => sub {
        my $r = $cb->( GET '/public' );

        is $r->code,    510,             'send_error sets the status to 510';
        like $r->content, qr{Static page}, 'Error message looks good';
    };

    subtest "/no_template" => sub {
        my $r = $cb->( GET '/no_template' );

        is $r->code, 404, 'send_error sets the status to 404';
        like $r->content, qr{<h1>Error 404 - Not Found</h1>},
          'Error message looks good';
    };

    subtest '404 with static template' => sub {
        my $r = $cb->( GET '/middle/of/nowhere' );

        is $r->code, 404, 'unknown route => 404';
        like $r->content, qr{you're lost}i, 'Error message looks good';
    };
};

done_testing;