Skip to content

Commit

Permalink
fixed Windows portability issue in upload_lite_app.t
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 21, 2011
1 parent 78e925d commit 7fd6f62
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 120 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@ This file documents the revision history for Perl extension Mojolicious.
2.04 2011-10-21 00:00:00
- Improved documentation.
- Fixed small HTTP response parser bug.
- Fixed Windows portability issue in "upload_lite_app.t".

2.03 2011-10-20 00:00:00
- Deprecated all is_done methods in favor of is_finished.
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojo/Content.pm
Expand Up @@ -283,6 +283,7 @@ sub parse_until_body {

sub progress {
my $self = shift;
return 0 unless ($self->{state} || '') ~~ [qw/body finished/];
return $self->{raw_size} - ($self->{header_size} || 0);
}

Expand Down Expand Up @@ -637,9 +638,9 @@ Parse chunk and stop after headers.
=head2 C<progress>
my $bytes = $content->progress;
my $size = $content->progress;
Number of bytes already received from message content.
Size of content already received from message in bytes.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<write>
Expand Down
11 changes: 2 additions & 9 deletions lib/Mojo/Message.pm
Expand Up @@ -591,16 +591,9 @@ Emitted when message building or parsing makes progress.
$message->on(progress => sub {
my $message = shift;
# Make sure we have enough information
return
unless $message->content->is_parsing_body || $message->is_finished;
return unless my $len = $message->headers->content_length;
my $progress = $message->content->progress;
# Calculate progress
say 'Progress: ',
$progress == $len ? 100 : int($progress / ($len / 100)), '%';
my $size = $message->content->progress;
say 'Progress: ', $size == $len ? 100 : int($size / ($len / 100)), '%';
});
=head1 ATTRIBUTES
Expand Down
14 changes: 13 additions & 1 deletion t/mojo/message.t
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 1310;
use Test::More tests => 1322;

use File::Spec;
use File::Temp;
Expand Down Expand Up @@ -253,13 +253,19 @@ is $req->headers->content_length, undef, 'no "Content-Length" value';
my $backup = $ENV{MOJO_MAX_MEMORY_SIZE} || '';
$ENV{MOJO_MAX_MEMORY_SIZE} = 12;
$req = Mojo::Message::Request->new;
is $req->content->progress, 0, 'right progress';
$req->parse('GET /foo/bar/baz.html?fo');
is $req->content->progress, 0, 'right progress';
$req->parse("o=13#23 HTTP/1.0\x0d\x0aContent");
$req->parse('-Type: text/');
is $req->content->progress, 0, 'right progress';
$req->parse("plain\x0d\x0aContent-Length: 27\x0d\x0a\x0d\x0aHell");
is $req->content->progress, 4, 'right progress';
$req->parse("o World!\n");
is $req->content->progress, 13, 'right progress';
is $req->content->asset->isa('Mojo::Asset::Memory'), 1, 'stored in memory';
$req->parse("1234\nlalalala\n");
is $req->content->progress, 27, 'right progress';
is $req->content->asset->isa('Mojo::Asset::File'), 1, 'stored in file';
ok $req->is_finished, 'request is finished';
is $req->method, 'GET', 'right method';
Expand Down Expand Up @@ -643,11 +649,16 @@ is $req->content->asset->slurp, 'abcdabcdefghi', 'right content';

# Parse HTTP 1.1 multipart request
$req = Mojo::Message::Request->new;
is $req->content->progress, 0, 'right progress';
$req->parse("GET /foo/bar/baz.html?foo13#23 HTTP/1.1\x0d\x0a");
is $req->content->progress, 0, 'right progress';
$req->parse("Content-Length: 418\x0d\x0a");
$req->parse('Content-Type: multipart/form-data; bo');
is $req->content->progress, 0, 'right progress';
$req->parse("undary=----------0xKhTmLbOuNdArY\x0d\x0a\x0d\x0a");
is $req->content->progress, 0, 'right progress';
$req->parse("\x0d\x0a------------0xKhTmLbOuNdArY\x0d\x0a");
is $req->content->progress, 31, 'right progress';
$req->parse("Content-Disposition: form-data; name=\"text1\"\x0d\x0a");
$req->parse("\x0d\x0ahallo welt test123\n");
$req->parse("\x0d\x0a------------0xKhTmLbOuNdArY\x0d\x0a");
Expand All @@ -661,6 +672,7 @@ $req->parse("use strict;\n");
$req->parse("use warnings;\n\n");
$req->parse("print \"Hello World :)\\n\"\n");
$req->parse("\x0d\x0a------------0xKhTmLbOuNdArY--");
is $req->content->progress, 418, 'right progress';
ok $req->is_finished, 'request is finished';
is $req->is_multipart, 1, 'multipart content';
is $req->method, 'GET', 'right method';
Expand Down
109 changes: 1 addition & 108 deletions t/mojolicious/upload_lite_app.t
@@ -1,50 +1,20 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use utf8;

# Disable Bonjour, IPv6 and libev
BEGIN {
$ENV{MOJO_NO_BONJOUR} = $ENV{MOJO_NO_IPV6} = 1;
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 31;
use Test::More tests => 18;

# "Um, Leela,
# Armondo and I are going to the back seat of his car for coffee."
use Mojo::Asset::File;
use Mojo::ByteStream 'b';
use Mojolicious::Lite;
use Test::Mojo;

# Upload progress
my $cache = {};
app->hook(
after_build_tx => sub {
shift->req->on(
progress => sub {
my $req = shift;

# Check if we've reached the body yet
return unless $req->content->is_parsing_body || $req->is_finished;

# Check for id
return unless my $id = $req->url->query->param('upload_id');

# Check for content length
return
unless my $len = $req->headers->content_length;

# Update cache with current progress
my $progress = $req->content->progress;
push @{$cache->{$id} ||= [0]},
$progress == $len ? 100 : int($progress / ($len / 100));
}
);
}
);

# POST /upload
post '/upload' => sub {
my $self = shift;
Expand Down Expand Up @@ -79,25 +49,6 @@ post '/multi' => sub {
. $file2->asset->slurp);
};

# GET /progress
get '/progress/:id' => sub {
my $self = shift;
my $id = $self->param('id');
$self->render_text(($cache->{$id}->[-1] || 0) . '%');
};

# POST /uploadlimit
post '/uploadlimit' => sub {
my $self = shift;
$self->rendered;
my $body = $self->res->body || '';
$self->res->body("called, $body");
return if $self->req->is_limit_exceeded;
if (my $u = $self->req->upload('Вячеслав')) {
$self->res->body($self->res->body . b($u->filename)->encode . $u->size);
}
};

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

# POST /upload (asset and filename)
Expand All @@ -119,11 +70,6 @@ my $hash = {content => 'alalal', 'Content-Type' => 'foo/bar', 'X-X' => 'Y'};
$t->post_form_ok('/upload', {file => $hash, test => 'tset'})->status_is(200)
->content_is('filealalaltsetfoo/barY');

# POST /upload (with progress)
$t->post_form_ok('/upload?upload_id=23',
{file => {content => 'alalal'}, test => 'tset'})->status_is(200)
->content_is('filealalaltsetapplication/octet-stream');

# POST /multi_reverse
$t->post_form_ok('/multi_reverse',
{file1 => {content => '1111'}, file2 => {content => '11112222'},})
Expand All @@ -133,56 +79,3 @@ $t->post_form_ok('/multi_reverse',
$t->post_form_ok('/multi',
{file1 => {content => '1111'}, file2 => {content => '11112222'},})
->status_is(200)->content_is('file11111file211112222');

# GET/progress/23
$t->get_ok('/progress/23')->status_is(200)->content_is('100%');
ok @{$cache->{23}} > 1, 'made progress';
ok $cache->{23}->[0] < $cache->{23}->[-1], 'progress increased';

my $ua = $t->ua;

# POST /uploadlimit (huge upload without appropriate max message size)
my $backup = $ENV{MOJO_MAX_MESSAGE_SIZE} || '';
$ENV{MOJO_MAX_MESSAGE_SIZE} = 655360;
my $tx = Mojo::Transaction::HTTP->new;
my $part = Mojo::Content::Single->new;
my $name = b('Вячеслав')->url_escape;
$part->headers->content_disposition(
qq/form-data; name="$name"; filename="$name.jpg"/);
$part->headers->content_type('image/jpeg');
$part->asset->add_chunk('1234' x 1310720);
my $content = Mojo::Content::MultiPart->new;
$content->headers($tx->req->headers);
$content->headers->content_type('multipart/form-data');
$content->parts([$part]);
$tx->req->method('POST');
$tx->req->url->parse('/uploadlimit');
$tx->req->content($content);
$ua->start($tx);
is $tx->res->code, 413, 'right status';
is $tx->res->body, 'called, ', 'right content';
$ENV{MOJO_MAX_MESSAGE_SIZE} = $backup;

# POST /uploadlimit (huge upload with appropriate max message size)
$backup = $ENV{MOJO_MAX_MESSAGE_SIZE} || '';
$ENV{MOJO_MAX_MESSAGE_SIZE} = 1073741824;
$tx = Mojo::Transaction::HTTP->new;
$part = Mojo::Content::Single->new;
$name = b('Вячеслав')->encode->url_escape;
$part->headers->content_disposition(
qq/form-data; name="$name"; filename="$name.jpg"/);
$part->headers->content_type('image/jpeg');
$part->asset->add_chunk('1234' x 1310720);
$content = Mojo::Content::MultiPart->new;
$content->headers($tx->req->headers);
$content->headers->content_type('multipart/form-data');
$content->parts([$part]);
$tx->req->method('POST');
$tx->req->url->parse('/uploadlimit');
$tx->req->content($content);
$ua->start($tx);
ok $tx->is_finished, 'transaction is finished';
is $tx->res->code, 200, 'right status';
is b($tx->res->body)->decode('UTF-8')->to_string,
'called, Вячеслав.jpg5242880', 'right content';
$ENV{MOJO_MAX_MESSAGE_SIZE} = $backup;

0 comments on commit 7fd6f62

Please sign in to comment.