diff --git a/lib/WebGUI.pm b/lib/WebGUI.pm index cacb42c37..3580bbc62 100644 --- a/lib/WebGUI.pm +++ b/lib/WebGUI.pm @@ -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; diff --git a/t/WebGUI.t b/t/WebGUI.t new file mode 100644 index 000000000..21196b0f8 --- /dev/null +++ b/t/WebGUI.t @@ -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 => 'Template World', +); + +# 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 { 'Hello World' } +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