File: error_document_streaming_app.t

package info (click to toggle)
libplack-perl 1.0048-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,480 kB
  • sloc: perl: 5,288; python: 7; makefile: 4; javascript: 1
file content (88 lines) | stat: -rw-r--r-- 2,247 bytes parent folder | download | duplicates (7)
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
87
88
use strict;
use warnings;
use FindBin;
use Test::More;
use HTTP::Request::Common;
use Plack::Test;
use Plack::Builder;

$Plack::Test::Impl = undef;
my @impl = ('Server', 'MockHTTP');
sub flip_backend {
    push @impl, $Plack::Test::Impl;
    $Plack::Test::Impl = shift @impl;
}

{
    my $handler = builder {
        enable "Plack::Middleware::ErrorDocument",
            404 => "$FindBin::Bin/errors/404.html";

        sub {
            my $env = shift;
            my $status = ($env->{PATH_INFO} =~ m!status/(\d+)!)[0] || 200;
            return sub {
                my $r = shift;
                my $w = $r->([ $status, [ 'Content-Type' => 'text/plain' ]]);
                $w->write("Error: $status\n");
                $w->close;
            };
        };
    };

    test_psgi app => $handler, client => sub {
        my $cb = shift;
        {
            my $res = $cb->(GET "http://localhost/");
            is $res->code, 200;

            $res = $cb->(GET "http://localhost/status/404");
            is $res->code, 404;
            like $res->header('content_type'), qr!text/html!;
            like $res->content, qr/fancy 404/;
        }
    } while flip_backend;
}

{
    my $handler = builder {
        enable "Plack::Middleware::ErrorDocument",
            404 => "/404", subrequest => 1;

        mount '/404' => sub {
            [200, ['Content-Type' => 'text/html'], [<<ERROR]]
a
b
c

This is a fancy 404 page.
ERROR
        };

        mount '/' => sub {
            my $env = shift;
            my $status = ($env->{PATH_INFO} =~ m!status/(\d+)!)[0] || 200;
            return sub {
                my $r = shift;
                my $w = $r->([ $status, [ 'Content-Type' => 'text/plain' ]]);
                $w->write("Error: $status\n");
                $w->close;
            };
        };
    };

    test_psgi app => $handler, client => sub {
        my $cb = shift;
        {
            my $res = $cb->(GET "http://localhost/");
            is $res->code, 200;

            $res = $cb->(GET "http://localhost/status/404");
            is $res->code, 404;
            like $res->header('content_type'), qr!text/html!;
            like $res->content, qr/fancy 404/;
        }
    } while flip_backend;
}

done_testing;