refactor another single-file app
[catagits/Catalyst-Action-REST.git] / t / catalyst-action-serialize-accept.t
CommitLineData
e601adda 1use strict;
2use warnings;
a51e7bbd 3use Test::More tests => 16;
e601adda 4use Data::Serializer;
5use FindBin;
6use Data::Dump qw(dump);
7
8use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken");
9use Test::Rest;
10
11# Should use Data::Dumper, via YAML
12my $t = Test::Rest->new('content_type' => 'text/x-yaml');
13
74bc96b9 14use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST';
e601adda 15
e601adda 16my $data = <<EOH;
17---
18lou: is my cat
19EOH
367b3ff4 20
21{
74bc96b9 22 my $req = $t->get(url => '/serialize/test');
367b3ff4 23 $req->remove_header('Content-Type');
24 $req->header('Accept', 'text/x-yaml');
25 my $res = request($req);
2f7533ed 26 SKIP: {
27 skip "can't test text/x-yaml without YAML support",
28 3 if (
29 not $res->is_success and
30 $res->content =~ m#Content-Type text/x-yaml is not supported#
31 );
32 ok( $res->is_success, 'GET the serialized request succeeded' );
33 is( $res->content, $data, "Request returned proper data");
34 is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
35
36 };
367b3ff4 37}
38
2f7533ed 39SKIP: {
fec6d454 40 eval 'require JSON 2.12;';
2f7533ed 41 skip "can't test application/json without JSON support", 3 if $@;
42 my $json = JSON->new;
43 my $at = Test::Rest->new('content_type' => 'text/doesnt-exist');
74bc96b9 44 my $req = $at->get(url => '/serialize/test');
c0aef9cd 45 $req->header('Accept', 'application/json');
46 my $res = request($req);
2f7533ed 47 ok( $res->is_success, 'GET the serialized request succeeded' );
48 my $ret = $json->decode($res->content);
49 is( $ret->{lou}, 'is my cat', "Request returned proper data");
50 is( $res->header('Content-type'), 'application/json', 'Accept header used if content-type mapping not found')
51};
c0aef9cd 52
367b3ff4 53# Make sure we don't get a bogus content-type when using default
54# serializer (rt.cpan.org ticket 27949)
55{
74bc96b9 56 my $req = $t->get(url => '/serialize/test');
367b3ff4 57 $req->remove_header('Content-Type');
58 $req->header('Accept', '*/*');
59 my $res = request($req);
60 ok( $res->is_success, 'GET the serialized request succeeded' );
61 is( $res->content, $data, "Request returned proper data");
62 is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
63}
e601adda 64
a51e7bbd 65# Make that using content_type_stash_key, an invalid value in the stash gets ignored
66{
74bc96b9 67 my $req = $t->get(url => '/serialize/test_second?serialize_content_type=nonesuch');
a51e7bbd 68 $req->remove_header('Content-Type');
69 $req->header('Accept', '*/*');
70 my $res = request($req);
71 ok( $res->is_success, 'GET the serialized request succeeded' );
72 is( $res->content, $data, "Request returned proper data");
73 is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
74}
75
76# Make that using content_type_stash_key, a valid value in the stash gets priority
77{
74bc96b9 78 my $req = $t->get(url =>
79 '/serialize/test_second?serialize_content_type=text/x-data-dumper'
80 );
a51e7bbd 81 $req->remove_header('Content-Type');
82 $req->header('Accept', '*/*');
83 my $res = request($req);
84 ok( $res->is_success, 'GET the serialized request succeeded' );
85 is( $res->content, "{'lou' => 'is my cat'}", "Request returned proper data");
86 is( $res->header('Content-type'), 'text/x-data-dumper', '... with expected content-type')
87}
88
e601adda 891;