From 2d31f7234a368b17a5090e682593dd57b1e130e3 Mon Sep 17 00:00:00 2001 From: Scott Walters Date: Tue, 10 May 2011 14:26:16 -0400 Subject: [PATCH] PSGI entry points (#11632) -- add a plackMiddleware section to the config file; modify share/site.psgi to load from it. This is minimal in implementation right now in that there's no support for passing args, which would generally be objects mixed with strings, to the middlewares being added, as would be necessary if the .psgi files were to be largely migrated to the config files. I agree that it's important that upgrade scripts be able to add middleware (probably custom ones that know how to get the session from %$env) but I'm not convinced of the utility of moving much logic out of share/site.psgi. --- share/site.psgi | 6 +++++ t/PSGI/ConfigMiddleware.t | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 t/PSGI/ConfigMiddleware.t diff --git a/share/site.psgi b/share/site.psgi index da9b5b8ff..9c553b73a 100644 --- a/share/site.psgi +++ b/share/site.psgi @@ -56,6 +56,12 @@ builder { Plack::App::File->new(root => $config->get('uploadsPath')); }; + # enable config defined Middleware + + for my $mw ( @{ $config->get('plackMiddleware') || [] } ) { + enable $mw; + } + # Return the app mount '/' => $wg->to_app; }; diff --git a/t/PSGI/ConfigMiddleware.t b/t/PSGI/ConfigMiddleware.t new file mode 100644 index 000000000..0555e04c0 --- /dev/null +++ b/t/PSGI/ConfigMiddleware.t @@ -0,0 +1,46 @@ +use strict; +use warnings; +use Test::More tests => 3; + +use Plack::Test; +use Plack::Util; +use HTTP::Request::Common; +use WebGUI::Paths; +use WebGUI::Test; + +SKIP: { + skip 'set WEBGUI_LIVE to enable these tests', 3 unless $ENV{WEBGUI_LIVE}; + + my $session = WebGUI::Test->session; + $session->config->addToArray( 'plackMiddleware', '+WebGUI::Middleware::SHOUTING' ); + + local $ENV{WEBGUI_CONFIG} = WebGUI::Test->file; # tell the share/site.psgi which site to load + + my $app = Plack::Util::load_psgi( WebGUI::Paths->defaultPSGI ); + + ok( $app, "created a PSGI app from app.psgi" ); + + test_psgi $app, sub { + my $cb = shift; + my $res = $cb->( GET "/home" ); + is $res->code, 200, 'able to fetch pages with WebGUI::Middleware::SHOUTING installed'; + like $res->content, qr/EASY TO USE WEB APPLICATION FRAMEWORK/, 'contains the text "EASY TO USE WEB APPLICATION FRAMEWORK"'; + }; + + $session->config->deleteFromArray( 'plackMiddleware', '+WebGUI::Middleware::SHOUTING' ); +} + +package WebGUI::Middleware::SHOUTING; +BEGIN { $INC{'WebGUI/Middleware/SHOUTING.pm'} = __FILE__; }; + +use parent 'Plack::Middleware'; + +sub call { + my($self, $env) = @_; + my $res = $self->app->($env); + for ( ref $res->[2] ? @{ $res->[2] } : ( $res->[2] ) ) { + s{>(.*?)<}{'>' . uc($1) . '<'}gse; + } + return $res; +} +