Actually fixing the version number (hopefully)
[catagits/Catalyst-Action-REST.git] / t / catalyst-action-serialize-accept.t
CommitLineData
e601adda 1package Test::Catalyst::Action::Serialize;
2
3use FindBin;
4
5use lib ("$FindBin::Bin/../lib");
6
7use strict;
8use warnings;
9
10use Catalyst::Runtime '5.70';
11
12use 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
30sub test :Local :ActionClass('Serialize') {
31 my ( $self, $c ) = @_;
32 $c->stash->{'rest'} = {
33 lou => 'is my cat',
34 };
35}
36
37sub test_second :Local :ActionClass('Serialize') {
38 my ( $self, $c ) = @_;
39 $c->stash->{'rest'} = {
40 lou => 'is my cat',
41 };
42}
43
44package main;
45
46use strict;
47use warnings;
c0aef9cd 48use Test::More tests => 10;
e601adda 49use Data::Serializer;
50use FindBin;
51use Data::Dump qw(dump);
c0aef9cd 52use JSON::Syck;
e601adda 53
54use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken");
55use Test::Rest;
56
57# Should use Data::Dumper, via YAML
58my $t = Test::Rest->new('content_type' => 'text/x-yaml');
59
60use_ok 'Catalyst::Test', 'Test::Catalyst::Action::Serialize';
61
e601adda 62my $data = <<EOH;
63---
64lou: is my cat
65EOH
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
1001;