/usr/share/perl5/Plack/Test/AnyEvent.pm is in libplack-test-anyevent-perl 0.06-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | ## no critic (RequireUseStrict)
package Plack::Test::AnyEvent;
$Plack::Test::AnyEvent::VERSION = '0.06';
## use critic (RequireUseStrict)
use strict;
use warnings;
use autodie qw(pipe);
use AnyEvent::Handle;
use Carp;
use HTTP::Request;
use HTTP::Message::PSGI;
use IO::Handle;
use Plack::Test::AnyEvent::Response;
# code adapted from Plack::Test::MockHTTP
sub test_psgi {
my ( %args ) = @_;
my $client = delete $args{client} or croak "client test code needed";
my $app = delete $args{app} or croak "app needed";
my $cb = sub {
my ( $req ) = @_;
$req->uri->scheme('http') unless defined $req->uri->scheme;
$req->uri->host('localhost') unless defined $req->uri->host;
my $env = $req->to_psgi;
$env->{'psgi.streaming'} = 1;
$env->{'psgi.nonblocking'} = 1;
my $res = $app->($env);
if(ref($res) eq 'CODE') {
my ( $status, $headers, $body );
my ( $read, $write );
my $cond = AnyEvent->condvar;
$res->(sub {
my ( $ref ) = @_;
( $status, $headers, $body ) = @$ref;
$cond->send;
unless(defined $body) {
pipe $read, $write;
$write = IO::Handle->new_from_fd($write, 'w');
$write->autoflush(1);
return $write;
}
});
unless(defined $status) {
local $SIG{__DIE__} = __PACKAGE__->exception_handler($cond);
my $ex = $cond->recv;
die $ex if defined $ex;
}
if(defined $body) {
$res = Plack::Test::AnyEvent::Response->from_psgi([ $status, $headers, $body ]);
$res->{'_cond'} = AnyEvent->condvar;
$res->{'_cond'}->send;
} else {
push @$headers, 'Transfer-Encoding', 'chunked';
$res = Plack::Test::AnyEvent::Response->from_psgi([ $status, $headers, [] ]);
my $h;
$res->{'_cond'} = AnyEvent->condvar(cb => sub {
undef $h;
close $read;
close $write;
});
$res->on_content_received(sub {});
$h = AnyEvent::Handle->new(
fh => $read,
on_read => sub {
my $buf = $h->rbuf;
$h->rbuf = '';
$res->content($res->content . $buf);
$res->on_content_received->($buf);
},
on_eof => sub {
$res->send;
},
on_error => sub {
my ( undef, undef, $msg ) = @_;
warn $msg;
$res->send;
},
);
}
} else {
unless(ref($res) eq 'Plack::Test::AnyEvent::Response') {
$res = Plack::Test::AnyEvent::Response->from_psgi($res);
}
my $cond = AnyEvent->condvar;
$res->{'_cond'} = $cond;
$res->on_content_received(sub {});
# make sure that the on_content_received callback is invoked inside
# of the event loop
my $faux_timer;
$faux_timer = AnyEvent->timer(
after => 0.001,
cb => sub {
undef $faux_timer;
$res->on_content_received->($res->content);
$cond->send;
},
);
$res->request($req);
}
return $res;
};
$client->($cb);
}
sub exception_handler {
my ( $class, $cond ) = @_;
return sub {
my $i = 0;
my @last_eval_frame;
while(my @info = caller($i)) {
my ( $subroutine, $evaltext ) = @info[3, 6];
if($subroutine eq '(eval)' && !defined($evaltext)) {
@last_eval_frame = caller($i + 1);
last;
}
} continue {
$i++;
}
if(@last_eval_frame) {
my ( $subroutine ) = $last_eval_frame[3];
if($subroutine =~ /^AnyEvent::Impl|AnyEvent::CondVar::Base/) {
$cond->send($_[0]);
}
}
};
}
1;
=pod
=encoding UTF-8
=head1 NAME
Plack::Test::AnyEvent - Run Plack::Test on AnyEvent-based PSGI applications
=head1 VERSION
version 0.06
=head1 SYNOPSIS
use HTTP::Request::Common;
use Plack::Test;
$Plack::Test::Impl = 'AnyEvent'; # or 'AE' for short
test_psgi $app, sub {
my ( $cb ) = @_;
my $res = $cb->(GET '/streaming-response');
is $res->header('Transfer-Encoding'), 'chunked';
$res->on_content_received(sub {
my ( $content ) = @_;
# test chunk of streaming response
});
$res->recv;
}
=head1 DESCRIPTION
This L<Plack::Test> implementation allows you to easily test your
L<AnyEvent>-based PSGI applications. Normally, L<Plack::Test::MockHTTP>
or L<Plack::Test::Server> work fine for this, but this implementation comes
in handy when you'd like to test your streaming results as they come in, or
if your application uses long-polling. For non-streaming requests, you can
use this module exactly like Plack::Test::MockHTTP; otherwise, you can set
up a content handler and call C<$res-E<gt>recv>. The event loop will then
run until the PSGI application closes its writer handle or until your test
client calls C<send> on the response.
=head1 FUNCTIONS
=head2 test_psgi
This function behaves almost identically to L<Plack::Test/test_psgi>; the
main difference is that the returned response object supports a few additional
methods on top of those normally found in an L<HTTP::Response> object:
=head3 $res->recv
Calls C<recv> on an internal AnyEvent condition variable. Use this after you
get the response object to run the event loop.
=head3 $res->send
Calls C<send> on an internal AnyEvent condition variable. Use this to stop
the event loop when you're done testing.
=head3 $res->on_content_received($cb)
Sets a callback to be called when a chunk is received from the application.
A single argument is passed to the callback; namely, the chunk itself.
=head1 EXCEPTION HANDLING
As of version 0.02, this module handles uncaught exceptions thrown by your code.
If the exception occurs before your PSGI application returns a response, or
directly in the response subroutine ref (if you return a subroutine as your
application's response), C<$cb> will propagate the exception. Otherwise,
the exception is propagated by C<$res-E<gt>recv>. Here's an example:
my $app = sub {
die 'thrown by $cb';
return sub {
my ( $respond ) = @_;
die 'still thrown by $cb';
if($streaming) {
my $writer = $respond->([
200,
['Content-Type' => 'text/plain'],
]);
die 'still thrown by $cb';
my $timer;
$timer = AnyEvent->timer(
after => 2,
cb => sub {
die 'thrown by $res->recv';
$writer->write("Ok");
$writer->close;
undef $timer;
},
);
} else {
$respond->([
200,
['Content-Type' => 'text/plain'],
['Ok'],
]);
die 'still thrown by $cb';
}
};
};
test_psgi $app, sub {
my ( $cb ) = @_;
my $res = $cb->(GET '/');
$res->on_content_received(sub {
...
});
$res->recv;
};
Note: The exception handling code may or may not work with your event loop.
Please run the tests in this distribution with
L<AnyEvent/"PERL_ANYEVENT_MODEL"> set to see if it works with your event loop
of choice. Patches will be accepted to accommodate loops, as long as it
doesn't break known good ones. The known good event loops are:
=over
=item Default
=item Cocoa
=item EV
=item Event
=item Glib
=item Perl
=back
This list isn't exclusive; ie. just because your event loop isn't on this list
doesn't mean it doesn't work. Also, even if your event loop doesn't pass
the exception tests, the general usage of this module (testing requests,
handling streaming results and long polling) should work on any AnyEvent loop.
Just don't throw any uncaught exceptions =).
=head1 SEE ALSO
L<AnyEvent>, L<Plack>, L<Plack::Test>
=begin comment
=over
=item exception_handler
=back
=end comment
=head1 AUTHOR
Rob Hoelz <rob@hoelz.ro>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by Rob Hoelz.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website
https://github.com/hoelzro/plack-test-anyevent/issues
When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.
=cut
__END__
# ABSTRACT: Run Plack::Test on AnyEvent-based PSGI applications
|