From: John Napiorkowski Date: Tue, 23 Jul 2013 21:18:30 +0000 (-0400) Subject: first pass at middleware tests X-Git-Tag: 5.90050~1^2~59 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=c17c004aa78ef1280963f76abff75224343d71a8;hp=3df65a8d8fa4a41c4d0ea0560875676a4cebfc6b first pass at middleware tests --- diff --git a/t/lib/TestMiddleware.pm b/t/lib/TestMiddleware.pm new file mode 100644 index 0000000..244b957 --- /dev/null +++ b/t/lib/TestMiddleware.pm @@ -0,0 +1,35 @@ +package TestMiddleware; + +use Plack::Middleware::Static; +use Plack::App::File; +use Catalyst; + +extends 'Catalyst'; + +my $static = Plack::Middleware::Static->new( + path => qr{^/static/}, root => TestApp->path_to('share')); + +__PACKAGE__->config( + 'Controller::Root', { namespace => '' }, + 'psgi_middleware', [ + $static, + 'Static', { path => qr{^/static2/}, root => TestApp->path_to('share') }, + '+TestApp::Custom', { path => qr{^/static3/}, root => TestApp->path_to('share') }, + sub { + my $app = shift; + return sub { + my $env = shift; + if($env->{PATH_INFO} =~m/forced/) { + Plack::App::File->new(file=>TestApp->path_to(qw/share static forced.txt/)) + ->call($env); + } else { + return $app->($env); + } + }, + }, + + ], +); + +__PACKAGE__->setup; + diff --git a/t/lib/TestMiddleware/Controller/Root.pm b/t/lib/TestMiddleware/Controller/Root.pm new file mode 100644 index 0000000..7c116a5 --- /dev/null +++ b/t/lib/TestMiddleware/Controller/Root.pm @@ -0,0 +1,13 @@ +package TestMiddleware::Controller::Root; + +use Moose; +use MooseX::MethodAttributes; + +extends 'Catalyst::Controller'; + +sub default : Path { } +sub welcome : Path(welcome) { + pop->res->body('Welcome to Catalyst'); +} + +__PACKAGE__->meta->make_immutable; diff --git a/t/lib/TestMiddleware/Custom.pm b/t/lib/TestMiddleware/Custom.pm new file mode 100644 index 0000000..580a66e --- /dev/null +++ b/t/lib/TestMiddleware/Custom.pm @@ -0,0 +1,8 @@ +package TestMiddleware::Custom; + +use strict; +use warnings; + +use parent qw/Plack::Middleware::Static/; + +1; diff --git a/t/lib/TestMiddleware/share/static/forced.txt b/t/lib/TestMiddleware/share/static/forced.txt new file mode 100644 index 0000000..47b6118 --- /dev/null +++ b/t/lib/TestMiddleware/share/static/forced.txt @@ -0,0 +1 @@ +forced message diff --git a/t/lib/TestMiddleware/share/static/message.txt b/t/lib/TestMiddleware/share/static/message.txt new file mode 100644 index 0000000..9bdf9d1 --- /dev/null +++ b/t/lib/TestMiddleware/share/static/message.txt @@ -0,0 +1 @@ +static message diff --git a/t/lib/TestMiddleware/share/static2/message2.txt b/t/lib/TestMiddleware/share/static2/message2.txt new file mode 100644 index 0000000..9bdf9d1 --- /dev/null +++ b/t/lib/TestMiddleware/share/static2/message2.txt @@ -0,0 +1 @@ +static message diff --git a/t/lib/TestMiddleware/share/static3/message3.txt b/t/lib/TestMiddleware/share/static3/message3.txt new file mode 100644 index 0000000..9bdf9d1 --- /dev/null +++ b/t/lib/TestMiddleware/share/static3/message3.txt @@ -0,0 +1 @@ +static message diff --git a/t/plack-middleware.t b/t/plack-middleware.t new file mode 100644 index 0000000..0693d88 --- /dev/null +++ b/t/plack-middleware.t @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +use FindBin; +use Test::Most; +use HTTP::Request::Common; + +use lib "$FindBin::Bin/lib"; +use Catalyst::Test 'TestApp'; + +ok my($res, $c) = ctx_request('/'); + +{ + ok my $response = request GET $c->uri_for_action('/welcome'), + 'got welcome from a catalyst controller'; + + is $response->content, 'Welcome to Catalyst', + 'expected content body'; +} + +{ + ok my $response = request GET $c->uri_for('/static/message.txt'), + 'got welcome from a catalyst controller'; + + like $response->content, qr'static message', + 'expected content body'; +} + +{ + ok my $response = request GET $c->uri_for('/static2/message2.txt'), + 'got welcome from a catalyst controller'; + + like $response->content, qr'static message', + 'expected content body'; +} + +{ + ok my $response = request GET $c->uri_for('/static3/message3.txt'), + 'got welcome from a catalyst controller'; + + like $response->content, qr'static message', + 'expected content body'; +} + +{ + ok my $response = request GET $c->uri_for('/forced'), + 'got welcome from a catalyst controller'; + + like $response->content, qr'forced message', + 'expected content body'; +} +done_testing;