File Coverage

blib/lib/Plack/Test/Simple/Transaction.pm
Criterion Covered Total %
statement 51 87 58.6
branch 4 8 50.0
condition 3 41 7.3
subroutine 16 28 57.1
pod 14 14 100.0
total 88 178 49.4


line stmt bran cond sub pod time code
1             # ABSTRACT: PSGI Automated Application Testing Layer
2             package Plack::Test::Simple::Transaction;
3              
4 2     2   10 use utf8;
  2         3  
  2         16  
5 2     2   59 use HTTP::Request;
  2         4  
  2         41  
6 2     2   10 use HTTP::Response;
  2         4  
  2         41  
7 2     2   9 use URI;
  2         4  
  2         37  
8 2     2   12 use Moo;
  2         4  
  2         25  
9 2     2   788 use Plack::Util;
  2         5  
  2         53  
10 2     2   10 use JSON qw(decode_json);
  2         5  
  2         19  
11 2     2   427 use Test::More qw();
  2         5  
  2         56  
12 2     2   1862 use Plack::Test qw();
  2         1328  
  2         44  
13 2     2   2522 use Data::DPath qw(dpath);
  2         396792  
  2         15  
14              
15              
16              
17             has data => (
18             is => 'rw',
19             lazy => 1,
20             builder => 1
21             );
22              
23             sub _build_data {
24 4     4   2555 my ($self) = @_;
25              
26 4 50       102 return {} unless $self->response->content;
27 4 50       537 return {} unless $self->response->header('Content-Type');
28 4 50       1888 return {} unless $self->response->header('Content-Type') =~ /json/i;
29              
30             # only supporting JSON data currently !!!
31 4         806 return decode_json $self->response->decoded_content;
32             }
33              
34              
35             has psgi => (
36             is => 'rw',
37             );
38              
39              
40             has request => (
41             is => 'rw',
42             lazy => 1,
43             builder => 1
44             );
45              
46             sub _build_request {
47 0     0   0 return HTTP::Request->new(
48             uri => URI->new(scheme => 'http', host => 'localhost', path => '/')
49             )
50             }
51              
52              
53             has response => (
54             is => 'rw',
55             lazy => 1,
56             builder => 1
57             );
58              
59             sub _build_response {
60 4     4   2243 my $self = shift;
61 4         111 return $self->psgi->request($self->request);
62             }
63              
64              
65             sub content_is {
66 0     0 1 0 my ($self, $value, $desc) = @_;
67 0   0     0 $desc ||= 'exact match for content';
68 0         0 return $self->_test_more(
69             'is', $self->response->decoded_content, $value, $desc
70             );
71             }
72              
73              
74             sub content_isnt {
75 0     0 1 0 my ($self, $value, $desc) = @_;
76 0   0     0 $desc ||= 'not an exact match for content';
77 0         0 return $self->_test_more(
78             'isnt', $self->response->decoded_content, $value, $desc
79             );
80             }
81              
82              
83             sub content_like {
84 0     0 1 0 my ($self, $regex, $desc) = @_;
85 0   0     0 $desc ||= 'content contains the expression specified';
86 0         0 return $self->_test_more(
87             'like', $self->response->decoded_content, $regex, $desc
88             );
89             }
90              
91              
92             sub content_unlike {
93 0     0 1 0 my ($self, $regex, $desc) = @_;
94 0   0     0 $desc ||= 'content does not contain the expression specified';
95 0         0 return $self->_test_more(
96             'unlike', $self->response->decoded_content, $regex, $desc
97             );
98             }
99              
100              
101             sub data_has {
102 0     0 1 0 my ($self, $path, $desc) = @_;
103 0   0     0 $desc ||= qq{has value for data path "$path"};
104 0         0 my $rs = [ dpath($path)->match($self->data) ];
105 0         0 return $self->_test_more(
106             'ok', $rs->[0], $desc
107             );
108             }
109              
110              
111             sub data_hasnt {
112 0     0 1 0 my ($self, $path, $desc) = @_;
113 0   0     0 $desc ||= qq{has no value for data path "$path"};
114 0         0 my $rs = [ dpath($path)->match($self->data) ];
115 0         0 return $self->_test_more(
116             'ok', !$rs->[0], $desc
117             );
118             }
119              
120              
121             sub data_is_deeply {
122 4     4 1 13 my $self = shift;
123 4 50       28 my ($path, $data) = ref $_[0] ? ('', shift) : (shift, shift);
124 4   50     17 $path ||= '/';
125 4   33     37 my $desc ||= qq{exact match for data path "$path"};
126 4         31 my $rs = [ dpath($path)->match($self->data) ];
127 4         4122 return $self->_test_more(
128             'is_deeply', $rs->[0], $data, $desc
129             );
130             }
131              
132              
133             sub data_match {
134 4     4 1 684 goto &data_is_deeply;
135             }
136              
137              
138             sub header_is {
139 0     0 1 0 my ($self, $name, $value, $desc) = @_;
140 0   0     0 $desc ||= "exact match for header $name with value " . ($value // '');
      0        
141 0         0 return $self->_test_more(
142             'is', $self->response->header($name), $value, $desc
143             );
144             }
145              
146              
147             sub header_isnt {
148 0     0 1 0 my ($self, $name, $value, $desc) = @_;
149 0   0     0 $desc ||= "not an exact match for header $name with value " . ($value // '');
      0        
150 0         0 return $self->_test_more(
151             'isnt', $self->response->header($name), $value, $desc
152             );
153             }
154              
155              
156             sub header_like {
157 0     0 1 0 my ($self, $name, $regex, $desc) = @_;
158 0   0     0 $desc ||= "header $name contains the expression specified";
159 0         0 return $self->_test_more(
160             'like', $self->response->header($name), $regex, $desc
161             );
162             }
163              
164              
165             sub header_unlike {
166 0     0 1 0 my ($self, $name, $regex, $desc) = @_;
167 0   0     0 $desc ||= "header $name does not contain the expression specified";
168 0         0 return $self->_test_more(
169             'unlike', $self->response->header($name), $regex, $desc
170             );
171             }
172              
173              
174             sub status_is {
175 4     4 1 14 my ($self, $code, $desc) = @_;
176 4   33     37 $desc ||= "status is $code";
177 4         106 return $self->_test_more(
178             'is', $self->response->code, $code, $desc
179             );
180             }
181              
182              
183             sub status_isnt {
184 0     0 1 0 my ($self, $code, $desc) = @_;
185 0   0     0 $desc ||= "status is not $code";
186 0         0 return $self->_test_more(
187             'isnt', $self->response->code, $code, $desc
188             );
189             }
190              
191             sub _test_more {
192 8     8   20523 my ($self, $name, @args) = @_;
193              
194 8         23 local $Test::Builder::Level = $Test::Builder::Level + 2;
195 8         116 Test::More->can($name)->(@args);
196              
197 8         15428 return $self;
198             }
199              
200             1;
201              
202             __END__