Skip to content

Commit

Permalink
a few more is_fresh tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 6, 2014
1 parent 19a8da6 commit b7c67a4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -282,7 +282,7 @@ Alias for C<Mojolicious::Controller/"render_to_string">.
my $bool = $c->is_fresh(last_modified => $epoch);
Check freshness of response by comparing the C<If-None-Match> and
C<If-Modified-Since> request headers with the C<ETag> and C<Last-Modified>
C<If-Modified-Since> request headers to the C<ETag> and C<Last-Modified>
response headers with L<Mojolicious::Static/"is_fresh">.
# Add ETag header and check freshness before rendering
Expand Down
12 changes: 6 additions & 6 deletions lib/Mojolicious/Static.pm
Expand Up @@ -84,11 +84,11 @@ sub serve_asset {
my ($self, $c, $asset) = @_;

# Last-Modified and ETag
my $mtime = $asset->is_file ? (stat $asset->path)[9] : $MTIME;
my $res = $c->res;
$res->code(200)->headers->accept_ranges('bytes');
return $res->code(304)
if $self->is_fresh($c, {etag => md5_sum($mtime), last_modified => $mtime});
my $mtime = $asset->is_file ? (stat $asset->path)[9] : $MTIME;
my $options = {etag => md5_sum($mtime), last_modified => $mtime};
return $res->code(304) if $self->is_fresh($c, $options);

# Range
my $size = $asset->size;
Expand Down Expand Up @@ -213,7 +213,7 @@ protect from traversing to parent directories.
my $bool = $static->is_fresh(Mojolicious::Controller->new, {etag => 'abc'});
Check freshness of response by comparing the C<If-None-Match> and
C<If-Modified-Since> request headers with the C<ETag> and C<Last-Modified>
C<If-Modified-Since> request headers to the C<ETag> and C<Last-Modified>
response headers.
These options are currently available:
Expand All @@ -224,13 +224,13 @@ These options are currently available:
etag => 'abc'
Add C<ETag> header.
Add C<ETag> header before comparing.
=item last_modified
last_modified => $epoch
Add C<Last-Modified> header.
Add C<Last-Modified> header before comparing.
=back
Expand Down
4 changes: 4 additions & 0 deletions t/mojolicious/static_lite_app.t
Expand Up @@ -39,6 +39,10 @@ ok $c->is_fresh(etag => 'abc', last_modified => $date->epoch),
'content is fresh';
is $c->res->headers->etag, '"abc"', 'right "ETag" value';
is $c->res->headers->last_modified, "$date", 'right "Last-Modified" value';
$c = $t->app->build_controller;
ok !$c->is_fresh(last_modified => $date->epoch), 'content is stale';
is $c->res->headers->etag, undef, 'no "ETag" value';
is $c->res->headers->last_modified, "$date", 'right "Last-Modified" value';

# Static file
$t->get_ok('/hello.txt')->status_is(200)
Expand Down

0 comments on commit b7c67a4

Please sign in to comment.