Add content_type_stash_key to enable a stash entry to override the serialize content...
[catagits/Catalyst-Action-REST.git] / t / catalyst-action-serialize-accept.t
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 => {
17         'default'   => 'text/x-yaml',
18         'stash_key' => 'rest',
19         'content_type_stash_key' => 'serialize_content_type',
20         'map'       => {
21             'text/x-yaml'        => 'YAML',
22             'application/json'   => 'JSON',
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 ) = @_;
40     $c->stash->{'serialize_content_type'} = $c->req->params->{'serialize_content_type'};
41     $c->stash->{'rest'} = {
42         lou => 'is my cat',
43     };
44 }
45
46
47 package main;
48
49 use strict;
50 use warnings;
51 use Test::More tests => 16;
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
64 my $data = <<EOH;
65 --- 
66 lou: is my cat
67 EOH
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);
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     };
85 }
86
87 SKIP: {
88     eval 'require JSON 2.12;';
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');
92         my $req = $at->get(url => '/test');
93         $req->header('Accept', 'application/json');
94         my $res = request($req);
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 };
100
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 }
112
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
135 1;