Fixing Makefile.PL, RT#42859
[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         'map'       => {
20             'text/x-yaml'        => 'YAML',
21             'application/json'   => 'JSON',
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;
48 use Test::More tests => 10;
49 use Data::Serializer;
50 use FindBin;
51 use Data::Dump qw(dump);
52
53 use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken");
54 use Test::Rest;
55
56 # Should use Data::Dumper, via YAML 
57 my $t = Test::Rest->new('content_type' => 'text/x-yaml');
58
59 use_ok 'Catalyst::Test', 'Test::Catalyst::Action::Serialize';
60
61 my $data = <<EOH;
62 --- 
63 lou: is my cat
64 EOH
65
66 {
67         my $req = $t->get(url => '/test');
68         $req->remove_header('Content-Type');
69         $req->header('Accept', 'text/x-yaml');
70         my $res = request($req);
71     SKIP: {
72         skip "can't test text/x-yaml without YAML support",
73         3 if ( 
74                 not $res->is_success and 
75                 $res->content =~ m#Content-Type text/x-yaml is not supported# 
76              );
77             ok( $res->is_success, 'GET the serialized request succeeded' );
78             is( $res->content, $data, "Request returned proper data");
79             is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
80
81     };
82 }
83
84 SKIP: {
85     eval 'require JSON 2.12;';
86     skip "can't test application/json without JSON support", 3 if $@;
87     my $json = JSON->new;
88     my $at = Test::Rest->new('content_type' => 'text/doesnt-exist');
89         my $req = $at->get(url => '/test');
90         $req->header('Accept', 'application/json');
91         my $res = request($req);
92     ok( $res->is_success, 'GET the serialized request succeeded' );
93     my $ret = $json->decode($res->content);
94     is( $ret->{lou}, 'is my cat', "Request returned proper data");
95     is( $res->header('Content-type'), 'application/json', 'Accept header used if content-type mapping not found')
96 };
97
98 # Make sure we don't get a bogus content-type when using default
99 # serializer (rt.cpan.org ticket 27949)
100 {
101         my $req = $t->get(url => '/test');
102         $req->remove_header('Content-Type');
103         $req->header('Accept', '*/*');
104         my $res = request($req);
105         ok( $res->is_success, 'GET the serialized request succeeded' );
106         is( $res->content, $data, "Request returned proper data");
107         is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
108 }
109
110 1;