blib/lib/Plack/Test/Agent.pm

filter: Statementstmt 97.0% 98 / 101 Branchbran 86.3% 19 / 22 Conditioncond 54.5% 6 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 95.8% 23 / 24 Podpod 100% 9 / 9 totaltotal 90.2% 157 / 174 CC 16 SCAR 10.4
CC
16
Cov
92%
execute_request
6.0
__ANON__
6.0
new
4.1
CRAP 2.8
↑ Summary
1 package Plack::Test::Agent;
2 8 our $VERSION = '1.6';
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Test::TCP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Plack::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use HTTP::Message::PSGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use HTTP::Request::Common qw/GET PUT DELETE POST/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Test::WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use Plack::Util::Accessor qw( app host port server ua jar );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 ▶sub new {
Subroutine new: called CC=4 SCAR=14.1
Pod documented
17 10 my ( $class, %args ) = @_;
18  
19 10 my $self = bless {}, $class;
20  
21 10 $self->app( delete $args{app} );
22 10 $self->ua( delete $args{ua} );
23 10 ▶ $self->host( delete $args{host} || 'localhost' );
Condition50%1/2
delete $args{'host'} || 'localhost'
l!l
010
MC/DC0%0/1
delete $args{'host'} || 'localhost'
delete $args{'host'}
Truth table50%1/2
A
A= delete $args{'host'}
Aresult
11
00
24 10 $self->port( delete $args{port} );
25 10 ▶ $self->jar( delete $args{jar} || HTTP::Cookies->new );
Condition33%1/3
delete $args{'jar'} || 'HTTP::Cookies'->new
l!l&&r!l&&!r
0100
MC/DC0%0/2
delete $args{'jar'} || 'HTTP::Cookies'->new
delete $args{'jar'} 'HTTP::Cookies'->new
Truth table33%1/3
A || B
A= delete $args{'jar'}B= 'HTTP::Cookies'->new
ABresult
1X1
011
000
26  
27 10 ▶ $self->start_server( delete $args{server} ) if $args{server};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"server"} 6 4 10
28  
29 10 return $self;
30 }
31  
32 6 ▶sub start_server {
Subroutine start_server: called CC=2 SCAR=8.4
Pod documented
33 6 my ( $self, $server_class ) = @_;
34  
35 6 my $app = $self->app;
36 6 my $host = $self->host;
37  
38 my $server = Test::TCP->new(
39 0 ▶ code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
40 0 my $port = shift;
41 0 my %args = ( host => $host, port => $port );
42 0 ▶ return $server_class
Branch: 1 branch
BranchTrueFalseTotal
$server_class ? : 0 0 0
43 ? Plack::Loader->load( $server_class, %args )->run($app)
44 : Plack::Loader->auto(%args)->run($app);
45 },
46 6 );
47  
48 6 $self->port( $server->port );
49 6 ▶ $self->ua( $self->get_mech ) unless $self->ua;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ua 6 0 6
50 6 $self->server($server);
51 }
52  
53 19 ▶sub execute_request {
Subroutine execute_request: called CC=6 SCAR=18.0
Pod documented
54 19 my ( $self, $req ) = @_;
55  
56 19 ▶ if ( !$self->server && $self->jar ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->server and $self->jar) 10 9 19
Condition66%2/3
not $self->server and $self->jar
!ll&&!rl&&r
9010
MC/DC50%1/2
not $self->server and $self->jar
not $self->server $self->jar
Truth table66%2/3
not(A) and B
A= not $self->serverB= $self->jar
ABresult
0X0
100
111
57 10 $self->jar->add_cookie_header($req);
58 }
59  
60 19 ▶ my $res
Branch: 1 branch
BranchTrueFalseTotal
$self->server ? : 9 10 19
61 = $self->server
62 ? $self->ua->request($req)
63 : HTTP::Response->from_psgi( $self->app->( $req->to_psgi ) );
64  
65 19 $res->request($req);
66  
67 19 ▶ if ( !$self->server && $self->jar ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->server and $self->jar) 10 9 19
Condition66%2/3
not $self->server and $self->jar
!ll&&!rl&&r
9010
MC/DC50%1/2
not $self->server and $self->jar
not $self->server $self->jar
Truth table66%2/3
not(A) and B
A= not $self->serverB= $self->jar
ABresult
0X0
100
111
68 10 $self->jar->extract_cookies($res);
69 }
70  
71 19 return $res;
72 }
73  
74 13 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
75 13 my ( $self, $uri, @args ) = @_;
76 13 my $req = GET $self->normalize_uri($uri), @args;
77 13 return $self->execute_request($req);
78 }
79  
80 2 ▶sub post {
Subroutine post: called CC=1 SCAR=0.0
Pod documented
81 2 my ( $self, $uri, @args ) = @_;
82 2 my $req = POST $self->normalize_uri($uri), @args;
83 2 return $self->execute_request($req);
84 }
85  
86 2 ▶sub put {
Subroutine put: called CC=1 SCAR=0.0
Pod documented
87 2 my ( $self, $uri, @args ) = @_;
88 2 my $req = PUT $self->normalize_uri($uri), @args;
89 2 return $self->execute_request($req);
90 }
91  
92 2 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
93 2 my ( $self, $uri, @args ) = @_;
94 2 my $req = DELETE $self->normalize_uri($uri), @args;
95 2 return $self->execute_request($req);
96 }
97  
98 27 ▶sub normalize_uri {
Subroutine normalize_uri: called CC=4 SCAR=13.9
Pod documented
99 27 my ( $self, $uri ) = @_;
100 27 my $normalized = URI->new($uri);
101 27 my $port = $self->port;
102  
103 27 ▶ $normalized->scheme('http') unless $normalized->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $normalized->scheme 24 3 27
104 27 ▶ $normalized->host('localhost') unless $normalized->host;
Branch: 1 branch
BranchTrueFalseTotal
unless $normalized->host 24 3 27
105 27 ▶ $normalized->port($port) if $port;
Branch: 1 branch
BranchTrueFalseTotal
if $port 17 10 27
106  
107 27 return $normalized;
108 }
109  
110 8 ▶sub get_mech {
Subroutine get_mech: called CC=1 SCAR=0.0
Pod documented
111 8 my $self = shift;
112 8 return Test::WWW::Mechanize::Bound->new(
113 bound_uri => $self->normalize_uri('/') );
114 }
115  
116 package Test::WWW::Mechanize::Bound;
117 8 our $VERSION = '1.6';
118 8 ▶use parent 'Test::WWW::Mechanize';
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
121 8 my ( $class, %args ) = @_;
122 8 my $bound_uri = delete $args{bound_uri};
123 8 my $self = $class->SUPER::new(%args);
124 8 $self->bound_uri($bound_uri);
125 8 return $self;
126 }
127  
128 21 ▶sub bound_uri {
Subroutine bound_uri: called CC=2 SCAR=6.9
129 21 my ( $self, $base_uri ) = @_;
130 21 ▶ $self->_elem( bound_uri => $base_uri ) if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 8 13 21
131 21 return $self->_elem('bound_uri');
132 }
133  
134 13 ▶sub prepare_request {
Subroutine prepare_request: called CC=2 SCAR=6.9
135 13 my $self = shift;
136 13 my ($req) = @_;
137 13 my $uri = $req->uri;
138 13 my $base = $self->bound_uri;
139  
140 13 ▶ unless ( $uri->scheme ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri->scheme) 1 12 13
141 1 $uri->scheme( $base->scheme );
142 1 $uri->host( $base->host );
143 1 $uri->port( $base->port );
144 }
145 13 return $self->SUPER::prepare_request(@_);
146 }
147  
148 8 1;
149  
150 =pod
151  
152 =head1 NAME
153  
154 Plack::Test::Agent - OO interface for testing low-level Plack/PSGI apps
155  
156 =head1 VERSION
157  
158 version 1.6
159  
160 =encoding utf-8
161  
162 =head2 SYNOPSIS
163  
164 use Test::More;
165 use Plack::Test::Agent;
166  
167 my $app = sub { ... };
168 my $local_agent = Plack::Test::Agent->new( app => $app );
169 my $server_agent = Plack::Test::Agent->new(
170 app => $app,
171 server => 'HTTP::Server::Simple' );
172  
173 my $local_res = $local_agent->get( '/' );
174 my $server_res = $server_agent->get( '/' );
175  
176 ok $local_res->is_success, 'local GET / should succeed';
177 ok $server_res->is_success, 'server GET / should succeed';
178  
179 =head2 DESCRIPTION
180  
181 C<Plack::Test::Agent> is an OO interface to test PSGI applications. It can
182 perform GET, POST, PUT and DELETE requests against PSGI applications either in
183 process or over HTTP through a L<Plack::Handler> compatible backend.
184  
185 =head2 CONSTRUCTION
186  
187 =head3 C<new>
188  
189 The C<new> constructor creates an instance of C<Plack::Test::Agent>. This
190 constructor takes one mandatory named argument and several optional arguments.
191  
192 =over 4
193  
194 =item * C<app> is the mandatory argument. You must provide a PSGI application
195 to test.
196  
197 =item * C<server> is an optional argument. When provided, C<Plack::Test::Agent>
198 will attempt to start a PSGI handler and will communicate via HTTP to the
199 application running through the handler. See L<Plack::Loader> for details on
200 selecting the appropriate server.
201  
202 =item * C<host> is an optional argument representing the name or IP address for
203 the server to use. The default is C<localhost>.
204  
205 =item * C<port> is an optional argument representing the TCP port to for the
206 server to use. If not provided, the service will run on a randomly selected
207 available port outside of the IANA reserved range. (See L<Test::TCP> for
208 details on the selection of the port number.)
209  
210 =item * C<ua> is an optional argument of something which conforms to the
211 L<LWP::UserAgent> interface such that it provides a C<request> method which
212 takes an L<HTTP::Request> object and returns an L<HTTP::Response> object. The
213 default is an instance of C<LWP::UserAgent>.
214  
215 =item * C<jar> is an optional argument for a L<HTTP::Cookies> instance that
216 will be used as cookie jar for the requests, by default plain one is created.
217  
218 =back
219  
220 =head2 METHODS
221  
222 This class provides several useful methods:
223  
224 =head3 C<get>
225  
226 This method takes a URI and makes a C<GET> request against the PSGI application
227 with that URI. It returns an L<HTTP::Response> object representing the results
228 of that request.
229  
230 =head3 C<post>
231  
232 This method takes a URI and makes a C<POST> request against the PSGI
233 application with that URI. It returns an L<HTTP::Response> object representing
234 the results of that request. As an optional second parameter, pass an array
235 reference of key/value pairs for the form content:
236  
237 $agent->post( '/edit_user',
238 [
239 shoe_size => '10.5',
240 eye_color => 'blue green',
241 status => 'twin',
242 ]);
243  
244 =head3 C<put>
245  
246 This method takes a URI and makes a C<PUT> request against the PSGI
247 application with that URI. It returns an L<HTTP::Response> object representing
248 the results of that request. As an optional second parameter, pass an array
249 reference of key/value pairs for the form content:
250  
251 $agent->put( '/edit_user',
252 [
253 shoe_size => '10.5',
254 eye_color => 'blue green',
255 status => 'twin',
256 ]);
257  
258 =head3 C<delete>
259  
260 This method takes a URI and makes a C<DELETE> request against the PSGI
261 application with that URI. It returns an L<HTTP::Response> object representing
262 the results of that request.
263  
264 =head3 C<execute_request>
265  
266 This method takes an L<HTTP::Request>, performs it against the bound app, and
267 returns an L<HTTP::Response>. This allows you to craft your own requests
268 directly.
269  
270 =head3 C<get_mech>
271  
272 Used internally to create a default UserAgent, if none is provided to the
273 constructor. Returns a Test::WWW::Mechanize::Bound object.
274  
275 =head3 C<normalize_uri>
276  
277 Used internally to ensure that all requests use the correct scheme, host and
278 port. The scheme and host default to C<http> and C<localhost> respectively,
279 while the port is determined by L<Test::TCP>.
280  
281 =head3 C<start_server>
282  
283 Starts a test server via L<Test::TCP>. If a C<server> arg has been provided to
284 the constructor, it will use this class to load a server. Defaults to letting
285 Plack::Loader decide which server class to use.
286  
287 =head2 CREDITS
288  
289 Thanks to Zbigniew E<0x141>ukasiak and Tatsuhiko Miyagawa for suggestions.
290  
291 =head1 AUTHORS
292  
293 =over 4
294  
295 =item *
296  
297 chromatic <chromatic@wgz.org>
298  
299 =item *
300  
301 Dave Rolsky <autarch@urth.org>
302  
303 =item *
304  
305 Ran Eilam <ran.eilam@gmail.com>
306  
307 =item *
308  
309 Olaf Alders <olaf@wundercounter.com>
310  
311 =back
312  
313 =head1 CONTRIBUTORS
314  
315 =for stopwords Andy Beverley Dave Rolsky Olaf Alders Ran Eilam Syohei YOSHIDA Torsten Raudssus
316  
317 =over 4
318  
319 =item *
320  
321 Andy Beverley <andy@andybev.com>
322  
323 =item *
324  
325 Dave Rolsky <drolsky@maxmind.com>
326  
327 =item *
328  
329 Olaf Alders <oalders@maxmind.com>
330  
331 =item *
332  
333 Ran Eilam <reilam@maxmind.com>
334  
335 =item *
336  
337 Syohei YOSHIDA <syohex@gmail.com>
338  
339 =item *
340  
341 Torsten Raudssus <torsten@raudss.us>
342  
343 =back
344  
345 =head1 COPYRIGHT AND LICENSE
346  
347 This software is copyright (c) 2011 by chromatic.
348  
349 This is free software; you can redistribute it and/or modify it under
350 the same terms as the Perl 5 programming language system itself.
351  
352 =cut
353  
354 __END__
↑ Summary