Adding return JSON on specific requeest feature to template.

This commit is contained in:
Martin Kamerbeek 2009-08-10 12:42:33 +00:00
parent 5675c1a3a3
commit 46829ac970
4 changed files with 38 additions and 2 deletions

View file

@ -16,8 +16,9 @@ use WebGUI::Test;
use WebGUI::Session;
use WebGUI::Asset::Template;
use Exception::Class;
use Test::More tests => 37; # increment this value for each test you create
use Test::More tests => 39; # increment this value for each test you create
use Test::Deep;
use JSON qw{ from_json };
my $session = WebGUI::Test->session;
@ -47,6 +48,18 @@ ok($output =~ m/\bBBBBB\b/, "process() - variables");
ok($output =~ m/true/, "process() - conditionals");
ok($output =~ m/\b(?:XY){5}\b/, "process() - loops");
# See if template listens the Accept header
$session->request->headers_in->{Accept} = 'application/json';
my $json = $template->process(\%var);
my $andNowItsAPerlHashRef = eval { from_json( $json ) };
ok( !$@, 'Accept = json, JSON is returned' );
cmp_deeply( \%var, $andNowItsAPerlHashRef, 'Accept = json, The correct JSON is returned' );
# Done, so remove the json Accept header.
delete $session->request->headers_in->{Accept};
my $newList = WebGUI::Asset::Template->getList($session, 'WebGUI Test Template');
ok(exists $newList->{$template->getId}, 'Uncommitted template exists returned from getList');

View file

@ -213,7 +213,7 @@ sub new {
my $this = shift;
my $class = ref($this) || $this;
my $headers = WebGUI::PseudoRequest::Headers->new();
my $self = {headers_out => $headers};
my $self = { headers_out => $headers, headers_in => {} };
bless $self, $class;
return $self;
}
@ -292,6 +292,19 @@ sub content_type {
#----------------------------------------------------------------------------
=head2 headers_in ( )
Mimics the behavior of Apache2::Request->headers_in.
=cut
sub headers_in {
my $self = shift;
return $self->{headers_in};
}
#----------------------------------------------------------------------------
=head2 headers_out ( )
Returns the PseudoRequst::Headers object stored in $self for access to the headers.