File Coverage

blib/lib/Plack/Test/Simple.pm
Criterion Covered Total %
statement 43 46 93.4
branch 7 10 70.0
condition 5 15 33.3
subroutine 13 14 92.8
pod 1 2 50.0
total 69 87 79.3


line stmt bran cond sub pod time code
1             # ABSTRACT: Object-Oriented PSGI Application Testing
2             package Plack::Test::Simple;
3              
4 2     2   1544 use utf8;
  2         4  
  2         15  
5              
6 2     2   249 use Carp;
  2         4  
  2         160  
7 2     2   2377 use HTTP::Request;
  2         55337  
  2         71  
8 2     2   2819 use HTTP::Response;
  2         20657  
  2         75  
9 2     2   8090 use Moo;
  2         58768  
  2         20  
10 2     2   6780 use Plack::Test::Simple::Transaction;
  2         6  
  2         71  
11 2     2   19 use Plack::Util;
  2         34  
  2         49  
12 2     2   10 use URI;
  2         4  
  2         68  
13              
14 2     2   12 use JSON qw(encode_json);
  2         3  
  2         21  
15              
16             our $VERSION = '0.02'; # VERSION
17              
18             sub BUILDARGS {
19 1     1 0 2771 my ($class, @args) = @_;
20              
21 0         0 @args = @args == 1 ? 'HASH' eq ref $args[0] ?
22 1 50       11 %{$args[0]} : ('psgi', @args) : @args;
    50          
23              
24 1         29 return {@args};
25             }
26              
27             has request => (
28             is => 'rw',
29             lazy => 1,
30             builder => 1
31             );
32              
33             sub _build_request {
34 1     1   558 return HTTP::Request->new(
35             uri => URI->new(scheme => 'http', host => 'localhost', path => '/')
36             )
37             }
38              
39             has psgi => (
40             is => 'rw',
41             isa => sub {
42             my $psgi = shift;
43              
44             die 'The psgi attribute must must be a valid PSGI filepath or code '.
45             'reference' if !$psgi && ('CODE' eq ref($psgi) xor -f $psgi);
46             },
47             coerce => sub {
48             my $psgi = shift;
49              
50             # return psgi
51             return $psgi if (ref $psgi) =~ /Plack::Test::/; # very trusting
52             return Plack::Test->create($psgi) if 'CODE' eq ref $psgi;
53             return Plack::Test->create(Plack::Util::load_psgi($psgi));
54             }
55             );
56              
57             sub transaction {
58 4     4 1 11358 my ($self, $meth, $path, $cont) = @_;
59              
60 4         90 my $trans = Plack::Test::Simple::Transaction->new(
61             psgi => $self->psgi,
62             request => $self->request->clone
63             );
64              
65 4   50     3100 $meth ||= 'get';
66 4   50     13 $path ||= '/';
67              
68 4         70 $trans->request->method(uc $meth);
69 4         578 $trans->request->uri(URI->new($path));
70              
71 4 100       386 if (defined $cont) {
72 2 100       46 $trans->request->content(ref $cont ? encode_json($cont) : $cont);
73             }
74              
75 4         73 return $trans;
76             }
77              
78             sub AUTOLOAD {
79 3     3   41 my ($self, @args) = @_;
80 3         27 my @cmds = split /_/, ($Plack::Test::Simple::AUTOLOAD =~ /.*::([^:]+)/)[0];
81              
82 3 50 33     57 return $self->transaction($cmds[0], @args[0,1])->status_is(
      33        
      33        
83             $cmds[2], $args[2]
84             )
85             if @cmds == 3
86             && $cmds[0] =~ /^(get|post|put|delete|head|options|connect|patch|trace)$/
87             && $cmds[1] eq 'returns'
88             && $cmds[2] =~ /^\d{3}$/
89             ;
90              
91 0   0       croak sprintf q(Can't locate object method "%s" via package "%s"),
92             join('_', @cmds), ((ref $_[0] || $_[0]) || 'main')
93             }
94              
95 0     0     sub DESTROY {
96             # noop
97             }
98              
99             1;
100              
101             __END__