allow templates to be returned from content handlers

This commit is contained in:
Doug Bell 2011-04-08 15:44:28 -05:00
parent adb67b2137
commit b8dec21449
2 changed files with 104 additions and 2 deletions

View file

@ -165,12 +165,18 @@ sub handle {
# We decide what to do next depending on what the contentHandler returned
# A WebGUI::Asset::Template object means we should process it
if ( defined $output && blessed $output && $output->isa( 'WebGUI::Asset::Template' ) ) {
$session->http->sendHeader;
$session->output->print( $output->process );
return;
}
# "chunked" or "empty" means it took care of its own output needs
if (defined $output && ( $output eq "chunked" || $output eq "empty" )) {
elsif (defined $output && ( $output eq "chunked" || $output eq "empty" )) {
#warn "chunked and empty no longer stream, use session->response->stream() instead";
return;
}
# non-empty output should be used as the response body
# other non-empty output should be used as the response body
elsif (defined $output && $output ne "") {
# Auto-set the headers
$session->http->sendHeader;

96
t/WebGUI.t Normal file
View file

@ -0,0 +1,96 @@
# vim:syntax=perl
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#------------------------------------------------------------------
# Test the WebGUI PSGI handler
#
#
use FindBin;
use strict;
use lib "$FindBin::Bin/lib";
use Test::More;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI;
use WebGUI::Session;
#----------------------------------------------------------------------------
# Init
my $session = WebGUI::Test->session;
# A template object to play with
my $template = WebGUI::Test->asset(
className => 'WebGUI::Asset::Template',
template => '<html><body>Template World</body></html>',
);
# Create a fake content handler class to install in the config
BEGIN {
$INC{'WebGUI/Content/TestHandler.pm'} = __FILE__;
$INC{'WebGUI/Content/TestChunked.pm'} = __FILE__;
$INC{'WebGUI/Content/TestTemplate.pm'} = __FILE__;
}
package WebGUI::Content::TestHandler;
sub handler { '<html><body>Hello World</body></html>' }
package WebGUI::Content::TestChunked;
sub handler { "chunked" }
package WebGUI::Content::TestTemplate;
sub handler { $template }
package main;
#----------------------------------------------------------------------------
# Test the various possibilities of content handlers
my ( $fh );
$session->config->set( 'contentHandlers', [ 'WebGUI::Content::TestHandler' ] );
$fh = capture_output();
WebGUI->handle( $session );
is(
get_output( $fh ),
WebGUI::Content::TestHandler->handler,
'handler that returns HTML is output directly',
);
$session->config->set( 'contentHandlers', [ 'WebGUI::Content::TestChunked' ] );
$fh = capture_output();
WebGUI->handle( $session );
isnt(
get_output( $fh ),
'chunked',
'chunked is not returned',
);
$session->config->set( 'contentHandlers', [ 'WebGUI::Content::TestTemplate' ] );
$fh = capture_output();
WebGUI->handle( $session );
is(
get_output( $fh ),
$template->process,
'handler that returns template is processed',
);
sub capture_output {
my $output_fh = undef;
open $fh, '+>', \$output_fh;
$session->output->setHandle( $fh );
return $fh;
}
sub get_output {
my ( $fh ) = @_;
seek $fh, 0, 0;
return join '', <$fh>;
}
done_testing;
#vim:ft=perl