File Coverage

blib/lib/Web/API/Mock/Resource.pm
Criterion Covered Total %
statement 31 31 100.0
branch 2 2 100.0
condition 6 12 50.0
subroutine 8 8 100.0
pod 0 4 0.0
total 47 57 82.4


line stmt bran cond sub pod time code
1             package Web::API::Mock::Resource;
2 5     5   95 use 5.008005;
  5         27  
3 5     5   30 use strict;
  5         11  
  5         195  
4 5     5   27 use warnings;
  5         10  
  5         499  
5              
6             our $VERSION = "0.11";
7              
8             use Class::Accessor::Lite (
9 5         46 new => 1,
10             rw => [ qw/url header body status content_type/ ],
11 5     5   3068 );
  5         8565  
12              
13             sub add {
14 20     20 0 49 my ($self, $args) = @_;
15 20   50     66 my $method = $args->{method} // 'GET';
16 20   50     73 my $header = $args->{header} // {};
17 20   50     68 my $body = $args->{body} // '';
18 20   50     49 my $status = $args->{status} // 200;
19 20   50     61 my $content_type = $args->{content_type} // 'text/html';
20              
21 20 100       52 unless ( $self->body ) {
22 18         135 $self->status({});
23 18         150 $self->content_type({});
24 18         168 $self->header({});
25 18         150 $self->body({});
26             }
27              
28 20         123 $self->status->{$method} = $status;
29 20         132 $self->content_type->{$method} = $content_type;
30 20         98 $self->header->{$method} = $header;
31 20         96 $self->body->{$method} = $body;
32             }
33              
34             sub response {
35 16     16 0 41 my ($self, $method) = @_;
36 16   50     60 $method //= 'GET';
37              
38             return {
39             status => $self->status->{$method},
40             content_type => $self->content_type->{$method},
41             header => $self->header->{$method},
42 16         55 body => $self->body->{$method}
43             }
44              
45             }
46              
47             sub status_404 {
48             return {
49 20     20 0 142 status => 404,
50             content_type => 'text/plain',
51             method => 'GET',
52             header => '',
53             body => '404 Not Found'
54             }
55             }
56              
57             sub status_501 {
58             return {
59 2     2 0 14 status => 501,
60             content_type => 'text/plain',
61             method => 'GET',
62             header => '',
63             body => '501 Not Implemented'
64             }
65             }
66              
67             1;