X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fcatalyst-action-serialize-accept.t;h=836a8e86f600577e43745ee48634c50047b4fc7a;hb=9d3bee45561e72bc5f859bf1fc2d8164519b7c5a;hp=e91c27e2eeff76d55c7c775f97c125a7b9f80b85;hpb=367b3ff4e4444df4d20e8688c1cad9e1f59a3ba5;p=catagits%2FCatalyst-Action-REST.git diff --git a/t/catalyst-action-serialize-accept.t b/t/catalyst-action-serialize-accept.t index e91c27e..836a8e8 100644 --- a/t/catalyst-action-serialize-accept.t +++ b/t/catalyst-action-serialize-accept.t @@ -1,50 +1,6 @@ -package Test::Catalyst::Action::Serialize; - -use FindBin; - -use lib ("$FindBin::Bin/../lib"); - -use strict; -use warnings; - -use Catalyst::Runtime '5.70'; - -use Catalyst; - -__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', - }, - } -); - -__PACKAGE__->setup; - -sub test :Local :ActionClass('Serialize') { - my ( $self, $c ) = @_; - $c->stash->{'rest'} = { - lou => 'is my cat', - }; -} - -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 Test::More tests => 16; use Data::Serializer; use FindBin; use Data::Dump qw(dump); @@ -55,7 +11,7 @@ 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'; +use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST'; my $data = <get(url => '/test'); + 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, $data, "Request returned proper data"); + is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') + + }; +} + +SKIP: { + eval 'require 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 default +# serializer (rt.cpan.org ticket 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, $data, "Request returned proper data"); is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') } -# Make sure we don't get a bogus content-type when using default -# serializer (rt.cpan.org ticket 27949) +# Make that using content_type_stash_key, an invalid value in the stash gets ignored { - my $req = $t->get(url => '/test'); + my $req = $t->get(url => '/serialize/test_second?serialize_content_type=nonesuch'); $req->remove_header('Content-Type'); $req->header('Accept', '*/*'); my $res = request($req); @@ -84,4 +73,17 @@ EOH is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type') } +# Make that using content_type_stash_key, a valid value in the stash gets priority +{ + 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') +} + 1;