3 use Test::More tests => 16;
7 use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken");
9 use Catalyst::Action::Serialize::YAML;
11 # Should use Data::Dumper, via YAML
12 my $t = Test::Rest->new('content_type' => 'text/x-yaml');
14 use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST';
16 # to avoid whatever serialization bugs YAML::Syck has,
17 # e.g. http://rt.cpan.org/Public/Bug/Display.html?id=46983,
18 # we won't hardcode the expected output
19 my $output_YAML = Catalyst::Action::Serialize::YAML->serialize({lou => 'is my cat'});
22 my $req = $t->get(url => '/serialize/test');
23 $req->remove_header('Content-Type');
24 $req->header('Accept', 'text/x-yaml');
25 my $res = request($req);
27 skip "can't test text/x-yaml without YAML support",
29 not $res->is_success and
30 $res->content =~ m#Content-Type text/x-yaml is not supported#
32 ok( $res->is_success, 'GET the serialized request succeeded' );
33 is( $res->content, $output_YAML, "Request returned proper data");
34 is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
40 eval 'use JSON 2.12;';
41 skip "can't test application/json without JSON support", 3 if $@;
43 my $at = Test::Rest->new('content_type' => 'text/doesnt-exist');
44 my $req = $at->get(url => '/serialize/test');
45 $req->header('Accept', 'application/json');
46 my $res = request($req);
47 ok( $res->is_success, 'GET the serialized request succeeded' );
48 my $ret = $json->decode($res->content);
49 is( $ret->{lou}, 'is my cat', "Request returned proper data");
50 is( $res->header('Content-type'), 'application/json', 'Accept header used if content-type mapping not found')
53 # Make sure we don't get a bogus content-type when using the default
54 # serializer (https://rt.cpan.org/Ticket/Display.html?id=27949)
56 my $req = $t->get(url => '/serialize/test');
57 $req->remove_header('Content-Type');
58 $req->header('Accept', '*/*');
59 my $res = request($req);
60 ok( $res->is_success, 'GET the serialized request succeeded' );
61 is( $res->content, $output_YAML, "Request returned proper data");
62 is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
65 # Make sure that when using content_type_stash_key, an invalid value in the stash gets ignored
67 my $req = $t->get(url => '/serialize/test_second?serialize_content_type=nonesuch');
68 $req->remove_header('Content-Type');
69 $req->header('Accept', '*/*');
70 my $res = request($req);
71 ok( $res->is_success, 'GET the serialized request succeeded' );
72 is( $res->content, $output_YAML, "Request returned proper data");
73 is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
76 # Make sure that when using content_type_stash_key, a valid value in the stash gets priority.
77 # This also tests that application-level config is properly passed to
78 # individual controllers; see t/lib/Test/Catalyst/Action/REST.pm
80 my $req = $t->get(url =>
81 '/serialize/test_second?serialize_content_type=text/x-data-dumper'
83 $req->remove_header('Content-Type');
84 $req->header('Accept', '*/*');
85 my $res = request($req);
86 ok( $res->is_success, 'GET the serialized request succeeded' );
87 is( $res->content, "{'lou' => 'is my cat'}", "Request returned proper data");
88 is( $res->header('Content-type'), 'text/x-data-dumper', '... with expected content-type')