Permalink
Browse files

Merge branch 'default-serializer' into devel

  • Loading branch information...
2 parents 16c53c0 + a71e98e commit ec812f046a7b4a20b252aea2a52f7634aeea855e @yanick yanick committed Mar 3, 2014
Showing with 59 additions and 15 deletions.
  1. +6 −0 CHANGES
  2. +19 −9 lib/Dancer/Plugin/REST.pm
  3. +6 −6 t/04_plugin_settings.t
  4. +28 −0 t/default-serializer.t
View
@@ -1,3 +1,9 @@
+$NEXT
+
+ [BUG FIXES]
+ * 'prepare_serializer_for_format' was clobbering the
+ default serializer. (RT#93478, reported by Pierre Vigier)
+
0.07 2011-11-01
* Minor updates to fix warnings with Dancer 1.3080
View
@@ -15,6 +15,8 @@ my $content_types = {
xml => 'application/xml',
};
+our $default_serializer;
+
register prepare_serializer_for_format => sub {
my $conf = plugin_setting;
my $serializers = (
@@ -28,23 +30,31 @@ register prepare_serializer_for_format => sub {
);
hook 'before' => sub {
- my $format = params->{'format'};
- return unless defined $format;
- my $serializer = $serializers->{$format};
- unless (defined $serializer) {
- return halt(
+ my $format = params->{'format'}
+ or return;
+
+ # remember what was there before
+ $default_serializer = setting 'serializer';
+
+ my $serializer = $serializers->{$format}
+ or return halt(
Dancer::Error->new(
code => 404,
+ title => "unsupported format requested",
message => "unsupported format requested: " . $format
- )
+ )->render
);
- }
set serializer => $serializer;
- my $ct = $content_types->{$format} || setting('content_type');
- content_type $ct;
+
+ content_type $content_types->{$format} || setting('content_type');
};
+
+ hook after => sub {
+ # put it back the way it was
+ set serializer => $default_serializer if $default_serializer;
+ }
};
register resource => sub {
View
@@ -18,6 +18,8 @@ my $yaml = YAML::Dump($data);
use Dancer::Plugin::REST;
set environment => 'test';
+ set show_errors => 1;
+
prepare_serializer_for_format;
get '/' => sub { "root" };
@@ -41,17 +43,13 @@ my @tests = (
request => [GET => '/foo.yml'],
response => $yaml,
},
- {
- request => [GET => '/foo.foobar'],
- response => qr/unsupported format requested: foobar/ms,
- },
{
request => [GET => '/'],
response => 'root',
},
);
-plan tests => scalar(@tests);
+plan tests => 6;
for my $test ( @tests ) {
my $response = dancer_response(@{$test->{request}});
@@ -65,5 +63,7 @@ for my $test ( @tests ) {
}
}
+my $response = dancer_response( GET => '/foo.foobar' );
-
+is $response->status => 404, 'error code 404';
+like $response->content => qr/unsupported format requested: foobar/ms;
View
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+
+{
+ use Dancer ':tests';
+ use Dancer::Plugin::REST;
+
+ prepare_serializer_for_format;
+ set serializer => 'XML';
+
+ get '/foo' => sub {
+ return { hello => 'world' };
+ };
+
+ get '/foo.:format' => sub {
+ return { hello => 'world' };
+ };
+
+}
+
+use Test::More tests => 3;
+use Dancer::Test;
+
+response_content_like '/foo' => qr/<data/, "default serializer is XML";
+
+response_content_like '/foo.json' => qr/"hello"\s*:\s*"world"/, "ask for JSON";
+
+response_content_like '/foo' => qr/<data/, "default serializer is still XML";

0 comments on commit ec812f0

Please sign in to comment.