blib/lib/Kelp/Test.pm

filter: Statementstmt 88.2% 142 / 161 Branchbran 62.5% 10 / 16 Conditioncond 49.0% 25 / 51 MC/DCmcdc 13.7% 4 / 29 Subroutinesub 85.0% 34 / 40 Podpod 100% 21 / 21 totaltotal 74.2% 236 / 318 CC 31 SCAR 10.5
CC
31
Cov
78%
header_unlike
12.0
content_type_isnt
6.0
code_is
4.4
CRAP 2.9
« blib/lib/Kelp/Base.pm ↑ Summary blib/lib/Kelp/Template.pm »
1 package Kelp::Test;
2  
3 29 ▶use Kelp::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 29 ▶use Plack::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use Test::More import => ['!note'];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use Test::Deep;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use Kelp::Test::CookieJar;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use Kelp::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 29 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 29 $ENV{KELP_TESTING} = 1; # Set the ENV for testing
15 }
16  
17 sub import
18 29 ▶{
Subroutine import: called CC=3 SCAR=11.0
19 29 my ($me, @args) = @_;
20  
21 29 ▶ if ($args[0] && $args[0] eq -utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] and $args[0] eq "-utf8") 6 23 29
Condition66%2/3
$args[0] and $args[0] eq "-utf8"
!ll&&!rl&&r
2306
MC/DC50%1/2
$args[0] and $args[0] eq "-utf8"
$args[0] $args[0] eq "-utf8"
Truth table66%2/3
A and B
A= $args[0]B= $args[0] eq "-utf8"
ABresult
0X0
100
111
22 6 my $builder = Test::More->builder;
23 6 ▶ binmode $builder->output, ":encoding(utf8)";
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 binmode $builder->failure_output, ":encoding(utf8)";
25 6 binmode $builder->todo_output, ":encoding(utf8)";
26 }
27 }
28  
29 29 attr -psgi => undef;
30  
31 1 ▶attr -app => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
32 1 my $self = shift;
33 1 ▶ return defined $self->psgi
Branch: 1 branch
BranchTrueFalseTotal
defined $self->psgi ? : 1 0 1
34 ? Plack::Util::load_psgi($self->psgi)
35 : die "'app' or 'psgi' parameter is required";
36 29 };
37 29 attr charset => undef;
38  
39 0 ▶attr res => sub { die "res is not initialized" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
40  
41 30 ▶attr cookies => sub { Kelp::Test::CookieJar->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 sub _decode
44 220 ▶{
Subroutine _decode: called CC=2 SCAR=7.1
45 220 my ($self, $string) = @_;
46 220 ▶ return Kelp::Util::charset_decode($self->charset // $self->app->charset, $string);
Condition66%2/3
$self->charset // $self->app->charset
l!l&&r!l&&!r
82120
MC/DC0%0/2
$self->charset // $self->app->charset
$self->charset $self->app->charset
Truth table66%2/3
A // B
A= $self->charsetB= $self->app->charset
ABresult
1X1
011
000
47 }
48  
49 sub request
50 251 ▶{
Subroutine request: called CC=4 SCAR=13.9
Pod documented
51 251 my ($self, $req) = @_;
52 251 ▶ croak "HTTP::Request object needed" unless ref($req) eq 'HTTP::Request';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $req eq "HTTP::Request" 0 251 251
53 251 $self->note($req->method . ' ' . $req->uri);
54  
55 # Most likely the request was not initialized with a URI that had a scheme,
56 # so we add a default http to prevent unitialized regex matches further
57 # down the chain
58 251 ▶ $req->uri->scheme('http') unless $req->uri->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $req->uri->scheme 251 0 251
59  
60 # If no host was given to the request's uri (most likely), then add
61 # localhost. This is needed by the cookies header, which will not be
62 # applied unless the request uri has a proper domain.
63 251 ▶ if ($req->uri->opaque =~ qr|^/{1}|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->uri->opaque =~ qr"^/{1}") 250 1 251
64 250 $req->uri->opaque('//localhost' . $req->uri->opaque);
65 }
66  
67 # Add the current cookie to the request headers
68 251 $self->cookies->add_cookie_header($req);
69  
70 251 ▶ my $res = test_psgi($self->app->run, sub { shift->($req) });
Subroutine __ANON__: called CC=1 SCAR=0.0
71  
72 # Extract the cookies from the response and add them to the cookie jar
73 251 $self->cookies->extract_cookies($res);
74  
75 251 $self->res($res);
76 251 return $self;
77 }
78  
79 sub request_ok
80 23 ▶{
Subroutine request_ok: called CC=1 SCAR=0.0
Pod documented
81 23 my ($self, $req, $test_name) = @_;
82 23 local $Test::Builder::Level = $Test::Builder::Level + 1;
83  
84 23 $self->request($req)->code_is(200, $test_name);
85 }
86  
87 sub code_is
88 176 ▶{
Subroutine code_is: called CC=4 SCAR=14.8
Pod documented
89 176 my ($self, $code, $test_name) = @_;
90 176 local $Test::Builder::Level = $Test::Builder::Level + 1;
91  
92 176 ▶ $test_name ||= "Response code is $code";
Condition50%1/2
$test_name ||= "Response code is $code"
l!l
0176
MC/DC0%0/1
$test_name ||= "Response code is $code"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
93 176 is $self->res->code, $code, $test_name;
94  
95 # If we got 500 back and shouldn't have, we show the content
96 176 ▶ if ($code != 500 && $self->res->code == 500) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code != 500 and $self->res->code == 500) 0 176 176
Condition66%2/3
$code != 500 and $self->res->code == 500
!ll&&!rl&&r
381380
MC/DC0%0/2
$code != 500 and $self->res->code == 500
$code != 500 $self->res->code == 500
Truth table66%2/3
A and B
A= $code != 500B= $self->res->code == 500
ABresult
0X0
100
111
97 0 fail $self->res->content;
98 }
99  
100 176 return $self;
101 }
102  
103 sub code_isnt
104 2 ▶{
Subroutine code_isnt: called CC=2 SCAR=7.0
Pod documented
105 2 my ($self, $code, $test_name) = @_;
106 2 local $Test::Builder::Level = $Test::Builder::Level + 1;
107  
108 2 ▶ $test_name ||= "Response code is not $code";
Condition50%1/2
$test_name ||= "Response code is not $code"
l!l
02
MC/DC0%0/1
$test_name ||= "Response code is not $code"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
109 2 isnt $self->res->code, $code, $test_name;
110 2 return $self;
111 }
112  
113 sub content_is
114 116 ▶{
Subroutine content_is: called CC=2 SCAR=7.0
Pod documented
115 116 my ($self, $value, $test_name) = @_;
116 116 local $Test::Builder::Level = $Test::Builder::Level + 1;
117  
118 116 ▶ $test_name ||= "Content is '$value'";
Condition50%1/2
$test_name ||= "Content is '${value}'"
l!l
0116
MC/DC0%0/1
$test_name ||= "Content is '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
119 116 is $self->_decode($self->res->content), $value,
120 $test_name;
121 116 return $self;
122 }
123  
124 sub content_bytes_are
125 11 ▶{
Subroutine content_bytes_are: called CC=2 SCAR=7.0
Pod documented
126 11 my ($self, $value, $test_name) = @_;
127 11 local $Test::Builder::Level = $Test::Builder::Level + 1;
128  
129 11 ▶ $test_name ||= "Content is '$value'";
Condition50%1/2
$test_name ||= "Content is '${value}'"
l!l
110
MC/DC0%0/1
$test_name ||= "Content is '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
130 11 my $got = unpack 'H*', $self->res->content;
131 11 my $expected = unpack 'H*', $value;
132 11 is $got, $expected, $test_name;
133 11 return $self;
134 }
135  
136 sub content_isnt
137 1 ▶{
Subroutine content_isnt: called CC=2 SCAR=7.0
Pod documented
138 1 my ($self, $value, $test_name) = @_;
139 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
140  
141 1 ▶ $test_name ||= "Content is not '$value'";
Condition50%1/2
$test_name ||= "Content is not '${value}'"
l!l
01
MC/DC0%0/1
$test_name ||= "Content is not '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
142 1 isnt $self->_decode($self->res->content), $value,
143 $test_name;
144 1 return $self;
145 }
146  
147 sub content_like
148 61 ▶{
Subroutine content_like: called CC=2 SCAR=6.9
Pod documented
149 61 my ($self, $regexp, $test_name) = @_;
150 61 local $Test::Builder::Level = $Test::Builder::Level + 1;
151  
152 61 ▶ $test_name ||= "Content matches $regexp";
Condition100%2/2
$test_name ||= "Content matches $regexp"
l!l
754
MC/DC100%1/1
$test_name ||= "Content matches $regexp"
$test_name
Truth table100%2/2
A
A= $test_name
Aresult
11
00
153 61 like $self->_decode($self->res->content), $regexp,
154 $test_name;
155 61 return $self;
156 }
157  
158 sub content_unlike
159 23 ▶{
Subroutine content_unlike: called CC=2 SCAR=6.9
Pod documented
160 23 my ($self, $regexp, $test_name) = @_;
161 23 local $Test::Builder::Level = $Test::Builder::Level + 1;
162  
163 23 ▶ $test_name ||= "Content does not match $regexp";
Condition100%2/2
$test_name ||= "Content does not match $regexp"
l!l
221
MC/DC100%1/1
$test_name ||= "Content does not match $regexp"
$test_name
Truth table100%2/2
A
A= $test_name
Aresult
11
00
164 23 unlike $self->_decode($self->res->content), $regexp,
165 $test_name;
166 23 return $self;
167 }
168  
169 sub content_type_is
170 40 ▶{
Subroutine content_type_is: called CC=2 SCAR=7.0
Pod documented
171 40 my ($self, $value, $test_name) = @_;
172 40 local $Test::Builder::Level = $Test::Builder::Level + 1;
173  
174 40 ▶ $test_name ||= "Content-Type is '$value'";
Condition50%1/2
$test_name ||= "Content-Type is '${value}'"
l!l
040
MC/DC0%0/1
$test_name ||= "Content-Type is '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
175 40 is $self->res->content_type, $value, $test_name;
176 40 return $self;
177 }
178  
179 sub full_content_type_is
180 6 ▶{
Subroutine full_content_type_is: called CC=2 SCAR=7.0
Pod documented
181 6 my ($self, $value, $test_name) = @_;
182 6 local $Test::Builder::Level = $Test::Builder::Level + 1;
183  
184 6 ▶ $test_name ||= "Full Content-Type is '$value'";
Condition50%1/2
$test_name ||= "Full Content-Type is '${value}'"
l!l
06
MC/DC0%0/1
$test_name ||= "Full Content-Type is '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
185 6 is join('; ', $self->res->content_type), $value, $test_name;
186 6 return $self;
187 }
188  
189 sub content_type_isnt
190 0 ▶{
Subroutine content_type_isnt: not called CC=2 SCAR=17.9
Pod documented
191 0 my ($self, $value, $test_name) = @_;
192 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
193  
194 0 ▶ $test_name ||= "Content-Type is not '$value'";
Condition0%0/2
$test_name ||= "Content-Type is not '${value}'"
l!l
00
MC/DC0%0/1
$test_name ||= "Content-Type is not '${value}'"
$test_name
Truth table0%0/2
A
A= $test_name
Aresult
11
00
195 0 isnt $self->res->content_type, $value, $test_name;
196 0 return $self;
197 }
198  
199 sub header_is
200 24 ▶{
Subroutine header_is: called CC=3 SCAR=11.8
Pod documented
201 24 my ($self, $header, $value, $test_name) = @_;
202 24 local $Test::Builder::Level = $Test::Builder::Level + 1;
203  
204 24 ▶ $test_name ||= "Header '$header' => '$value'";
Condition50%1/2
$test_name ||= "Header '${header}' => '${value}'"
l!l
024
MC/DC0%0/1
$test_name ||= "Header '${header}' => '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
205 24 ▶ is $self->res->header($header), $value, $test_name
Condition33%1/3
$test_name || $self->diag_headers
l!l&&r!l&&!r
2400
MC/DC0%0/2
$test_name || $self->diag_headers
$test_name $self->diag_headers
Truth table33%1/3
A || B
A= $test_nameB= $self->diag_headers
ABresult
1X1
011
000
206 || $self->diag_headers();
207 24 return $self;
208 }
209  
210 sub header_isnt
211 3 ▶{
Subroutine header_isnt: called CC=3 SCAR=11.8
Pod documented
212 3 my ($self, $header, $value, $test_name) = @_;
213 3 local $Test::Builder::Level = $Test::Builder::Level + 1;
214  
215 3 ▶ $test_name ||= "Header '$header' is not '$value'";
Condition50%1/2
$test_name ||= "Header '${header}' is not '${value}'"
l!l
03
MC/DC0%0/1
$test_name ||= "Header '${header}' is not '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
216 3 ▶ isnt $self->res->header($header), $value, $test_name
Condition33%1/3
$test_name || $self->diag_headers
l!l&&r!l&&!r
300
MC/DC0%0/2
$test_name || $self->diag_headers
$test_name $self->diag_headers
Truth table33%1/3
A || B
A= $test_nameB= $self->diag_headers
ABresult
1X1
011
000
217 || $self->diag_headers();
218 3 return $self;
219 }
220  
221 sub header_like
222 1 ▶{
Subroutine header_like: called CC=3 SCAR=11.8
Pod documented
223 1 my ($self, $header, $regexp, $test_name) = @_;
224 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
225  
226 1 ▶ $test_name ||= "Header '$header' =~ $regexp";
Condition50%1/2
$test_name ||= "Header '${header}' =~ $regexp"
l!l
01
MC/DC0%0/1
$test_name ||= "Header '${header}' =~ $regexp"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
227 1 ▶ like $self->res->header($header), $regexp, $test_name
Condition33%1/3
$test_name || $self->diag_headers
l!l&&r!l&&!r
100
MC/DC0%0/2
$test_name || $self->diag_headers
$test_name $self->diag_headers
Truth table33%1/3
A || B
A= $test_nameB= $self->diag_headers
ABresult
1X1
011
000
228 || $self->diag_headers();
229 1 return $self;
230 }
231  
232 sub header_unlike
233 0 ▶{
Subroutine header_unlike: not called CC=3 SCAR=24.8
Pod documented
234 0 my ($self, $header, $regexp, $test_name) = @_;
235 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
236  
237 0 ▶ $test_name ||= "Header '$header' !~ $regexp";
Condition0%0/2
$test_name ||= "Header '${header}' !~ $regexp"
l!l
00
MC/DC0%0/1
$test_name ||= "Header '${header}' !~ $regexp"
$test_name
Truth table0%0/2
A
A= $test_name
Aresult
11
00
238 0 ▶ unlike $self->res->header($header), $regexp, $test_name
Condition0%0/3
$test_name || $self->diag_headers
l!l&&r!l&&!r
000
MC/DC0%0/2
$test_name || $self->diag_headers
$test_name $self->diag_headers
Truth table0%0/3
A || B
A= $test_nameB= $self->diag_headers
ABresult
1X1
011
000
239 || $self->diag_headers();
240 0 return $self;
241 }
242  
243 sub json_content
244 19 ▶{
Subroutine json_content: called CC=1 SCAR=0.0
Pod documented
245 19 my $self = shift;
246 19 my $result;
247 19 my $decoder = $self->app->get_encoder(json => 'internal');
248 19 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
249 19 $result = $decoder->decode(
250 $self->_decode($self->res->content)
251 );
252 }
253 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
254 0 fail("Poorly formatted JSON");
255 19 };
256 19 return $result;
257 }
258  
259 sub json_cmp
260 18 ▶{
Subroutine json_cmp: called CC=4 SCAR=14.0
Pod documented
261 18 my ($self, $expected, $test_name) = @_;
262 18 local $Test::Builder::Level = $Test::Builder::Level + 1;
263  
264 18 ▶ $test_name ||= "JSON structure matches";
Condition100%2/2
$test_name ||= "JSON structure matches"
l!l
117
MC/DC100%1/1
$test_name ||= "JSON structure matches"
$test_name
Truth table100%2/2
A
A= $test_name
Aresult
11
00
265 18 ▶ like $self->res->header('content-type'), qr/json/, 'Content-Type is JSON'
Branch: 1 branch
BranchTrueFalseTotal
&like(scalar $self->res->header("content-type"), qr/json/, "Content-Type is JSON") or return $self 18 0 18
266 or return $self;
267 18 my $json = $self->json_content;
268 18 ▶ cmp_deeply($json, $expected, $test_name) or diag explain $json;
Branch: 1 branch
BranchTrueFalseTotal
cmp_deeply($json, $expected, $test_name) or diag(explain($json)) 18 0 18
269 18 return $self;
270 }
271  
272 sub note
273 251 ▶{
Subroutine note: called CC=1 SCAR=0.0
Pod documented
274 251 my $self = shift;
275 251 Test::More::note @_;
276 251 return $self;
277 }
278  
279 sub diag_headers
280 0 ▶{
Subroutine diag_headers: not called CC=1 SCAR=6.9
Pod documented
281 0 my $self = shift;
282 0 diag $self->res->headers->as_string;
283 0 return $self;
284 }
285  
286 sub diag_content
287 0 ▶{
Subroutine diag_content: not called CC=1 SCAR=6.9
Pod documented
288 0 my $self = shift;
289 0 diag $self->res->content;
290 0 return $self;
291 }
292  
293 29 1;
294  
295 __END__
296  
297 =pod
298  
299 =head1 NAME
300  
301 Kelp::Test - Automated tests for a Kelp web app
302  
303 =head1 SYNOPSIS
304  
305 use MyApp;
306 use Kelp::Test;
307 use HTTP::Request::Common;
308  
309 my $app = MyApp->new;
310 my $t = Kelp::Test->new( app => $app );
311  
312 $t->request( GET '/path' )
313 ->code_is(200)
314 ->content_is("It works");
315  
316 $t->request( POST '/api' )
317 ->json_cmp({auth => 1});
318  
319 # automatically sets wide output for Test::More (disables Wide character warnings)
320 use Kelp::Test -utf8;
321  
322 =head1 DESCRIPTION
323  
324 This module provides basic tools for testing a Kelp based web application. It
325 is object oriented, and all methods return C<$self>, so they can be chained
326 together.
327 Testing is done by sending HTTP requests to an already built application and
328 analyzing the response. Therefore, each test usually begins with the L</request>
329 method, which takes a single L<HTTP::Request> parameter. It sends the request to
330 the web app and saves the response as an L<HTTP::Response> object.
331  
332 =head1 ENV VARIABLES
333  
334 =head2 KELP_TESTING
335  
336 This module sets the C<KELP_TESTING> environmental variable to a true value.
337  
338 =head1 ATTRIBUTES
339  
340 =head2 app
341  
342 The Kelp::Test object is instantiated with single attribute called C<app>. It
343 is a reference to a Kelp based web app.
344  
345 my $myapp = MyApp->new;
346 my $t = Kelp::Test->new( app => $myapp );
347  
348 From this point on, all requests run with C<$t-E<gt>request> will be sent to C<$app>.
349  
350 =head2 charset
351  
352 The charset to use for decoding the response. By default, application's charset
353 will be used. Use it if some responses are in a different charset. Can be
354 cleared by setting it back to undef.
355  
356 =head2 res
357  
358 Each time C<$t-E<gt>request> is used to send a request, an HTTP::Response object is
359 returned and saved in the C<res> attribute. You can use it to run tests,
360 although as you will see, this module provides methods which make this a lot
361 easier. It is recommended that you use the convenience methods rather than using
362 C<res>.
363  
364 $t->request( GET '/path' )
365 is $t->res->code, 200, "It's a success";
366  
367 =head2 cookies
368  
369 An object of C<Kelp::Test::CookieJar> implementing the partial interface of
370 L<HTTP::Cookies> module, containing the cookie jar for all tests. Compared to
371 the module it's mocking, it does not handle cookie parameters other than name
372 and value, but properly escapes the cookie name and value for the request.
373 Its usage should usually be as trivial as this:
374  
375 # NOTE: extra undef parameters are required to match HTTP::Cookies interface
376  
377 $t->set_cookie(undef, $name, $value);
378 $t->request(...);
379  
380 my $cookies_hash = $t->get_cookies;
381 my @cookie_values = $t->get_cookies(undef, 'cookie1', 'cookie2');
382  
383 =head1 METHODS
384  
385 =head2 request
386  
387 C<request( $http_request )>
388  
389 Takes an L<HTTP::Request> object and sends it to the application. When the
390 L<HTTP::Response> object is returned, it is initialized in the L</res>
391 attribute.
392 It is very convenient to use L<HTTP::Request::Common> in your test modules, so
393 you can take advantage of the simplified syntax for creating an HTTP request.
394  
395 $t->request( POST '/api', [ user => 'jane' ] );
396  
397 This method returns C<$self>, so other methods can be chained after it.
398  
399 =head2 request_ok
400  
401 C<request_ok( $http_request, $test_name )>
402  
403 Runs C<request>, then tests if the response code is 200. Equivalent to the following
404 code:
405  
406 $t->request( GET '/path' )->code_is(200);
407 $t->request_ok( GET '/path' ); # Same as the above
408  
409 =head2 code_is, code_isnt
410  
411 C<code_is( $code, $test_name )>, C<code_isnt( $code, $test_name )>
412  
413 Tests if the last response returned a status code equal or not equal to C<$code>.
414 An optional name of the test can be added as a second parameter.
415  
416 $t->request( GET '/path' )->code_is(200);
417 $t->request( GET '/path' )->code_isnt(500);
418  
419 =head2 request_ok
420  
421 Same as L</request>, but also runs C<code_is(200)>.
422  
423 $t->request_ok( GET '/home' );
424 # Tests for code = 200
425  
426 =head2 content_is, content_isnt
427  
428 C<content_is( $value, $test_name )>, C<content_isnt( $value, $test_name )>
429  
430 Tests if the last response returned content equal or not equal to C<$value>.
431 An optional name of the test can be added as a second parameter.
432  
433 $t->request( GET '/path' )->content_is("Ok.");
434 $t->request( GET '/path' )->content_isnt("Fail.");
435  
436 =head2 content_bytes_are
437  
438 Same as C<content_is>, but the result is not decoded and the values are
439 compared byte by byte as hex-encoded string.
440  
441 =head2 content_like, content_unlike
442  
443 C<content_like( $regexp, $test_name )>, C<content_unlike( $regexp, $test_name )>
444  
445 Tests if the last response returned content that matches or doesn't match C<$regexp>.
446 An optional name of the test can be added as a second parameter.
447  
448 $t->request( GET '/path' )->content_like(qr{Amsterdam});
449 $t->request( GET '/path' )->content_unlike(qr{Rotterdam});
450  
451 =head2 content_type_is, content_type_isnt
452  
453 C<content_type_is( $value, $test_name )>, C<content_type_isnt( $value, $test_name )>
454  
455 Tests if the last response's content-type header is equal or not equal to C<$value>.
456 An optional name of the test can be added as a second parameter.
457  
458 $t->request( GET '/path' )->content_type_is("text/plain");
459 $t->request( GET '/path' )->content_type_isnt("text/html");
460  
461 =head2 full_content_type_is
462  
463 Like L</content_type_is>, but checks the full content type (with charset).
464  
465 =head2 header_is, header_isnt
466  
467 C<header_is( $header, $value, $test_name )>, C<header_isnt( $header, $value, $test_name )>
468  
469 Tests if the last response returned a header C<$header> that is equal or not
470 equal to C<$value>. An optional name of the test can be added as a second parameter.
471  
472 $t->request( GET '/path' )->header_is( "Pragma", "no-cache" );
473 $t->request( GET '/path' )->header_isnt( "X-Check", "yes" );
474  
475 =head2 header_like, header_unlike
476  
477 C<header_like( $header, $regexp, $test_name )>, C<header_unlike( $header, $regexp, $test_name )>
478  
479 Tests if the last response returned a header C<$header> that matches or doesn't
480 match C<$regexp>. An optional name of the test can be added as a second parameter.
481  
482 $t->request( GET '/path' )->header_like( "Content-Type", qr/json/ );
483 $t->request( GET '/path' )->header_unlike( "Content-Type", qr/image/ );
484  
485 =head2 json_content
486  
487 C<json_content()>
488  
489 Returns the content decoded as JSON. Does not perform any checks, but may
490 C<fail()> and return C<undef> if the JSON decoding fails.
491  
492 =head2 json_cmp
493  
494 C<json_cmp( $expected, $test_name )>
495  
496 This tests for two things: If the returned C<content-type> is
497 C<application-json>, and if the returned JSON structure matches the structure
498 specified in C<$expected>. To compare the two structures this method uses
499 C<cmp_deeply> from L<Test::Deep>, so you can use all the goodies from the
500 C<SPECIAL-COMPARISONS-PROVIDED> section of the Test::Deep module.
501  
502 $t->request( GET '/api' )->json_cmp(
503 {
504 auth => 1,
505 timestamp => ignore(),
506 info => subhashof( { name => 'Rick James' } )
507 }
508 );
509  
510 An optional name of the test can be added as a second parameter.
511  
512 =head2 note
513  
514 C<note( $note )>
515  
516 Print a note, using the L<Test::More> C<note> function.
517  
518 $t->request( GET '/path' )
519 ->note("Checking headers now")
520 ->header_is( "Content-Type", qr/json/ );
521  
522 =head2 diag_headers
523  
524 Prints all headers for debugging purposes.
525  
526 $t->request( GET '/path' )
527 ->header_is( "Content-Type", qr/json/ )
528 ->diag_headers();
529  
530 =head2 diag_content
531  
532 Prints the entire content for debugging purposes.
533  
534 $t->request( GET '/path' )
535 ->content_is("Well")
536 ->diag_content();
537  
538 =cut
539  
« blib/lib/Kelp/Base.pm ↑ Summary blib/lib/Kelp/Template.pm »