X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Action-REST.git;a=blobdiff_plain;f=t%2Fcatalyst-action-serialize-accept.t;h=64732f510892e5af70edb5730860713c9662ab34;hp=873a3796509b0a1af21c48ab5b03a20689227657;hb=960e29eed9ccd049c3c0dcd78a65308242a64ae0;hpb=faf5c20bff09c91f18b46a3d3d8349379aa36192 diff --git a/t/catalyst-action-serialize-accept.t b/t/catalyst-action-serialize-accept.t index 873a379..64732f5 100644 --- a/t/catalyst-action-serialize-accept.t +++ b/t/catalyst-action-serialize-accept.t @@ -1,87 +1,101 @@ -package Test::Catalyst::Action::Serialize; - -use FindBin; - -use lib ("$FindBin::Bin/../lib"); - use strict; use warnings; +use Test::More; +use Data::Serializer; +use FindBin; -use Catalyst::Runtime '5.70'; +use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken"); +use Test::Rest; +use Catalyst::Action::Serialize::YAML; -use Catalyst; +# Should use Data::Dumper, via YAML +my $t = Test::Rest->new('content_type' => 'text/x-yaml'); -__PACKAGE__->config( - name => 'Test::Catalyst::Action::Serialize', - serialize => { - 'default' => 'text/x-yaml', - 'stash_key' => 'rest', - 'map' => { - 'text/x-yaml' => 'YAML', - 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], - 'text/broken' => 'Broken', - }, - } -); +use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST'; -__PACKAGE__->setup; +# to avoid whatever serialization bugs YAML::Syck has, +# e.g. http://rt.cpan.org/Public/Bug/Display.html?id=46983, +# we won't hardcode the expected output +my $output_YAML = Catalyst::Action::Serialize::YAML->serialize({lou => 'is my cat'}); -sub test :Local :ActionClass('Serialize') { - my ( $self, $c ) = @_; - $c->stash->{'rest'} = { - lou => 'is my cat', - }; -} +{ + my $req = $t->get(url => '/serialize/test'); + $req->remove_header('Content-Type'); + $req->header('Accept', 'text/x-yaml'); + my $res = request($req); + SKIP: { + skip "can't test text/x-yaml without YAML support", + 3 if ( + not $res->is_success and + $res->content =~ m#Content-Type text/x-yaml is not supported# + ); + ok( $res->is_success, 'GET the serialized request succeeded' ); + is( $res->content, $output_YAML, "Request returned proper data"); + is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') -sub test_second :Local :ActionClass('Serialize') { - my ( $self, $c ) = @_; - $c->stash->{'rest'} = { - lou => 'is my cat', }; } -package main; - -use strict; -use warnings; -use Test::More tests => 7; -use Data::Serializer; -use FindBin; -use Data::Dump qw(dump); - -use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken"); -use Test::Rest; - -# Should use Data::Dumper, via YAML -my $t = Test::Rest->new('content_type' => 'text/x-yaml'); - -use_ok 'Catalyst::Test', 'Test::Catalyst::Action::Serialize'; +SKIP: { + eval 'use JSON 2.12;'; + skip "can't test application/json without JSON support", 3 if $@; + my $json = JSON->new; + my $at = Test::Rest->new('content_type' => 'text/doesnt-exist'); + my $req = $at->get(url => '/serialize/test'); + $req->header('Accept', 'application/json'); + my $res = request($req); + ok( $res->is_success, 'GET the serialized request succeeded' ); + my $ret = $json->decode($res->content); + is( $ret->{lou}, 'is my cat', "Request returned proper data"); + is( $res->header('Content-type'), 'application/json', 'Accept header used if content-type mapping not found') +}; + +# Make sure we don't get a bogus content-type when using the default +# serializer (https://rt.cpan.org/Ticket/Display.html?id=27949) +{ + my $req = $t->get(url => '/serialize/test'); + $req->remove_header('Content-Type'); + $req->header('Accept', '*/*'); + my $res = request($req); + ok( $res->is_success, 'GET the serialized request succeeded' ); + is( $res->content, $output_YAML, "Request returned proper data"); + is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') +} -my $data = <get(url => '/serialize/test_second?serialize_content_type=nonesuch'); + $req->remove_header('Content-Type'); + $req->header('Accept', '*/*'); + my $res = request($req); + ok( $res->is_success, 'GET the serialized request succeeded' ); + is( $res->content, $output_YAML, "Request returned proper data"); + is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') +} +# Make sure that when using content_type_stash_key, a valid value in the stash gets priority. +# This also tests that application-level config is properly passed to +# individual controllers; see t/lib/Test/Catalyst/Action/REST.pm { - my $req = $t->get(url => '/test'); - $req->remove_header('Content-Type'); - $req->header('Accept', 'text/x-yaml'); - my $res = request($req); - ok( $res->is_success, 'GET the serialized request succeeded' ); - is( $res->content, $data, "Request returned proper data"); - is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') + my $req = $t->get(url => + '/serialize/test_second?serialize_content_type=text/x-data-dumper' + ); + $req->remove_header('Content-Type'); + $req->header('Accept', '*/*'); + my $res = request($req); + ok( $res->is_success, 'GET the serialized request succeeded' ); + is( $res->content, "{'lou' => 'is my cat'}", "Request returned proper data"); + is( $res->header('Content-type'), 'text/x-data-dumper', '... with expected content-type') } -# Make sure we don't get a bogus content-type when using default -# serializer (rt.cpan.org ticket 27949) +# Make sure that the default content type you specify really gets used. { - my $req = $t->get(url => '/test'); - $req->remove_header('Content-Type'); - $req->header('Accept', '*/*'); - my $res = request($req); - ok( $res->is_success, 'GET the serialized request succeeded' ); - is( $res->content, $data, "Request returned proper data"); - is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') + my $req = $t->get(url => '/override/test'); + $req->remove_header('Content-Type'); + my $res = request($req); + ok( $res->is_success, 'GET the serialized request succeeded' ); + is( $res->content, "--- \nlou: is my cat\n", "Request returned proper data"); } -1; +done_testing; +