Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
updated streaming upload test to use the new multipart events
  • Loading branch information
kraih committed Oct 19, 2011
1 parent 60f016a commit e4c7dd7
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions t/mojolicious/upload_stream_lite_app.t
Expand Up @@ -16,54 +16,65 @@ use Mojolicious::Lite;
use Scalar::Util 'weaken';
use Test::Mojo;

# Trigger early request for everything under "/upload"
# Trigger early request for multipart requests under "/upload"
app->hook(
after_build_tx => sub {
my $tx = shift;
weaken $tx;
$tx->req->content->on(
body => sub {
upgrade => sub {
$tx->emit('request') if $tx->req->url->path->contains('/upload');
}
);
}
);

# POST /upload
# POST /upload/*
my $cache = {};
post '/upload' => sub {
post '/upload/:id' => sub {
my $self = shift;

# First invocation, prepare streaming
# First invocation, locate part and prepare streaming
my $id = $self->param('id');
$self->req->content->on(read => sub { $cache->{$id} .= pop });
$self->req->content->on(
part => sub {
my ($multi, $single) = @_;
$single->on(
body => sub {
my $single = shift;
return unless $single->headers->content_disposition =~ /my_file/;
$single->on(read => sub { $cache->{$id} .= pop });
}
);
}
);
return unless $self->req->is_done;

# Second invocation, render response
$self->render(data => $cache->{$id});
};

# GET /download
get '/download' => sub {
# GET /download/*
get '/download/:id' => sub {
my $self = shift;
$self->render(data => $cache->{$self->param('id')});
};

my $t = Test::Mojo->new;

# POST /upload (small upload)
$t->post_ok('/upload?id=23' => 'whatever')->status_is(200)
->content_is('whatever');
# POST /upload/23 (small upload)
$t->post_form_ok('/upload/23' => {my_file => {content => 'whatever'}})
->status_is(200)->content_is('whatever');

# GET /download (small download)
$t->get_ok('/download?id=23')->status_is(200)->content_is('whatever');
# GET /download/23 (small download)
$t->get_ok('/download/23')->status_is(200)->content_is('whatever');

# POST /upload (big upload)
$t->post_ok('/upload?id=24' => '1234' x 131072)->status_is(200)
->content_is('1234' x 131072);
# POST /upload/24 (big upload)
$t->post_form_ok('/upload/24' => {my_file => {content => '1234' x 131072}})
->status_is(200)->content_is('1234' x 131072);

# GET /download (big download)
$t->get_ok('/download?id=24')->status_is(200)->content_is('1234' x 131072);
# GET /download/24 (big download)
$t->get_ok('/download/24')->status_is(200)->content_is('1234' x 131072);

# GET /download (small download again)
$t->get_ok('/download?id=23')->status_is(200)->content_is('whatever');
# GET /download/23 (small download again)
$t->get_ok('/download/23')->status_is(200)->content_is('whatever');

0 comments on commit e4c7dd7

Please sign in to comment.