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',
20 'text/x-yaml' => 'YAML',
21 'application/json' => 'JSON',
22 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
23 'text/broken' => 'Broken',
30 sub test :Local :ActionClass('Serialize') {
31 my ( $self, $c ) = @_;
32 $c->stash->{'rest'} = {
37 sub test_second :Local :ActionClass('Serialize') {
38 my ( $self, $c ) = @_;
39 $c->stash->{'rest'} = {
48 use Test::More tests => 10;
51 use Data::Dump qw(dump);
53 use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken");
56 # Should use Data::Dumper, via YAML
57 my $t = Test::Rest->new('content_type' => 'text/x-yaml');
59 use_ok 'Catalyst::Test', 'Test::Catalyst::Action::Serialize';
67 my $req = $t->get(url => '/test');
68 $req->remove_header('Content-Type');
69 $req->header('Accept', 'text/x-yaml');
70 my $res = request($req);
72 skip "can't test text/x-yaml without YAML support",
74 not $res->is_success and
75 $res->content =~ m#Content-Type text/x-yaml is not supported#
77 ok( $res->is_success, 'GET the serialized request succeeded' );
78 is( $res->content, $data, "Request returned proper data");
79 is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
85 eval 'require JSON 2.12;';
86 skip "can't test application/json without JSON support", 3 if $@;
88 my $at = Test::Rest->new('content_type' => 'text/doesnt-exist');
89 my $req = $at->get(url => '/test');
90 $req->header('Accept', 'application/json');
91 my $res = request($req);
92 ok( $res->is_success, 'GET the serialized request succeeded' );
93 my $ret = $json->decode($res->content);
94 is( $ret->{lou}, 'is my cat', "Request returned proper data");
95 is( $res->header('Content-type'), 'application/json', 'Accept header used if content-type mapping not found')
98 # Make sure we don't get a bogus content-type when using default
99 # serializer (rt.cpan.org ticket 27949)
101 my $req = $t->get(url => '/test');
102 $req->remove_header('Content-Type');
103 $req->header('Accept', '*/*');
104 my $res = request($req);
105 ok( $res->is_success, 'GET the serialized request succeeded' );
106 is( $res->content, $data, "Request returned proper data");
107 is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')