Skip to content

Instantly share code, notes, and snippets.

@dotandimet
Created February 17, 2011 20:09
Show Gist options
  • Save dotandimet/832561 to your computer and use it in GitHub Desktop.
Save dotandimet/832561 to your computer and use it in GitHub Desktop.
mojo response streaming with redirection
#!/usr/bin/env perl
use warnings;
use strict;
use Test::More;
use Test::Mojo;
use Mojolicious::Lite;
post '/foo' => sub {
my $self = shift;
if ($self->param('fruit') && $self->param('fruit') eq 'apple') {
$self->session('fruit' => 'apple');
$self->redirect_to('blag');
}
$self->render(text => 'not blag!');
};
get '/blag' => sub {
my $self = shift;
my $fruit = $self->session('fruit') || 'null';
$self->render(text => $fruit);
};
# app->start;
my $t = Test::Mojo->new(max_redirects => 5);
# do a normal test:
$t->post_form_ok('/foo', {})->status_is(200)->content_is('not blag!');
$t->get_ok('/blag')->status_is(200)->content_like(qr/null/);
$t->post_form_ok('/foo', { fruit => 'banana'} )->status_is(200)->content_like(qr/null/);
$t->post_form_ok('/foo', { fruit => 'apple'})->status_is(200)->content_like(qr/apple/);
# Now do a streaming response:
my $tx = $t->client->build_form_tx('/foo', { fruit => 'apple' });
$tx->res->body(sub {
like($_[1], qr/apple/, 'streaming with redirection');
print "Processing chunk: ", $_[1];
} );
$t->client->start($tx);
done_testing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment