Skip to content

Commit

Permalink
fixed another state bug in Mojo::Content::MultiPart
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 16, 2012
1 parent be13816 commit b91f4d4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Changes
Expand Up @@ -2,7 +2,7 @@
3.58 2012-11-16
- Improved documentation.
- Improved tests.
- Fixed state bug in Mojo::Content::MultiPart.
- Fixed state bugs in Mojo::Content::MultiPart.

3.57 2012-11-12
- Deprecated Mojo::Exception->raw_message.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Content/MultiPart.pm
Expand Up @@ -181,7 +181,7 @@ sub _read {
$self->{multipart} .= $chunk;
$self->{multi_state} ||= 'multipart_preamble';
my $boundary = $self->boundary;
until ($self->is_finished) {
until ($self->{multi_state} eq 'finished') {

# Preamble
if (($self->{multi_state} // '') eq 'multipart_preamble') {
Expand Down
63 changes: 34 additions & 29 deletions t/mojo/response.t
Expand Up @@ -334,7 +334,8 @@ is $res->headers->content_length, undef, 'right "Content-Length" value';
is $res->code, 200, 'right status';
is $res->message, 'OK', 'right message';
is $res->version, '1.1', 'right version';
ok $res->headers->content_type =~ m!multipart/form-data!,
is $res->headers->content_type,
'multipart/form-data; boundary=----------0xKhTmLbOuNdArY',
'right "Content-Type" value';
}

Expand Down Expand Up @@ -403,42 +404,45 @@ ok $res->is_finished, 'response is finished';
is $res->code, 200, 'right status';
is $res->message, 'OK', 'right message';
is $res->version, '1.1', 'right version';
ok $res->headers->content_type =~ m!multipart/form-data!,
is $res->headers->content_type,
'multipart/form-data; boundary=----------0xKhTmLbOuNdArY',
'right "Content-Type" value';
isa_ok $res->content->parts->[0], 'Mojo::Content::Single', 'right part';
isa_ok $res->content->parts->[1], 'Mojo::Content::Single', 'right part';
isa_ok $res->content->parts->[2], 'Mojo::Content::Single', 'right part';
is $res->content->parts->[0]->asset->slurp, "hallo welt test123\n",
'right content';

# Parse HTTP 1.1 chunked multipart response
$res = Mojo::Message::Response->new;
$res->parse("HTTP/1.1 200 OK\x0d\x0a");
$res->parse("Transfer-Encoding: chunked\x0d\x0a");
$res->parse('Content-Type: multipart/form-data; bo');
$res->parse("undary=----------0xKhTmLbOuNdArY\x0d\x0a\x0d\x0a");
$res->parse("1a1\x0d\x0a------------0xKhTmLbOuNdArY\x0d\x0a");
$res->parse("Content-Disposition: form-data; name=\"text1\"\x0d\x0a");
$res->parse("\x0d\x0ahallo welt test123\n");
$res->parse("\x0d\x0a------------0xKhTmLbOuNdArY\x0d\x0a");
$res->parse("Content-Disposition: form-data; name=\"text2\"\x0d\x0a");
$res->parse("\x0d\x0a\x0d\x0a------------0xKhTmLbOuNdArY\x0d\x0a");
$res->parse('Content-Disposition: form-data; name="upload"; file');
$res->parse("name=\"hello.pl\"\x0d\x0a\x0d\x0a");
$res->parse("Content-Type: application/octet-stream\x0d\x0a\x0d\x0a");
$res->parse("#!/usr/bin/perl\n\n");
$res->parse("use strict;\n");
$res->parse("use warnings;\n\n");
$res->parse("print \"Hello World :)\\n\"\n");
$res->parse("\x0d\x0a------------0xKhTmLbOuNdA");
$res->parse("r\x0d\x0a3\x0d\x0aY--\x0d\x0a");
ok !$res->is_finished, 'response is not finished';
$res->parse("0\x0d\x0a\x0d\x0a");
# Parse HTTP 1.1 chunked multipart response (at once)
$res = Mojo::Message::Response->new;
my $multipart
= "HTTP/1.1 200 OK\x0d\x0a"
. "Transfer-Encoding: chunked\x0d\x0a"
. 'Content-Type: multipart/form-data; bo'
. "undary=----------0xKhTmLbOuNdArY\x0d\x0a\x0d\x0a"
. "1a1\x0d\x0a------------0xKhTmLbOuNdArY\x0d\x0a"
. "Content-Disposition: form-data; name=\"text1\"\x0d\x0a"
. "\x0d\x0ahallo welt test123\n"
. "\x0d\x0a------------0xKhTmLbOuNdArY\x0d\x0a"
. "Content-Disposition: form-data; name=\"text2\"\x0d\x0a"
. "\x0d\x0a\x0d\x0a------------0xKhTmLbOuNdArY\x0d\x0a"
. 'Content-Disposition: form-data; name="upload"; file'
. "name=\"hello.pl\"\x0d\x0a\x0d\x0a"
. "Content-Type: application/octet-stream\x0d\x0a\x0d\x0a"
. "#!/usr/bin/perl\n\n"
. "use strict;\n"
. "use warnings;\n\n"
. "print \"Hello World :)\\n\"\n"
. "\x0d\x0a------------0xKhTmLbOuNdA"
. "r\x0d\x0a3\x0d\x0aY--\x0d\x0a"
. "0\x0d\x0a\x0d\x0a";
$res->parse($multipart);
ok $res->is_finished, 'response is finished';
is $res->code, 200, 'right status';
is $res->message, 'OK', 'right message';
is $res->version, '1.1', 'right version';
ok $res->headers->content_type =~ m!multipart/form-data!,
is $res->headers->content_type,
'multipart/form-data; boundary=----------0xKhTmLbOuNdArY',
'right "Content-Type" value';
is $res->headers->content_length, 420, 'right "Content-Length" value';
is $res->headers->transfer_encoding, undef, 'no "Transfer-Encoding" value';
Expand All @@ -449,7 +453,7 @@ isa_ok $res->content->parts->[2], 'Mojo::Content::Single', 'right part';
is $res->content->parts->[0]->asset->slurp, "hallo welt test123\n",
'right content';

# Parse HTTP 1.1 chunked multipart response with many small chunks
# Parse HTTP 1.1 chunked multipart response (in multiple small chunks)
$res = Mojo::Message::Response->new;
$res->parse("HTTP/1.1 200 OK\x0d\x0a");
$res->parse("Transfer-Encoding: chunked\x0d\x0a");
Expand Down Expand Up @@ -479,7 +483,8 @@ ok $res->is_finished, 'response is finished';
is $res->code, 200, 'right status';
is $res->message, 'OK', 'right message';
is $res->version, '1.1', 'right version';
ok $res->headers->content_type =~ m!multipart/parallel!,
is $res->headers->content_type,
'multipart/parallel; boundary=AAA; charset=utf-8',
'right "Content-Type" value';
is $res->headers->content_length, 129, 'right "Content-Length" value';
is $res->headers->transfer_encoding, undef, 'no "Transfer-Encoding" value';
Expand Down Expand Up @@ -520,7 +525,7 @@ ok $res->is_finished, 'response is finished';
is $res->code, 200, 'right status';
is $res->message, 'OK', 'right message';
is $res->version, '1.1', 'right version';
ok $res->headers->content_type =~ m!multipart/form-data!,
is $res->headers->content_type, 'multipart/form-data; bo',
'right "Content-Type" value';
isa_ok $res->content, 'Mojo::Content::Single', 'right content';
like $res->content->asset->slurp, qr/hallo welt/, 'right content';
Expand Down

0 comments on commit b91f4d4

Please sign in to comment.