Skip to content

Instantly share code, notes, and snippets.

@jberger
Last active April 28, 2021 18:04
Show Gist options
  • Save jberger/ba75a2c90d2061740ad0e2ca3a594f8f to your computer and use it in GitHub Desktop.
Save jberger/ba75a2c90d2061740ad0e2ca3a594f8f to your computer and use it in GitHub Desktop.
use Mojo::Base -strict, -signatures;
use Mojo::UserAgent;
use Mojolicious;
my $mock = Mojolicious->new;
$mock->routes->any('/' => sub ($c) { $c->render(data => $c->req->body) });
my @content = (qw(this is a test));
my $iterator = sub { shift @content };
# Build a normal transaction
my $ua = Mojo::UserAgent->new;
$ua->server->app($mock);
my $tx = $ua->build_tx(POST => '/');
# Start writing directly with a drain callback
my $drain = sub ($content, @) {
my $chunk = $iterator->();
if (length $chunk) {
$content->write_chunk($chunk, __SUB__);
} else {
$content->write_chunk('');
}
};
$tx->req->content->$drain;
my $got;
$ua->start_p($tx)->then(sub ($tx) { $got = $tx->res->body })->wait;
use Test::More;
is $got, 'thisisatest';
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment