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', |
19 | 'map' => { |
20 | 'text/x-yaml' => 'YAML', |
c0aef9cd |
21 | 'application/json' => 'JSON', |
e601adda |
22 | 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], |
23 | 'text/broken' => 'Broken', |
24 | }, |
25 | } |
26 | ); |
27 | |
28 | __PACKAGE__->setup; |
29 | |
30 | sub test :Local :ActionClass('Serialize') { |
31 | my ( $self, $c ) = @_; |
32 | $c->stash->{'rest'} = { |
33 | lou => 'is my cat', |
34 | }; |
35 | } |
36 | |
37 | sub test_second :Local :ActionClass('Serialize') { |
38 | my ( $self, $c ) = @_; |
39 | $c->stash->{'rest'} = { |
40 | lou => 'is my cat', |
41 | }; |
42 | } |
43 | |
44 | package main; |
45 | |
46 | use strict; |
47 | use warnings; |
c0aef9cd |
48 | use Test::More tests => 10; |
e601adda |
49 | use Data::Serializer; |
50 | use FindBin; |
51 | use Data::Dump qw(dump); |
c0aef9cd |
52 | use JSON::Syck; |
e601adda |
53 | |
54 | use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken"); |
55 | use Test::Rest; |
56 | |
57 | # Should use Data::Dumper, via YAML |
58 | my $t = Test::Rest->new('content_type' => 'text/x-yaml'); |
59 | |
60 | use_ok 'Catalyst::Test', 'Test::Catalyst::Action::Serialize'; |
61 | |
e601adda |
62 | my $data = <<EOH; |
63 | --- |
64 | lou: is my cat |
65 | EOH |
367b3ff4 |
66 | |
67 | { |
68 | my $req = $t->get(url => '/test'); |
69 | $req->remove_header('Content-Type'); |
70 | $req->header('Accept', 'text/x-yaml'); |
71 | my $res = request($req); |
72 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
73 | is( $res->content, $data, "Request returned proper data"); |
74 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
75 | } |
76 | |
c0aef9cd |
77 | { |
78 | my $at = Test::Rest->new('content_type' => 'text/doesnt-exist'); |
79 | my $req = $at->get(url => '/test'); |
80 | $req->header('Accept', 'application/json'); |
81 | my $res = request($req); |
82 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
83 | my $ret = JSON::Syck::Load($res->content); |
84 | is( $ret->{lou}, 'is my cat', "Request returned proper data"); |
85 | is( $res->header('Content-type'), 'application/json', 'Accept header used if content-type mapping not found') |
86 | } |
87 | |
367b3ff4 |
88 | # Make sure we don't get a bogus content-type when using default |
89 | # serializer (rt.cpan.org ticket 27949) |
90 | { |
91 | my $req = $t->get(url => '/test'); |
92 | $req->remove_header('Content-Type'); |
93 | $req->header('Accept', '*/*'); |
94 | my $res = request($req); |
95 | ok( $res->is_success, 'GET the serialized request succeeded' ); |
96 | is( $res->content, $data, "Request returned proper data"); |
97 | is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') |
98 | } |
e601adda |
99 | |
100 | 1; |