Making the default serializer be specified by content-type, not module.
[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             'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
22             'text/broken'        => 'Broken',
23         },
24     }
25 );
26
27 __PACKAGE__->setup;
28
29 sub test :Local :ActionClass('Serialize') {
30     my ( $self, $c ) = @_;
31     $c->stash->{'rest'} = {
32         lou => 'is my cat',
33     };
34 }
35
36 sub test_second :Local :ActionClass('Serialize') {
37     my ( $self, $c ) = @_;
38     $c->stash->{'rest'} = {
39         lou => 'is my cat',
40     };
41 }
42
43 package main;
44
45 use strict;
46 use warnings;
47 use Test::More tests => 7;
48 use Data::Serializer;
49 use FindBin;
50 use Data::Dump qw(dump);
51
52 use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken");
53 use Test::Rest;
54
55 # Should use Data::Dumper, via YAML 
56 my $t = Test::Rest->new('content_type' => 'text/x-yaml');
57
58 use_ok 'Catalyst::Test', 'Test::Catalyst::Action::Serialize';
59
60 my $data = <<EOH;
61 --- 
62 lou: is my cat
63 EOH
64
65 {
66         my $req = $t->get(url => '/test');
67         $req->remove_header('Content-Type');
68         $req->header('Accept', 'text/x-yaml');
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 sure we don't get a bogus content-type when using default
76 # serializer (rt.cpan.org ticket 27949)
77 {
78         my $req = $t->get(url => '/test');
79         $req->remove_header('Content-Type');
80         $req->header('Accept', '*/*');
81         my $res = request($req);
82         ok( $res->is_success, 'GET the serialized request succeeded' );
83         is( $res->content, $data, "Request returned proper data");
84         is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
85 }
86
87 1;