Skip to content

Commit

Permalink
a few more asset tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 18, 2014
1 parent 3a67549 commit 959157c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 22 deletions.
13 changes: 4 additions & 9 deletions lib/Mojo/Asset/File.pm
Expand Up @@ -78,8 +78,7 @@ sub contains {
# Search window
my $pos = index $window, $str;
return $offset + $pos if $pos >= 0;
$offset += $read;
return -1 if $read == 0 || $offset == $end;
return -1 if $read == 0 || ($offset += $read) == $end;

# Resize window
substr $window, 0, $read, '';
Expand All @@ -98,8 +97,7 @@ sub get_chunk {

my $buffer;
if (defined(my $end = $self->end_range)) {
my $chunk = $end + 1 - $offset;
return '' if $chunk <= 0;
return '' if (my $chunk = $end + 1 - $offset) <= 0;
$handle->sysread($buffer, $chunk > $max ? $max : $chunk);
}
else { $handle->sysread($buffer, $max) }
Expand Down Expand Up @@ -128,11 +126,8 @@ sub size {
}

sub slurp {
my $handle = shift->handle;
$handle->sysseek(0, SEEK_SET);
my $content = '';
while ($handle->sysread(my $buffer, 131072)) { $content .= $buffer }
return $content;
return '' unless defined(my $file = shift->path);
return Mojo::Util::slurp $file;
}

1;
Expand Down
7 changes: 2 additions & 5 deletions lib/Mojo/Base.pm
Expand Up @@ -70,11 +70,8 @@ sub attr {
$code .= ref $default eq 'CODE' ? '$default->($_[0]);' : '$default;';
}

# Store value
$code .= "\n }\n \$_[0]{'$attr'} = \$_[1];\n";

# Footer (return invocant)
$code .= " \$_[0];\n}";
# Footer (store value and return invocant)
$code .= "\n }\n \$_[0]{'$attr'} = \$_[1];\n \$_[0];\n}";

warn "-- Attribute $attr in $class\n$code\n\n" if $ENV{MOJO_BASE_DEBUG};
Carp::croak "Mojo::Base error: $@" unless eval "$code;1";
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/Content.pm
Expand Up @@ -10,17 +10,17 @@ has headers => sub { Mojo::Headers->new };
has max_buffer_size => sub { $ENV{MOJO_MAX_BUFFER_SIZE} || 262144 };
has max_leftover_size => sub { $ENV{MOJO_MAX_LEFTOVER_SIZE} || 262144 };

my $BOUNDARY_RE
= qr!multipart.*boundary\s*=\s*(?:"([^"]+)"|([\w'(),.:?\-+/]+))!i;

sub body_contains {
croak 'Method "body_contains" not implemented by subclass';
}

sub body_size { croak 'Method "body_size" not implemented by subclass' }

sub boundary {
return undef unless my $type = shift->headers->content_type;
$type =~ m!multipart.*boundary\s*=\s*(?:"([^"]+)"|([\w'(),.:?\-+/]+))!i
and return $1 // $2;
return undef;
(shift->headers->content_type // '') =~ $BOUNDARY_RE ? $1 // $2 : undef;
}

sub build_body { shift->_build('get_body_chunk') }
Expand Down
6 changes: 2 additions & 4 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -223,10 +223,8 @@ sub _parse_env {
$self->method($env->{REQUEST_METHOD}) if $env->{REQUEST_METHOD};

# Scheme/Version
if (($env->{SERVER_PROTOCOL} // '') =~ m!^([^/]+)/([^/]+)$!) {
$base->scheme($1);
$self->version($2);
}
$base->scheme($1) and $self->version($2)
if ($env->{SERVER_PROTOCOL} // '') =~ m!^([^/]+)/([^/]+)$!;

# HTTPS
$base->scheme('https') if uc($env->{HTTPS} // '') eq 'ON';
Expand Down
2 changes: 2 additions & 0 deletions t/mojo/asset.t
Expand Up @@ -9,6 +9,8 @@ use Mojo::Asset::Memory;

# File asset
my $file = Mojo::Asset::File->new;
is $file->size, 0, 'file is empty';
is $file->slurp, '', 'file is empty';
$file->add_chunk('abc');
is $file->contains('abc'), 0, '"abc" at position 0';
is $file->contains('bc'), 1, '"bc" at position 1';
Expand Down

0 comments on commit 959157c

Please sign in to comment.