File: 05-plack.t

package info (click to toggle)
libhttp-exception-perl 0.04006-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 288 kB
  • ctags: 55
  • sloc: perl: 223; makefile: 2
file content (86 lines) | stat: -rwxr-xr-x 2,266 bytes parent folder | download | duplicates (3)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use strict;

use Test::More;

BEGIN {
    # Plack 0.9913 brings us Plack::Middleware::HTTPExceptions
    eval "use Plack 0.9913";
    plan skip_all => "Plack 0.9913 or newer required for this test" if $@;

    eval "use HTTP::Request::Common";
    plan skip_all => "HTTP::Request::Common required for this test" if $@;
}

use HTTP::Exception;
use Plack::Test;
use HTTP::Status;
use HTTP::Request::Common;

{
    package My::HTTP::Exception;
    use base 'HTTP::Exception::405';

    sub code    { 404 }
    sub my_info { 'Interesting Info' }
}

{
    package My::HTTP::Exception::WithStatusMessage;
    use base 'HTTP::Exception::405';

    sub code    { 404 }
    sub status_message { 'Nothing here' }
}


my @tests = ({
    path                => '/ok',
    exception           => sub { HTTP::Exception::UNAUTHORIZED->throw; },
    expected_code       => 401,
},{
    path                => '/secret',
    exception           => sub { HTTP::Exception::402->throw; },
    expected_code       => 402,
},{
    path                => '/not_found',
    exception           => sub { HTTP::Exception->throw(403); },
    expected_code       => 403,
},{
    path                => '/custom',
    exception           => sub { My::HTTP::Exception->throw; },
    expected_code       => 404,
    expected_content    => HTTP::Status::status_message(405),
},{
    path                => '/custom/with/message',
    exception           => sub { My::HTTP::Exception::WithStatusMessage->throw; },
    expected_code       => 404,
    expected_content    => 'Nothing here',
});

my $app = sub {
    my $env = shift;
    my ($found_test) = grep { $_->{path} eq $env->{PATH_INFO} } @tests;
    HTTP::Exception::500->throw unless ($found_test);
    $found_test->{exception}->();
};

use Plack::Middleware::HTTPExceptions;
$app = Plack::Middleware::HTTPExceptions->wrap($app);

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

    my $res = $cb->(GET "/");
    is $res->code,      500;
    is $res->content,   'Internal Server Error';

    for my $test (@tests) {
        my $res = $cb->(GET ($test->{path}));
        is $res->code,    $test->{expected_code};
        is $res->content, $test->{expected_content} || HTTP::Status::status_message($test->{expected_code});
    }

};


done_testing;