[libdancer-plugin-rest-perl] 02/12: reset the serializer once done
Jonas Smedegaard
dr at jones.dk
Wed Jul 2 11:45:04 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to annotated tag v0.08
in repository libdancer-plugin-rest-perl.
commit 708b3f0269a3a5f26c031097196a205b6997d62d
Author: Yanick Champoux <yanick at babyl.dyndns.org>
Date: Mon Mar 3 14:31:56 2014 -0500
reset the serializer once done
---
lib/Dancer/Plugin/REST.pm | 11 +++++++++++
t/default-serializer.t | 33 +++++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+)
diff --git a/lib/Dancer/Plugin/REST.pm b/lib/Dancer/Plugin/REST.pm
index e9fd152..4d538d6 100644
--- a/lib/Dancer/Plugin/REST.pm
+++ b/lib/Dancer/Plugin/REST.pm
@@ -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,9 +30,13 @@ register prepare_serializer_for_format => sub {
);
hook 'before' => sub {
+
my $format = params->{'format'};
return unless defined $format;
+ # remember what was there before
+ $default_serializer = setting 'serializer';
+
my $serializer = $serializers->{$format};
unless (defined $serializer) {
return halt(
@@ -45,6 +51,11 @@ register prepare_serializer_for_format => sub {
my $ct = $content_types->{$format} || setting('content_type');
content_type $ct;
};
+
+ hook after => sub {
+ # put it back the way it was
+ set serializer => $default_serializer if $default_serializer;
+ }
};
register resource => sub {
diff --git a/t/default-serializer.t b/t/default-serializer.t
new file mode 100644
index 0000000..728d1cb
--- /dev/null
+++ b/t/default-serializer.t
@@ -0,0 +1,33 @@
+use strict;
+use warnings;
+
+use Test::More tests => 1;
+
+{
+ 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 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";
+
+
+
+
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libdancer-plugin-rest-perl.git
More information about the Pkg-perl-cvs-commits
mailing list