r7361@luke-mbp (orig r7801): lukes | 2008-05-26 15:09:41 +0100
lukes [Mon, 30 Jun 2008 18:50:29 +0000 (18:50 +0000)]
 new branch for decoupling Catalyst::Action::Serialize from REST
 r7362@luke-mbp (orig r7804):  lukes | 2008-05-26 18:50:20 +0100
 all accepted request types are tried before resorting to default or erroring
 r7363@luke-mbp (orig r7805):  lukes | 2008-05-26 18:53:17 +0100
 sanified code surrounding recent change
 r8416@luke-mbp (orig r8059):  lukes | 2008-06-30 19:50:17 +0100
 fixed stuff after pull

lib/Catalyst/Action/SerializeBase.pm
t/catalyst-action-serialize-accept.t

index 7a2980f..b7a6765 100644 (file)
@@ -36,14 +36,6 @@ sub _load_content_plugins {
         $self->_serialize_plugins( \@plugins );
     }
 
-    my $content_type = $c->request->preferred_content_type || '';
-
-    # carp about old text/x-json
-    if ($content_type eq 'text/x-json') {
-        $c->log->info('Using deprecated text/x-json content-type.');
-        $c->log->info('Use application/json instead!');
-    }
-
     # Finally, we load the class.  If you have a default serializer,
     # and we still don't have a content-type that exists in the map,
     # we'll use it.
@@ -63,7 +55,10 @@ sub _load_content_plugins {
     $map = $config->{'map'};
     # If we don't have a handler for our preferred content type, try
     # the default
-    if ( ! exists $map->{$content_type} ) {
+
+    my ($content_type) = grep { $map->{$_} } @{$c->request->accepted_content_types};
+
+    unless ( defined $content_type ) {
         if( exists $config->{'default'} ) {
             $content_type = $config->{'default'} ;
         } else {
@@ -71,6 +66,12 @@ sub _load_content_plugins {
         }
     }
 
+    # carp about old text/x-json
+    if ($content_type eq 'text/x-json') {
+        $c->log->info('Using deprecated text/x-json content-type.');
+        $c->log->info('Use application/json instead!');
+    }
+
     if ( exists( $map->{$content_type} ) ) {
         my $mc;
         if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
index 873a379..12f5df6 100644 (file)
@@ -18,6 +18,7 @@ __PACKAGE__->config(
         'stash_key' => 'rest',
         'map'       => {
             'text/x-yaml'        => 'YAML',
+            'application/json'        => 'JSON',
             'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
             'text/broken'        => 'Broken',
         },
@@ -44,10 +45,11 @@ package main;
 
 use strict;
 use warnings;
-use Test::More tests => 7;
+use Test::More tests => 10;
 use Data::Serializer;
 use FindBin;
 use Data::Dump qw(dump);
+use JSON::Syck; 
 
 use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib", "$FindBin::Bin/broken");
 use Test::Rest;
@@ -72,6 +74,17 @@ EOH
        is( $res->header('Content-type'), 'text/x-yaml', '... with expected content-type')
 }
 
+{
+        my $at = Test::Rest->new('content_type' => 'text/doesnt-exist');               
+       my $req = $at->get(url => '/test');
+       $req->header('Accept', 'application/json');
+       my $res = request($req);
+       ok( $res->is_success, 'GET the serialized request succeeded' );
+        my $ret = JSON::Syck::Load($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)
 {