Commit | Line | Data |
e601adda |
1 | use strict; |
2 | use warnings; |
a51e7bbd |
3 | use Test::More tests => 16; |
e601adda |
4 | use Data::Serializer; |
5 | use FindBin; |
e601adda |
6 | |
7 | use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken"); |
8 | use Test::Rest; |
f1064486 |
9 | use Catalyst::Action::Serialize::YAML; |
e601adda |
10 | |
21d3f6ae |
11 | # Should use Data::Dumper, via YAML |
e601adda |
12 | my $t = Test::Rest->new('content_type' => 'text/x-yaml'); |
13 | |
74bc96b9 |
14 | use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST'; |
e601adda |
15 | |
f1064486 |
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'}); |
367b3ff4 |
20 | |
21 | { |
21d3f6ae |
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); |
2f7533ed |
26 | SKIP: { |
27 | skip "can't test text/x-yaml without YAML support", |
21d3f6ae |
28 | 3 if ( |
29 | not $res->is_success and |
30 | $res->content =~ m#Content-Type text/x-yaml is not supported# |
2f7533ed |
31 | ); |
21d3f6ae |
32 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
f1064486 |
33 | is( $res->content, $output_YAML, "Request returned proper data"); |
21d3f6ae |
34 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
2f7533ed |
35 | |
36 | }; |
367b3ff4 |
37 | } |
38 | |
2f7533ed |
39 | SKIP: { |
8b010556 |
40 | eval 'use JSON 2.12;'; |
2f7533ed |
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'); |
21d3f6ae |
44 | my $req = $at->get(url => '/serialize/test'); |
45 | $req->header('Accept', 'application/json'); |
46 | my $res = request($req); |
2f7533ed |
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 | }; |
c0aef9cd |
52 | |
f1064486 |
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) |
367b3ff4 |
55 | { |
21d3f6ae |
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' ); |
f1064486 |
61 | is( $res->content, $output_YAML, "Request returned proper data"); |
21d3f6ae |
62 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
367b3ff4 |
63 | } |
e601adda |
64 | |
f1064486 |
65 | # Make sure that when using content_type_stash_key, an invalid value in the stash gets ignored |
a51e7bbd |
66 | { |
21d3f6ae |
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' ); |
f1064486 |
72 | is( $res->content, $output_YAML, "Request returned proper data"); |
21d3f6ae |
73 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
a51e7bbd |
74 | } |
75 | |
f1064486 |
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 |
0d86eca6 |
78 | # individual controllers; see t/lib/Test/Catalyst/Action/REST.pm |
a51e7bbd |
79 | { |
21d3f6ae |
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') |
a51e7bbd |
89 | } |
90 | |
e601adda |
91 | 1; |