File Coverage

lib/PAGI/App/NotFound.pm
Criterion Covered Total %
statement 21 21 100.0
branch 1 2 50.0
condition 4 6 66.6
subroutine 6 6 100.0
pod 0 2 0.0
total 32 37 86.4


line stmt bran cond sub pod time code
1             package PAGI::App::NotFound;
2              
3 1     1   378 use strict;
  1         1  
  1         33  
4 1     1   3 use warnings;
  1         1  
  1         65  
5 1     1   4 use Future::AsyncAwait;
  1         1  
  1         5  
6              
7             =head1 NAME
8              
9             PAGI::App::NotFound - Customizable 404 response
10              
11             =head1 SYNOPSIS
12              
13             use PAGI::App::NotFound;
14              
15             my $app = PAGI::App::NotFound->new(
16             body => '

Page not found

',
17             content_type => 'text/html',
18             )->to_app;
19              
20             =cut
21              
22             sub new {
23 2     2 0 4703 my ($class, %args) = @_;
24              
25             return bless {
26             body => $args{body} // 'Not Found',
27             content_type => $args{content_type} // 'text/plain',
28 2   100     25 status => $args{status} // 404,
      50        
      50        
29             }, $class;
30             }
31              
32             sub to_app {
33 2     2 0 4 my ($self) = @_;
34              
35 2         5 my $body = $self->{body};
36 2         4 my $content_type = $self->{content_type};
37 2         3 my $status = $self->{status};
38              
39 2     2   32 return async sub {
40 2         4 my ($scope, $receive, $send) = @_;
41 2 50       4 my $response_body = ref $body eq 'CODE' ? $body->($scope) : $body;
42              
43 2         9 await $send->({
44             type => 'http.response.start',
45             status => $status,
46             headers => [
47             ['content-type', $content_type],
48             ['content-length', length($response_body)],
49             ],
50             });
51 2         81 await $send->({ type => 'http.response.body', body => $response_body, more => 0 });
52 2         9 };
53             }
54              
55             1;
56              
57             __END__