Fixing the JSON::Syck reference
[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',
2f7533ed 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);
52
53use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken");
54use Test::Rest;
55
56# Should use Data::Dumper, via YAML
57my $t = Test::Rest->new('content_type' => 'text/x-yaml');
58
59use_ok 'Catalyst::Test', 'Test::Catalyst::Action::Serialize';
60
e601adda 61my $data = <<EOH;
62---
63lou: is my cat
64EOH
367b3ff4 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);
2f7533ed 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 };
367b3ff4 82}
83
2f7533ed 84SKIP: {
85 eval 'require JSON';
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');
c0aef9cd 89 my $req = $at->get(url => '/test');
90 $req->header('Accept', 'application/json');
91 my $res = request($req);
2f7533ed 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};
c0aef9cd 97
367b3ff4 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}
e601adda 109
1101;