From: Alexander Hartmaier Date: Fri, 4 May 2012 17:01:26 +0000 (+0000) Subject: Change configuration key to 'Plugin::Static::Simple' by default. X-Git-Tag: v0.30~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ee1e7faf8d6bb43a85f6d6f74b8f0bd7b7e8712a;p=catagits%2FCatalyst-Plugin-Static-Simple.git Change configuration key to 'Plugin::Static::Simple' by default. The old 'static' key is still supported, but issues a warning. --- diff --git a/Changes b/Changes index 71d2367..641903e 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Perl extension Catalyst::Plugin::Static::Simple + - Change configuration key to 'Plugin::Static::Simple' by default. + The old 'static' key is still supported, but issues a warning. + 0.30 2011-02-xx hh:mm:00 - Add Cache-Control:public header - Optionally provide Expires header diff --git a/lib/Catalyst/Plugin/Static/Simple.pm b/lib/Catalyst/Plugin/Static/Simple.pm index 91cef4a..1611f93 100644 --- a/lib/Catalyst/Plugin/Static/Simple.pm +++ b/lib/Catalyst/Plugin/Static/Simple.pm @@ -6,6 +6,7 @@ use File::Spec (); use IO::File (); use MIME::Types (); use MooseX::Types::Moose qw/ArrayRef Str/; +use Catalyst::Utils; use namespace::autoclean; our $VERSION = '0.30'; @@ -16,7 +17,7 @@ has _static_debug_message => ( is => 'rw', isa => ArrayRef[Str] ); before prepare_action => sub { my $c = shift; my $path = $c->req->path; - my $config = $c->config->{static}; + my $config = $c->config->{'Plugin::Static::Simple'}; $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; @@ -67,7 +68,7 @@ around dispatch => sub { return if ( $c->res->status != 200 ); if ( $c->_static_file ) { - if ( $c->config->{static}{no_logs} && $c->log->can('abort') ) { + if ( $c->config->{'Plugin::Static::Simple'}->{no_logs} && $c->log->can('abort') ) { $c->log->abort( 1 ); } return $c->_serve_static; @@ -81,7 +82,7 @@ before finalize => sub { my $c = shift; # display all log messages - if ( $c->config->{static}{debug} && scalar @{$c->_debug_msg} ) { + if ( $c->config->{'Plugin::Static::Simple'}->{debug} && scalar @{$c->_debug_msg} ) { $c->log->debug( 'Static::Simple: ' . join q{ }, @{$c->_debug_msg} ); } }; @@ -89,7 +90,15 @@ before finalize => sub { before setup_finalize => sub { my $c = shift; - my $config = $c->config->{static} ||= {}; + $c->log->warn("Deprecated 'static' config key used, please use the key 'Plugin::Static::Simple' instead") + if exists $c->config->{static}; + my $config + = $c->config->{'Plugin::Static::Simple'} + = $c->config->{'static'} + = Catalyst::Utils::merge_hashes( + $c->config->{'Plugin::Static::Simple'} || {}, + $c->config->{static} || {} + ); $config->{dirs} ||= []; $config->{include_path} ||= [ $c->config->{root} ]; @@ -117,7 +126,7 @@ sub _locate_static_file { File::Spec->no_upwards( File::Spec->splitdir( $path ) ) ); - my $config = $c->config->{static}; + my $config = $c->config->{'Plugin::Static::Simple'}; my @ipaths = @{ $config->{include_path} }; my $dpaths; my $count = 64; # maximum number of directories to search @@ -172,7 +181,7 @@ sub _locate_static_file { sub _serve_static { my $c = shift; - my $config = $c->config->{static} ||= {}; + my $config = $c->config->{'Plugin::Static::Simple'}; my $full_path = shift || $c->_static_file; my $type = $c->_ext_to_type( $full_path ); @@ -204,7 +213,7 @@ sub _serve_static { sub serve_static_file { my ( $c, $full_path ) = @_; - my $config = $c->config->{static} ||= {}; + my $config = $c->config->{'Plugin::Static::Simple'}; if ( -e $full_path ) { $c->_debug_msg( "Serving static file: $full_path" ) @@ -225,7 +234,7 @@ sub serve_static_file { sub _ext_to_type { my ( $c, $full_path ) = @_; - my $config = $c->config->{static}; + my $config = $c->config->{'Plugin::Static::Simple'}; if ( $full_path =~ /.*\.(\S{1,})$/xms ) { my $ext = $1; diff --git a/t/04static.t b/t/04static.t index bae2922..b301341 100644 --- a/t/04static.t +++ b/t/04static.t @@ -1,37 +1,37 @@ -#!perl - -use strict; -use warnings; - -use FindBin; -use lib "$FindBin::Bin/lib"; - -# Module::Build craps out on files with spaces so it's not included in the dist -my $has_space_file = -e "$FindBin::Bin/lib/TestApp/root/files/space file.txt"; - -use Test::More; +#!perl + +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/lib"; + +# Module::Build craps out on files with spaces so it's not included in the dist +my $has_space_file = -e "$FindBin::Bin/lib/TestApp/root/files/space file.txt"; + +use Test::More; plan tests => ($has_space_file) ? 12 : 9; -use Catalyst::Test 'TestApp'; - -# test getting a css file -ok( my $res = request('http://localhost/files/static.css'), 'request ok' ); -is( $res->content_type, 'text/css', 'content-type text/css ok' ); -like( $res->content, qr/background/, 'content of css ok' ); - -# test a file with spaces -if ( $has_space_file ) { - ok( $res = request('http://localhost/files/space file.txt'), 'request ok' ); - is( $res->content_type, 'text/plain', 'content-type text/plain ok' ); - like( $res->content, qr/background/, 'content of space file ok' ); -} - -# test a non-existent file -ok( $res = request('http://localhost/files/404.txt'), 'request ok' ); -is( $res->content, 'default', 'default handler for non-existent content ok' ); - -# test unknown extension -ok( $res = request('http://localhost/files/err.omg'), 'request ok' ); -is( $res->content_type, 'text/plain', 'unknown extension as text/plain ok' ); +use Catalyst::Test 'TestApp'; + +# test getting a css file +ok( my $res = request('http://localhost/files/static.css'), 'request ok' ); +is( $res->content_type, 'text/css', 'content-type text/css ok' ); +like( $res->content, qr/background/, 'content of css ok' ); + +# test a file with spaces +if ( $has_space_file ) { + ok( $res = request('http://localhost/files/space file.txt'), 'request ok' ); + is( $res->content_type, 'text/plain', 'content-type text/plain ok' ); + like( $res->content, qr/background/, 'content of space file ok' ); +} + +# test a non-existent file +ok( $res = request('http://localhost/files/404.txt'), 'request ok' ); +is( $res->content, 'default', 'default handler for non-existent content ok' ); + +# test unknown extension +ok( $res = request('http://localhost/files/err.omg'), 'request ok' ); +is( $res->content_type, 'text/plain', 'unknown extension as text/plain ok' ); ok( $res = request('http://localhost/files/empty.txt'), 'request ok' ); is( $res->content, '', 'empty files result in an empty response' ); diff --git a/t/05dirs.t b/t/05dirs.t index acb299e..f342556 100644 --- a/t/05dirs.t +++ b/t/05dirs.t @@ -1,42 +1,42 @@ -#!perl - -use strict; -use warnings; - -use FindBin; -use lib "$FindBin::Bin/lib"; - -use Test::More tests => 13; -use Catalyst::Test 'TestApp'; - -# test defined static dirs -TestApp->config->{static}->{dirs} = [ - 'always-static', - qr/^images/, - 'qr/^css/', -]; - -# a file with no extension will return text/plain -ok( my $res = request('http://localhost/always-static/test'), 'request ok' ); -is( $res->content_type, 'text/plain', 'text/plain ok' ); - -# a file with an extension in ignore_extensions still gets served -ok( $res = request('http://localhost/always-static/test.html'), 'request ok' ); -is( $res->code, 200, 'html file in dirs get served' ); - -# a missing file in a defined static dir will return 404 and text/html -ok( $res = request('http://localhost/always-static/404.txt'), 'request ok' ); -is( $res->code, 404, '404 ok' ); -is( $res->content_type, 'text/html', '404 is text/html' ); - -# qr regex test -ok( $res = request('http://localhost/images/catalyst.png'), 'request ok' ); -is( $res->content_type, 'image/png', 'qr regex path ok' ); - -# eval regex test -ok( $res = request('http://localhost/css/static.css'), 'request ok' ); -like( $res->content, qr/background/, 'eval regex path ok' ); - -# A static dir with no trailing slash is handled by Cat -ok( $res = request('http://localhost/always-static'), 'request ok' ); -is( $res->content, 'default', 'content ok' ); \ No newline at end of file +#!perl + +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/lib"; + +use Test::More tests => 13; +use Catalyst::Test 'TestApp'; + +# test defined static dirs +TestApp->config->{'Plugin::Static::Simple'}->{dirs} = [ + 'always-static', + qr/^images/, + 'qr/^css/', +]; + +# a file with no extension will return text/plain +ok( my $res = request('http://localhost/always-static/test'), 'request ok' ); +is( $res->content_type, 'text/plain', 'text/plain ok' ); + +# a file with an extension in ignore_extensions still gets served +ok( $res = request('http://localhost/always-static/test.html'), 'request ok' ); +is( $res->code, 200, 'html file in dirs get served' ); + +# a missing file in a defined static dir will return 404 and text/html +ok( $res = request('http://localhost/always-static/404.txt'), 'request ok' ); +is( $res->code, 404, '404 ok' ); +is( $res->content_type, 'text/html', '404 is text/html' ); + +# qr regex test +ok( $res = request('http://localhost/images/catalyst.png'), 'request ok' ); +is( $res->content_type, 'image/png', 'qr regex path ok' ); + +# eval regex test +ok( $res = request('http://localhost/css/static.css'), 'request ok' ); +like( $res->content, qr/background/, 'eval regex path ok' ); + +# A static dir with no trailing slash is handled by Cat +ok( $res = request('http://localhost/always-static'), 'request ok' ); +is( $res->content, 'default', 'content ok' ); diff --git a/t/06include_path.t b/t/06include_path.t index 144f39a..b0d2d26 100644 --- a/t/06include_path.t +++ b/t/06include_path.t @@ -1,29 +1,29 @@ -#!perl - -use strict; -use warnings; - -use FindBin; -use lib "$FindBin::Bin/lib"; - -use Test::More tests => 6; -use Catalyst::Test 'TestApp'; - -# test altenate root dirs -TestApp->config->{static}->{include_path} = [ - TestApp->config->{root} . '/overlay', - \&TestApp::incpath_generator, - TestApp->config->{root}, -]; - -# test overlay dir -ok( my $res = request('http://localhost/overlay.jpg'), 'request ok' ); -is( $res->content_type, 'image/jpeg', 'overlay path ok' ); - -# test incpath_generator -ok( $res = request('http://localhost/incpath.css'), 'request ok' ); -is( $res->content_type, 'text/css', 'incpath coderef ok' ); - -# test passthrough to root -ok( $res = request('http://localhost/images/bad.gif'), 'request ok' ); -is( $res->content_type, 'image/gif', 'root path ok' ); +#!perl + +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/lib"; + +use Test::More tests => 6; +use Catalyst::Test 'TestApp'; + +# test altenate root dirs +TestApp->config->{'Plugin::Static::Simple'}->{include_path} = [ + TestApp->config->{root} . '/overlay', + \&TestApp::incpath_generator, + TestApp->config->{root}, +]; + +# test overlay dir +ok( my $res = request('http://localhost/overlay.jpg'), 'request ok' ); +is( $res->content_type, 'image/jpeg', 'overlay path ok' ); + +# test incpath_generator +ok( $res = request('http://localhost/incpath.css'), 'request ok' ); +is( $res->content_type, 'text/css', 'incpath coderef ok' ); + +# test passthrough to root +ok( $res = request('http://localhost/images/bad.gif'), 'request ok' ); +is( $res->content_type, 'image/gif', 'root path ok' ); diff --git a/t/07mime_types.t b/t/07mime_types.t index 4652494..e9276b0 100644 --- a/t/07mime_types.t +++ b/t/07mime_types.t @@ -10,7 +10,7 @@ use Test::More tests => 4; use Catalyst::Test 'TestApp'; # test custom MIME types -TestApp->config->{static}->{mime_types} = { +TestApp->config->{'Plugin::Static::Simple'}->{mime_types} = { omg => 'holy/crap', gif => 'patents/are-evil', }; diff --git a/t/08subreq.t b/t/08subreq.t index 911bf5c..e5be28d 100644 --- a/t/08subreq.t +++ b/t/08subreq.t @@ -1,24 +1,24 @@ -#!perl - -use strict; -use warnings; - -use FindBin; -use lib "$FindBin::Bin/lib"; - -use Test::More tests => 2; -use Catalyst::Test 'TestApp'; - -SKIP: -{ - unless ( TestApp->isa('Catalyst::Plugin::SubRequest') ) { - skip "Install Catalyst::Plugin::SubRequest >= 0.15 for these tests", 2; - } - unless ( $Catalyst::Plugin::SubRequest::VERSION >= 0.15 ) { - skip "Need Catalyst::Plugin::SubRequest >= 0.15 for these tests", 2; - } - - ok( my $res = request('http://localhost/subtest'), 'Request' ); - is( $res->content, 'subtest2 ok', 'SubRequest ok' ); -} - +#!perl + +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/lib"; + +use Test::More tests => 2; +use Catalyst::Test 'TestApp'; + +SKIP: +{ + unless ( TestApp->isa('Catalyst::Plugin::SubRequest') ) { + skip "Install Catalyst::Plugin::SubRequest >= 0.15 for these tests", 2; + } + unless ( $Catalyst::Plugin::SubRequest::VERSION >= 0.15 ) { + skip "Need Catalyst::Plugin::SubRequest >= 0.15 for these tests", 2; + } + + ok( my $res = request('http://localhost/subtest'), 'Request' ); + is( $res->content, 'subtest2 ok', 'SubRequest ok' ); +} + diff --git a/t/10ignore_dirs.t b/t/10ignore_dirs.t index ba80c03..01cdc14 100644 --- a/t/10ignore_dirs.t +++ b/t/10ignore_dirs.t @@ -10,10 +10,10 @@ use Test::More tests => 6; use Catalyst::Test 'TestApp'; # test ignoring directories -TestApp->config->{static}->{ignore_dirs} = [ qw/ignored o-ignored files/ ]; +TestApp->config->{'Plugin::Static::Simple'}->{ignore_dirs} = [ qw/ignored o-ignored files/ ]; # test altenate root dirs -TestApp->config->{static}->{include_path} = [ +TestApp->config->{'Plugin::Static::Simple'}->{include_path} = [ TestApp->config->{root} . '/overlay', TestApp->config->{root}, ]; diff --git a/t/12check_error_scope.t b/t/12check_error_scope.t index 14a7655..fc67c29 100644 --- a/t/12check_error_scope.t +++ b/t/12check_error_scope.t @@ -23,7 +23,7 @@ BEGIN { } use Catalyst::Test 'TestApp'; -TestApp->config->{static}->{dirs} = [qr{stuff/}]; +TestApp->config->{'Plugin::Static::Simple'}->{dirs} = [qr{stuff/}]; ok( my $res = request("http://localhost/"), 'request ok' ); ok( $res->code == 200, q{Previous error doesn't crash static::simple} ); diff --git a/t/20debug.t b/t/20debug.t index e934dee..400dfcd 100644 --- a/t/20debug.t +++ b/t/20debug.t @@ -1,38 +1,38 @@ -#!perl - -use strict; -use warnings; - -use FindBin; -use lib "$FindBin::Bin/lib"; - -use Test::More tests => 5; -use Catalyst::Test 'TestApp'; - -# test defined static dirs -TestApp->config->{static}->{dirs} = [ - 'always-static', -]; - -TestApp->config->{static}->{debug} = 1; - -use Catalyst::Log; - -local *Catalyst::Log::_send_to_log; -local our @MESSAGES; -{ - no warnings 'redefine'; - *Catalyst::Log::_send_to_log = sub { - my $self = shift; - push @MESSAGES, @_; - }; -} - - -# a missing file in a defined static dir will return 404 and text/html -ok( my $res = request('http://localhost/always-static/404.txt'), 'request ok' ); -is( $res->code, 404, '404 ok' ); -is( $res->content_type, 'text/html', '404 is text/html' ); -ok(defined $MESSAGES[0], 'debug message set'); -like( $MESSAGES[0], qr/404/, 'debug message contains 404'); - +#!perl + +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/lib"; + +use Test::More tests => 5; +use Catalyst::Test 'TestApp'; + +# test defined static dirs +TestApp->config->{'Plugin::Static::Simple'}->{dirs} = [ + 'always-static', +]; + +TestApp->config->{'Plugin::Static::Simple'}->{debug} = 1; + +use Catalyst::Log; + +local *Catalyst::Log::_send_to_log; +local our @MESSAGES; +{ + no warnings 'redefine'; + *Catalyst::Log::_send_to_log = sub { + my $self = shift; + push @MESSAGES, @_; + }; +} + + +# a missing file in a defined static dir will return 404 and text/html +ok( my $res = request('http://localhost/always-static/404.txt'), 'request ok' ); +is( $res->code, 404, '404 ok' ); +is( $res->content_type, 'text/html', '404 is text/html' ); +ok(defined $MESSAGES[0], 'debug message set'); +like( $MESSAGES[0], qr/404/, 'debug message contains 404'); +