1 package Test::Catalyst::Action::Serialize;
5 use lib ("$FindBin::Bin/../lib");
10 use Catalyst::Runtime '5.70';
15 name => 'Test::Catalyst::Action::Serialize',
17 'default' => 'text/x-yaml',
18 'stash_key' => 'rest',
19 'content_type_stash_key' => 'serialize_content_type',
21 'text/x-yaml' => 'YAML',
22 'application/json' => 'JSON',
23 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
24 'text/broken' => 'Broken',
31 sub test :Local :ActionClass('Serialize') {
32 my ( $self, $c ) = @_;
33 $c->stash->{'rest'} = {
38 sub test_second :Local :ActionClass('Serialize') {
39 my ( $self, $c ) = @_;
40 $c->stash->{'serialize_content_type'} = $c->req->params->{'serialize_content_type'};
41 $c->stash->{'rest'} = {
51 use Test::More tests => 16;
54 use Data::Dump qw(dump);
56 use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken");
59 # Should use Data::Dumper, via YAML
60 my $t = Test::Rest->new('content_type' => 'text/x-yaml');
62 use_ok 'Catalyst::Test', 'Test::Catalyst::Action::Serialize';
70 my $req = $t->get(url => '/test');
71 $req->remove_header('Content-Type');
72 $req->header('Accept', 'text/x-yaml');
73 my $res = request($req);
75 skip "can't test text/x-yaml without YAML support",
77 not $res->is_success and
78 $res->content =~ m#Content-Type text/x-yaml is not supported#
80 ok( $res->is_success, 'GET the serialized request succeeded' );
81 is( $res->content, $data, "Request returned proper data");
82 is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
88 eval 'require JSON 2.12;';
89 skip "can't test application/json without JSON support", 3 if $@;
91 my $at = Test::Rest->new('content_type' => 'text/doesnt-exist');
92 my $req = $at->get(url => '/test');
93 $req->header('Accept', 'application/json');
94 my $res = request($req);
95 ok( $res->is_success, 'GET the serialized request succeeded' );
96 my $ret = $json->decode($res->content);
97 is( $ret->{lou}, 'is my cat', "Request returned proper data");
98 is( $res->header('Content-type'), 'application/json', 'Accept header used if content-type mapping not found')
101 # Make sure we don't get a bogus content-type when using default
102 # serializer (rt.cpan.org ticket 27949)
104 my $req = $t->get(url => '/test');
105 $req->remove_header('Content-Type');
106 $req->header('Accept', '*/*');
107 my $res = request($req);
108 ok( $res->is_success, 'GET the serialized request succeeded' );
109 is( $res->content, $data, "Request returned proper data");
110 is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
113 # Make that using content_type_stash_key, an invalid value in the stash gets ignored
115 my $req = $t->get(url => '/test_second?serialize_content_type=nonesuch');
116 $req->remove_header('Content-Type');
117 $req->header('Accept', '*/*');
118 my $res = request($req);
119 ok( $res->is_success, 'GET the serialized request succeeded' );
120 is( $res->content, $data, "Request returned proper data");
121 is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
124 # Make that using content_type_stash_key, a valid value in the stash gets priority
126 my $req = $t->get(url => '/test_second?serialize_content_type=text/x-data-dumper');
127 $req->remove_header('Content-Type');
128 $req->header('Accept', '*/*');
129 my $res = request($req);
130 ok( $res->is_success, 'GET the serialized request succeeded' );
131 is( $res->content, "{'lou' => 'is my cat'}", "Request returned proper data");
132 is( $res->header('Content-type'), 'text/x-data-dumper', '... with expected content-type')