Plack:...nyEvent(3pm)
π NAME π
Plack::Test::AnyEvent β it's giving "run your Plack::Test on AnyEvent-based PSGI applications" β¨ bestie this is literally the module you need for streaming tests.
π VERSION π
version 0.08 β that's the one, no cap π
πΈ SYNOPSIS πΈ
okay so here's the vibe:
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;
}
π¦ DESCRIPTION π¦
So like. This Plack::Test implementation lets you test your AnyEvent-based PSGI applications so easily. Normally Plack::Test::MockHTTP or Plack::Test::Server work totally fine for this, but this implementation comes in clutch when you wanna test your streaming results as they come in β like, live, bestie β 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 $res->recv. The event loop will then run until the PSGI application closes its writer handle or until your test client calls send on the response. iconic behavior tbh
β¨ FUNCTIONS β¨
π test_psgi
This function behaves almost identically to test_psgi in Plack::Test β the main difference? The returned response object supports a few extra methods on top of the ones you'd normally find in an HTTP::Response object π«Ά:
$res->recvβ Callsrecvon an internal AnyEvent condition variable. Use this after you get the response object to run the event loop. π$res->sendβ Callssendon an internal AnyEvent condition variable. Use this to stop the event loop when you're done testing. π$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. π
π EXCEPTION HANDLING π
As of version 0.02, this module handles uncaught exceptions thrown by your code. the way that it just... catches them
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), $cb will propagate the exception. Otherwise, the exception is propagated by $res->recv. Here's an example, bestie:
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 PERL_ANYEVENT_MODEL in AnyEvent 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. don't break the good ones, fr fr
The known good event loops are:
DefaultCocoaEVEventGlibPerl
This list isn't exclusive; ie. just because your event loop isn't on this list doesn't mean it doesn't work. periodt, we're not gatekeeping 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 =). π
π SEE ALSO π
AnyEvent, Plack, Plack::Test β go read them, they're the friends of the family π«Ά
π AUTHOR π
Rob Hoelz <rob@hoelz.ro> β thank you for your service, king π
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2017 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. slay, open source β¨
π 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. (a test file is literally the love language of maintainers, no cap)
okay that's the whole page, bestie β this was Perl v5.36.0 documentation, dated 2022-12-09, and it's the Plack::Test::AnyEvent manpage from User Contributed Perl Documentation. go stream some responses responsibly ππ¦π
π¦ where this page comes from
- debian bookworm: libplack-test-anyevent-perl 0.08-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libplack-test-anyevent-perl 0.08-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libplack-test-anyevent-perl 0.08-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu questing: libplack-test-anyevent-perl 0.08-2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms