Commit | Line | Data |
e601adda |
1 | use strict; |
2 | use warnings; |
960e29ee |
3 | use Test::More; |
e601adda |
4 | use FindBin; |
e601adda |
5 | |
6 | use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken"); |
7 | use Test::Rest; |
f1064486 |
8 | use Catalyst::Action::Serialize::YAML; |
e601adda |
9 | |
21d3f6ae |
10 | # Should use Data::Dumper, via YAML |
e601adda |
11 | my $t = Test::Rest->new('content_type' => 'text/x-yaml'); |
12 | |
74bc96b9 |
13 | use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST'; |
e601adda |
14 | |
f1064486 |
15 | # to avoid whatever serialization bugs YAML::Syck has, |
16 | # e.g. http://rt.cpan.org/Public/Bug/Display.html?id=46983, |
17 | # we won't hardcode the expected output |
18 | my $output_YAML = Catalyst::Action::Serialize::YAML->serialize({lou => 'is my cat'}); |
367b3ff4 |
19 | |
20 | { |
21d3f6ae |
21 | my $req = $t->get(url => '/serialize/test'); |
22 | $req->remove_header('Content-Type'); |
23 | $req->header('Accept', 'text/x-yaml'); |
24 | my $res = request($req); |
2f7533ed |
25 | SKIP: { |
26 | skip "can't test text/x-yaml without YAML support", |
21d3f6ae |
27 | 3 if ( |
28 | not $res->is_success and |
29 | $res->content =~ m#Content-Type text/x-yaml is not supported# |
2f7533ed |
30 | ); |
21d3f6ae |
31 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
f1064486 |
32 | is( $res->content, $output_YAML, "Request returned proper data"); |
21d3f6ae |
33 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
2f7533ed |
34 | |
35 | }; |
367b3ff4 |
36 | } |
37 | |
2f7533ed |
38 | SKIP: { |
8b010556 |
39 | eval 'use JSON 2.12;'; |
2f7533ed |
40 | skip "can't test application/json without JSON support", 3 if $@; |
41 | my $json = JSON->new; |
42 | my $at = Test::Rest->new('content_type' => 'text/doesnt-exist'); |
21d3f6ae |
43 | my $req = $at->get(url => '/serialize/test'); |
44 | $req->header('Accept', 'application/json'); |
45 | my $res = request($req); |
2f7533ed |
46 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
47 | my $ret = $json->decode($res->content); |
48 | is( $ret->{lou}, 'is my cat', "Request returned proper data"); |
49 | is( $res->header('Content-type'), 'application/json', 'Accept header used if content-type mapping not found') |
50 | }; |
c0aef9cd |
51 | |
f1064486 |
52 | # Make sure we don't get a bogus content-type when using the default |
53 | # serializer (https://rt.cpan.org/Ticket/Display.html?id=27949) |
367b3ff4 |
54 | { |
21d3f6ae |
55 | my $req = $t->get(url => '/serialize/test'); |
56 | $req->remove_header('Content-Type'); |
57 | $req->header('Accept', '*/*'); |
58 | my $res = request($req); |
59 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
f1064486 |
60 | is( $res->content, $output_YAML, "Request returned proper data"); |
21d3f6ae |
61 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
367b3ff4 |
62 | } |
e601adda |
63 | |
f1064486 |
64 | # Make sure that when using content_type_stash_key, an invalid value in the stash gets ignored |
a51e7bbd |
65 | { |
21d3f6ae |
66 | my $req = $t->get(url => '/serialize/test_second?serialize_content_type=nonesuch'); |
67 | $req->remove_header('Content-Type'); |
68 | $req->header('Accept', '*/*'); |
69 | my $res = request($req); |
70 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
f1064486 |
71 | is( $res->content, $output_YAML, "Request returned proper data"); |
21d3f6ae |
72 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
a51e7bbd |
73 | } |
74 | |
960e29ee |
75 | # Make sure that the default content type you specify really gets used. |
76 | { |
77 | my $req = $t->get(url => '/override/test'); |
78 | $req->remove_header('Content-Type'); |
79 | my $res = request($req); |
80 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
81 | is( $res->content, "--- \nlou: is my cat\n", "Request returned proper data"); |
82 | } |
83 | |
84 | done_testing; |
85 | |