Commit | Line | Data |
e601adda |
1 | package Test::Catalyst::Action::Serialize; |
2 | |
3 | use FindBin; |
4 | |
5 | use lib ("$FindBin::Bin/../lib"); |
6 | |
7 | use strict; |
8 | use warnings; |
9 | |
10 | use Catalyst::Runtime '5.70'; |
11 | |
12 | use Catalyst; |
13 | |
14 | __PACKAGE__->config( |
15 | name => 'Test::Catalyst::Action::Serialize', |
16 | serialize => { |
faf5c20b |
17 | 'default' => 'text/x-yaml', |
e601adda |
18 | 'stash_key' => 'rest', |
a51e7bbd |
19 | 'content_type_stash_key' => 'serialize_content_type', |
e601adda |
20 | 'map' => { |
21 | 'text/x-yaml' => 'YAML', |
2f7533ed |
22 | 'application/json' => 'JSON', |
e601adda |
23 | 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], |
24 | 'text/broken' => 'Broken', |
25 | }, |
26 | } |
27 | ); |
28 | |
29 | __PACKAGE__->setup; |
30 | |
31 | sub test :Local :ActionClass('Serialize') { |
32 | my ( $self, $c ) = @_; |
33 | $c->stash->{'rest'} = { |
34 | lou => 'is my cat', |
35 | }; |
36 | } |
37 | |
38 | sub test_second :Local :ActionClass('Serialize') { |
39 | my ( $self, $c ) = @_; |
a51e7bbd |
40 | $c->stash->{'serialize_content_type'} = $c->req->params->{'serialize_content_type'}; |
e601adda |
41 | $c->stash->{'rest'} = { |
42 | lou => 'is my cat', |
43 | }; |
44 | } |
45 | |
a51e7bbd |
46 | |
e601adda |
47 | package main; |
48 | |
49 | use strict; |
50 | use warnings; |
a51e7bbd |
51 | use Test::More tests => 16; |
e601adda |
52 | use Data::Serializer; |
53 | use FindBin; |
54 | use Data::Dump qw(dump); |
55 | |
56 | use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken"); |
57 | use Test::Rest; |
58 | |
59 | # Should use Data::Dumper, via YAML |
60 | my $t = Test::Rest->new('content_type' => 'text/x-yaml'); |
61 | |
62 | use_ok 'Catalyst::Test', 'Test::Catalyst::Action::Serialize'; |
63 | |
e601adda |
64 | my $data = <<EOH; |
65 | --- |
66 | lou: is my cat |
67 | EOH |
367b3ff4 |
68 | |
69 | { |
70 | my $req = $t->get(url => '/test'); |
71 | $req->remove_header('Content-Type'); |
72 | $req->header('Accept', 'text/x-yaml'); |
73 | my $res = request($req); |
2f7533ed |
74 | SKIP: { |
75 | skip "can't test text/x-yaml without YAML support", |
76 | 3 if ( |
77 | not $res->is_success and |
78 | $res->content =~ m#Content-Type text/x-yaml is not supported# |
79 | ); |
80 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
81 | is( $res->content, $data, "Request returned proper data"); |
82 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
83 | |
84 | }; |
367b3ff4 |
85 | } |
86 | |
2f7533ed |
87 | SKIP: { |
fec6d454 |
88 | eval 'require JSON 2.12;'; |
2f7533ed |
89 | skip "can't test application/json without JSON support", 3 if $@; |
90 | my $json = JSON->new; |
91 | my $at = Test::Rest->new('content_type' => 'text/doesnt-exist'); |
c0aef9cd |
92 | my $req = $at->get(url => '/test'); |
93 | $req->header('Accept', 'application/json'); |
94 | my $res = request($req); |
2f7533ed |
95 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
96 | my $ret = $json->decode($res->content); |
97 | is( $ret->{lou}, 'is my cat', "Request returned proper data"); |
98 | is( $res->header('Content-type'), 'application/json', 'Accept header used if content-type mapping not found') |
99 | }; |
c0aef9cd |
100 | |
367b3ff4 |
101 | # Make sure we don't get a bogus content-type when using default |
102 | # serializer (rt.cpan.org ticket 27949) |
103 | { |
104 | my $req = $t->get(url => '/test'); |
105 | $req->remove_header('Content-Type'); |
106 | $req->header('Accept', '*/*'); |
107 | my $res = request($req); |
108 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
109 | is( $res->content, $data, "Request returned proper data"); |
110 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
111 | } |
e601adda |
112 | |
a51e7bbd |
113 | # Make that using content_type_stash_key, an invalid value in the stash gets ignored |
114 | { |
115 | my $req = $t->get(url => '/test_second?serialize_content_type=nonesuch'); |
116 | $req->remove_header('Content-Type'); |
117 | $req->header('Accept', '*/*'); |
118 | my $res = request($req); |
119 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
120 | is( $res->content, $data, "Request returned proper data"); |
121 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
122 | } |
123 | |
124 | # Make that using content_type_stash_key, a valid value in the stash gets priority |
125 | { |
126 | my $req = $t->get(url => '/test_second?serialize_content_type=text/x-data-dumper'); |
127 | $req->remove_header('Content-Type'); |
128 | $req->header('Accept', '*/*'); |
129 | my $res = request($req); |
130 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
131 | is( $res->content, "{'lou' => 'is my cat'}", "Request returned proper data"); |
132 | is( $res->header('Content-type'), 'text/x-data-dumper', '... with expected content-type') |
133 | } |
134 | |
e601adda |
135 | 1; |