Made test independent of YAML::Syck bugs
[catagits/Catalyst-Action-REST.git] / t / catalyst-action-serialize-accept.t
1 use strict;
2 use warnings;
3 use Test::More tests => 16;
4 use Data::Serializer;
5 use FindBin;
6
7 use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken");
8 use Test::Rest;
9 use Catalyst::Action::Serialize::YAML;
10
11 # Should use Data::Dumper, via YAML
12 my $t = Test::Rest->new('content_type' => 'text/x-yaml');
13
14 use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST';
15
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'});
20
21 {
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);
26     SKIP: {
27         skip "can't test text/x-yaml without YAML support",
28         3 if (
29                 not $res->is_success and
30                 $res->content =~ m#Content-Type text/x-yaml is not supported#
31              );
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')
35
36     };
37 }
38
39 SKIP: {
40     eval 'use JSON 2.12;';
41     skip "can't test application/json without JSON support", 3 if $@;
42     my $json = JSON->new;
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')
51 };
52
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)
55 {
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')
63 }
64
65 # Make sure that when using content_type_stash_key, an invalid value in the stash gets ignored
66 {
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')
74 }
75
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
79 {
80     my $req = $t->get(url =>
81         '/serialize/test_second?serialize_content_type=text/x-data-dumper'
82     );
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')
89 }
90
91 1;