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; |
9 | |
10 | # Should use Data::Dumper, via YAML |
11 | my $t = Test::Rest->new('content_type' => 'text/x-yaml'); |
12 | |
74bc96b9 |
13 | use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST'; |
e601adda |
14 | |
e601adda |
15 | my $data = <<EOH; |
16 | --- |
17 | lou: is my cat |
18 | EOH |
367b3ff4 |
19 | |
20 | { |
74bc96b9 |
21 | my $req = $t->get(url => '/serialize/test'); |
367b3ff4 |
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", |
27 | 3 if ( |
28 | not $res->is_success and |
29 | $res->content =~ m#Content-Type text/x-yaml is not supported# |
30 | ); |
31 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
32 | is( $res->content, $data, "Request returned proper data"); |
33 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
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'); |
74bc96b9 |
43 | my $req = $at->get(url => '/serialize/test'); |
c0aef9cd |
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 | |
367b3ff4 |
52 | # Make sure we don't get a bogus content-type when using default |
53 | # serializer (rt.cpan.org ticket 27949) |
54 | { |
74bc96b9 |
55 | my $req = $t->get(url => '/serialize/test'); |
367b3ff4 |
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' ); |
60 | is( $res->content, $data, "Request returned proper data"); |
61 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
62 | } |
e601adda |
63 | |
a51e7bbd |
64 | # Make that using content_type_stash_key, an invalid value in the stash gets ignored |
65 | { |
74bc96b9 |
66 | my $req = $t->get(url => '/serialize/test_second?serialize_content_type=nonesuch'); |
a51e7bbd |
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' ); |
71 | is( $res->content, $data, "Request returned proper data"); |
72 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
73 | } |
74 | |
75 | # Make that using content_type_stash_key, a valid value in the stash gets priority |
0d86eca6 |
76 | # this also tests that application-level config is properly passed to |
77 | # individual controllers; see t/lib/Test/Catalyst/Action/REST.pm |
a51e7bbd |
78 | { |
74bc96b9 |
79 | my $req = $t->get(url => |
80 | '/serialize/test_second?serialize_content_type=text/x-data-dumper' |
81 | ); |
a51e7bbd |
82 | $req->remove_header('Content-Type'); |
83 | $req->header('Accept', '*/*'); |
84 | my $res = request($req); |
85 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
86 | is( $res->content, "{'lou' => 'is my cat'}", "Request returned proper data"); |
87 | is( $res->header('Content-type'), 'text/x-data-dumper', '... with expected content-type') |
88 | } |
89 | |
e601adda |
90 | 1; |