Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed file and content type detection bugs in Mojolicious::Static
  • Loading branch information
kraih committed Jul 19, 2012
1 parent 4815ea9 commit 50c9613
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -8,6 +8,8 @@
- Improved text_field helper to always set the type attribute. (marty, sri)
- Improved documentation.
- Improved tests.
- Fixed file and content type detection bugs in Mojolicious::Static.
(marty, sri)

3.11 2012-07-19
- Added or method to Test::Mojo. (moritz, sri)
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Static.pm
Expand Up @@ -54,8 +54,8 @@ sub file {
sub serve {
my ($self, $c, $rel) = @_;
return unless my $asset = $self->file($rel);
$rel =~ /\.(\w+)$/;
$c->res->headers->content_type($c->app->types->type($1) || 'text/plain');
my $type = $rel =~ /\.(\w+)$/ ? $c->app->types->type($1) : undef;
$c->res->headers->content_type($type || 'text/plain');
return $self->serve_asset($c, $asset);
}

Expand Down Expand Up @@ -126,7 +126,7 @@ sub _get_data_file {
sub _get_file {
my ($self, $path) = @_;
no warnings 'newline';
return -r $path ? Mojo::Asset::File->new(path => $path) : undef;
return -f $path && -r $path ? Mojo::Asset::File->new(path => $path) : undef;
}

1;
Expand Down
13 changes: 12 additions & 1 deletion t/mojolicious/app.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 327;
use Test::More tests => 337;

use FindBin;
use lib "$FindBin::Bin/lib";
Expand Down Expand Up @@ -224,6 +224,17 @@ $t->get_ok('/', {'X-Test' => 'Hi there!'})->status_is(404)
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/Page not found/);

# Static file /another/file (no extension)
$t->get_ok('/another/file')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_type_is('text/plain')->content_like(qr/Hello Mojolicious!/);

# Static directory /another
$t->get_ok('/hidden')->status_is(404)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)');

# Check Last-Modified header for static files
my $path = catdir($FindBin::Bin, 'public_dev', 'hello.txt');
my $size = (stat $path)[7];
Expand Down
1 change: 1 addition & 0 deletions t/mojolicious/public_dev/another/file
@@ -0,0 +1 @@
Hello Mojolicious!

0 comments on commit 50c9613

Please sign in to comment.