EntityModel-Web-PSGI
view release on metacpan or search on metacpan
lib/EntityModel/Web/PSGI.pm view on Meta::CPAN
# Prepare for page rendering
$ctx->resolve_data;
# Get a response object
my $resp = EntityModel::Web::Response->new(
context => $ctx,
page => $ctx->page,
request => $req,
);
# then ignore it and generate the body and a hardcoded 200 return
# FIXME use proper status code here and support streaming/async!
my $body = $ctx->process;
return $self->psgi_result(
$env,
200,
[ 'Content-Type' => 'text/html' ],
$body
);
}
=head2 psgi_result
Returns an appropriate PSGI response, either
an arrayref or a coderef depending on server
support for async/streaming.
=cut
sub psgi_result {
my $self = shift;
my ($env, $rslt, $hdr, $body) = @_;
logInfo("Streaming: %s", $env->{'psgi.streaming'} ? 'yes' : 'no');
return [ $rslt, $hdr, [ $body ] ] unless $env->{'psgi.streaming'};
return sub {
my $responder = shift;
my $writer = $responder->([
$rslt,
$hdr,
]);
$writer->write($body);
$writer->close;
};
use strict;
use warnings;
use Test::More tests => 4;
use EntityModel::Web::PSGI;
my $psgi = new_ok('EntityModel::Web::PSGI');
can_ok($psgi, qw(run_psgi psgi_result web template));
$psgi->web(EntityModel::Web->new);
is(ref $psgi->run_psgi({}), 'ARRAY', 'returns arrayref with no streaming');
is(ref $psgi->run_psgi({'psgi.streaming' => '1'}), 'CODE', 'returns coderef with streaming');
( run in 0.287 second using v1.01-cache-2.11-cpan-4d50c553e7e )