File Coverage

blib/lib/Web/API/Mock/Resource.pm
Criterion Covered Total %
statement 32 32 100.0
branch 2 2 100.0
condition 6 12 50.0
subroutine 8 8 100.0
pod 0 4 0.0
total 48 58 82.7


line stmt bran cond sub pod time code
1             package Web::API::Mock::Resource;
2 5     5   134 use 5.008005;
  5         13  
  5         179  
3 5     5   19 use strict;
  5         7  
  5         143  
4 5     5   22 use warnings;
  5         5  
  5         283  
5              
6             our $VERSION = "0.01";
7              
8             use Class::Accessor::Lite (
9 5         42 new => 1,
10             rw => [ qw/url header body status content_type/ ],
11 5     5   2721 );
  5         5785  
12              
13             sub add {
14 20     20 0 107 my ($self, $args) = @_;
15 20   50     66 my $method = $args->{method} // 'GET';
16 20   50     63 my $header = $args->{header} // {};
17 20   50     44 my $body = $args->{body} // '';
18 20   50     69 my $status = $args->{status} // 200;
19 20   50     50 my $content_type = $args->{content_type} // 'text/html';
20              
21 20 100       52 unless ( $self->body ) {
22 18         116 $self->status({});
23 18         115 $self->content_type({});
24 18         102 $self->header({});
25 18         87 $self->body({});
26             }
27              
28 20         100 $self->status->{$method} = $status;
29 20         109 $self->content_type->{$method} = $content_type;
30 20         101 $self->header->{$method} = $header;
31 20         94 $self->body->{$method} = $body;
32             }
33              
34             sub response {
35 16     16 0 29 my ($self, $method) = @_;
36 16   50     42 $method //= 'GET';
37              
38             return {
39 16         58 status => $self->status->{$method},
40             content_type => $self->content_type->{$method},
41             header => $self->header->{$method},
42             body => $self->body->{$method}
43             }
44              
45             }
46              
47             sub status_404 {
48             return {
49 20     20 0 120 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 8 status => 501,
60             content_type => 'text/plain',
61             method => 'GET',
62             header => '',
63             body => '501 Not Implemented'
64             }
65             }
66              
67             1;