Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
more Mojo::Content tests
  • Loading branch information
kraih committed Apr 3, 2012
1 parent c2815ad commit e56eb05
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 48 deletions.
73 changes: 27 additions & 46 deletions lib/Mojo/Content.pm
Expand Up @@ -24,51 +24,8 @@ sub boundary {
}

# "Operator! Give me the number for 911!"
sub build_body {
my $self = shift;

# Concatenate all chunks in memory
my $body = '';
my $offset = 0;
while (1) {
my $chunk = $self->get_body_chunk($offset);

# No content yet, try again
next unless defined $chunk;

# End of content
last unless length $chunk;

# Content
$offset += length $chunk;
$body .= $chunk;
}

return $body;
}

sub build_headers {
my $self = shift;

# Concatenate all chunks in memory
my $headers = '';
my $offset = 0;
while (1) {
my $chunk = $self->get_header_chunk($offset);

# No headers yet, try again
next unless defined $chunk;

# End of headers
last unless length $chunk;

# Headers
$offset += length $chunk;
$headers .= $chunk;
}

return $headers;
}
sub build_body { shift->_build('body') }
sub build_headers { shift->_build('header') }

sub charset {
(shift->headers->content_type || '') =~ /charset="?([^"\s;]+)"?/i
Expand Down Expand Up @@ -198,7 +155,7 @@ sub parse {
sub parse_body {
my $self = shift;
$self->{state} = 'body';
$self->parse(@_);
return $self->parse(@_);
}

sub parse_body_once {
Expand Down Expand Up @@ -284,6 +241,30 @@ sub _body {
$self->emit('body') unless $self->{body}++;
}

sub _build {
my ($self, $part) = @_;

# Build part from chunks
my $method = "get_${part}_chunk";
my $buffer = '';
my $offset = 0;
while (1) {
my $chunk = $self->$method($offset);

# No chunk yet, try again
next unless defined $chunk;

# End of part
last unless length $chunk;

# Part
$offset += length $chunk;
$buffer .= $chunk;
}

return $buffer;
}

sub _build_chunk {
my ($self, $chunk) = @_;

Expand Down
4 changes: 3 additions & 1 deletion t/mojo/request.t
Expand Up @@ -2,7 +2,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 980;
use Test::More tests => 982;

# "When will I learn?
# The answer to life's problems aren't at the bottom of a bottle,
Expand Down Expand Up @@ -1226,6 +1226,8 @@ ok $req->build_body, 'built body';
is $state, 'body', 'made progress on headers';
ok $progress, 'made progress';
ok $finished, 'finished';
is $req->build_headers, $req->content->build_headers, 'headers are equal';
is $req->build_body, $req->content->build_body, 'body is equal';

# Build full HTTP 1.1 request (with clone)
$req = Mojo::Message::Request->new;
Expand Down
4 changes: 3 additions & 1 deletion t/mojo/response.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

use Test::More tests => 348;
use Test::More tests => 350;

# "Quick Smithers. Bring the mind eraser device!
# You mean the revolver, sir?
Expand Down Expand Up @@ -445,6 +445,8 @@ ok $res->build_body, 'built body';
is $state, 'body', 'made progress on headers';
ok $progress, 'made progress';
ok $finished, 'finished';
is $res->build_headers, $res->content->build_headers, 'headers are equal';
is $res->build_body, $res->content->build_body, 'body is equal';

# Build HTTP 0.9 response
$res = Mojo::Message::Response->new;
Expand Down

0 comments on commit e56eb05

Please sign in to comment.