Skip to content

Commit

Permalink
Merge branch 'release/1.3002'
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Sukrieh committed Feb 2, 2011
2 parents 36ea7e8 + 4292413 commit 0466b40
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 17 deletions.
18 changes: 18 additions & 0 deletions CHANGES
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,21 @@
Dancer 1.3002

[ API CHANGES ]

* to_json and from_json accept options as hashref instead of hash.
Passing arguments as hash is deprecated
(Franck Cuny).

[ BUGFIXES ]

* status is kept even when halt is used in a before filter
(Alexis Sukrieh)

[ ENHANCEMENTS ]

* In development, pretty-print JSON serializations for easier development
(Ask Bjørn Hansen)

Dancer 1.3001 Dancer 1.3001


[ Flavio Poletti ] [ Flavio Poletti ]
Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ t/12_response/02_headers.t
t/12_response/03_charset.t t/12_response/03_charset.t
t/12_response/04_charset_server.t t/12_response/04_charset_server.t
t/12_response/05_api.t t/12_response/05_api.t
t/12_response/06_filter_halt_status.t
t/13_engines/00_load.t t/13_engines/00_load.t
t/13_engines/02_template_init.t t/13_engines/02_template_init.t
t/14_serializer/000_create_fake_env.t t/14_serializer/000_create_fake_env.t
Expand Down
6 changes: 4 additions & 2 deletions lib/Dancer.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ use strict;
use warnings; use warnings;
use Carp; use Carp;
use Cwd 'abs_path', 'realpath'; use Cwd 'abs_path', 'realpath';

use vars qw($VERSION $AUTHORITY @EXPORT); use vars qw($VERSION $AUTHORITY @EXPORT);


$VERSION = '1.3002';
$AUTHORITY = 'SUKRIA';

use Dancer::Config; use Dancer::Config;
use Dancer::FileUtils; use Dancer::FileUtils;
use Dancer::GetOpt; use Dancer::GetOpt;
Expand All @@ -30,8 +34,6 @@ use File::Basename 'basename';


use base 'Exporter'; use base 'Exporter';


$AUTHORITY = 'SUKRIA';
$VERSION = '1.3001';
@EXPORT = qw( @EXPORT = qw(
after after
any any
Expand Down
9 changes: 7 additions & 2 deletions lib/Dancer/Response.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -127,8 +127,13 @@ sub halt {
$CURRENT = $content; $CURRENT = $content;
} }
else { else {
my $resp = Dancer::Response->new(content => $content); # FIXME we probably want to do better here, I think this class really deserves a
$CURRENT = $resp; # complete rewrite ;-)
my $resp = Dancer::Response->new(
status => ($CURRENT->{status} || 200),
content => $content,
);
$CURRENT = $resp;
} }
$CURRENT->{halted} = 1; $CURRENT->{halted} = 1;
return $content; return $content;
Expand Down
43 changes: 35 additions & 8 deletions lib/Dancer/Serializer/JSON.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,25 +31,52 @@ sub init {
} }


sub serialize { sub serialize {
my ($self, $entity, %options) = @_; my $self = shift;
my $entity = shift;

my $options = $self->_options_as_hashref(@_) || {};


# Why doesn't $self->config have this? # Why doesn't $self->config have this?
my $config = setting('engines') || {}; my $config = setting('engines') || {};
$config = $config->{JSON} || {}; $config = $config->{JSON} || {};


if ($config->{allow_blessed} && !defined $options{allow_blessed}) { if ( $config->{allow_blessed} && !defined $options->{allow_blessed} ) {
$options{allow_blessed} = $config->{allow_blessed}; $options->{allow_blessed} = $config->{allow_blessed};
}
if ( $config->{convert_blessed} ) {
$options->{convert_blessed} = $config->{convert_blessed};
} }
if ($config->{convert_blessed}) {
$options{convert_blessed} = $config->{convert_blessed}; if (setting('environment') eq 'development' and not defined $options->{pretty}) {
$options->{pretty} = 1;
} }


JSON::to_json($entity, \%options); JSON::to_json( $entity, $options );
} }


sub deserialize { sub deserialize {
my ($self, $entity, %options) = @_; my $self = shift;
JSON::from_json($entity, \%options); my $entity = shift;

my $options = $self->_options_as_hashref(@_);
JSON::from_json( $entity, $options );
}

sub _options_as_hashref {
my $self = shift;

return if scalar @_ == 0;

if ( scalar @_ == 1 ) {
return shift;
}
elsif ( scalar @_ % 2 ) {
carp "options for to_json/from_json must be key value pairs (as a hashref)";
}
else {
carp "options as hash for to_json/from_json is DEPRECATED. please pass a hashref.";
return { @_ };
}
} }


sub content_type {'application/json'} sub content_type {'application/json'}
Expand Down
2 changes: 2 additions & 0 deletions t/12_response/03_charset.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use Encode;


plan tests => 16; plan tests => 16;


set environment => 'production';

my $res = Dancer::Response->new(headers => [ 'Content-Type' => 'text/html' ], content_type => 'text/html'); my $res = Dancer::Response->new(headers => [ 'Content-Type' => 'text/html' ], content_type => 'text/html');
my $psgi_res = Dancer::Handler->render_response($res); my $psgi_res = Dancer::Handler->render_response($res);
is(@$psgi_res, 3); is(@$psgi_res, 3);
Expand Down
44 changes: 44 additions & 0 deletions t/12_response/06_filter_halt_status.t
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,44 @@
use strict;
use warnings;
use Test::More;

# make sure we keep the status when halt is used

{
package SomeApp;
use Dancer;

set serializer => 'JSON';
set environment => 'production';

before sub {
if (params->{'troll'}) {
status 401;
return halt({error => "Go away you troll!"})
}
};

get '/' => sub {
"root"
};
}

use Dancer::Test;

my @tests = (
['/', {}, 200, 'root'],
['/', {troll => 1}, 401,
'{"error":"Go away you troll!"}'],
);

plan tests => scalar(@tests) * 2;

for my $t (@tests) {
my ($path, $params, $status, $content) = @{ $t };

my $resp = dancer_response(GET => $path, { params => $params });
is $resp->{status}, $status, "status is $status";
is $resp->{content}, $content, "content is $content";
}


25 changes: 21 additions & 4 deletions t/14_serializer/02_json.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Dancer;


plan skip_all => "JSON is needed to run this tests" plan skip_all => "JSON is needed to run this tests"
unless Dancer::ModuleLoader->load('JSON'); unless Dancer::ModuleLoader->load('JSON');
plan tests => 14; plan tests => 17;


eval { eval {
setting serializer => 'FooBar'; setting serializer => 'FooBar';
Expand All @@ -23,21 +23,24 @@ isa_ok($s, $_) for qw(
can_ok $s, qw(serialize deserialize); can_ok $s, qw(serialize deserialize);


my $data = { foo => 42 }; my $data = { foo => 42 };
my $json = $s->serialize($data); my $json = $s->serialize($data, { pretty => 0 });
is $json, '{"foo":42}', "data is correctly serialized"; is $json, '{"foo":42}', "data is correctly serialized";
my $data2 = $s->deserialize($json); my $data2 = $s->deserialize($json);
is_deeply $data2, $data, "data is correctly deserialized"; is_deeply $data2, $data, "data is correctly deserialized";


# helpers # helpers


$json = to_json($data); $json = to_json($data);
is $json, '{"foo":42}', "data is correctly serialized"; is $json, '{
"foo" : 42
}
', "data is correctly serialized";


$data2 = from_json($json); $data2 = from_json($json);
is_deeply($data2, $data, "data is correctly deserialized"); is_deeply($data2, $data, "data is correctly deserialized");


$data = {foo => {bar => {baz => [qw/23 42/]}}}; $data = {foo => {bar => {baz => [qw/23 42/]}}};
$json = to_json($data, pretty => 1); $json = to_json($data, {pretty => 1});
like $json, qr/"foo" : {/, "data is pretty!"; like $json, qr/"foo" : {/, "data is pretty!";
$data2 = from_json($json); $data2 = from_json($json);
is_deeply($data2, $data, "data is correctly deserialized"); is_deeply($data2, $data, "data is correctly deserialized");
Expand All @@ -47,6 +50,7 @@ my $config = {
JSON => { JSON => {
allow_blessed => 1, allow_blessed => 1,
convert_blessed => 1, convert_blessed => 1,
pretty => 0,
} }
} }
}; };
Expand All @@ -56,3 +60,16 @@ ok $s = Dancer::Serializer->init( 'JSON', $config ),
$data = { foo => 'bar' }; $data = { foo => 'bar' };
my $res = $s->serialize($data); my $res = $s->serialize($data);
is_deeply( $data, JSON::decode_json($res), 'data is correctly serialized' ); is_deeply( $data, JSON::decode_json($res), 'data is correctly serialized' );

# XXX tests for deprecation
my $warn;
local $SIG{__WARN__} = sub { $warn = $_[0] };
$s->_options_as_hashref(foo => 'bar');
ok $warn, 'deprecation warning';
undef $warn;

$s->_options_as_hashref({foo => 'bar'});
ok !$warn, 'no deprecation warning';

to_json({foo => 'bar'}, indent => 0);
ok $warn, 'deprecation warning';
2 changes: 2 additions & 0 deletions t/14_serializer/04_mutable.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ BEGIN {


plan tests => 17; plan tests => 17;


set environment => 'production';

ok(setting('serializer' => 'Mutable'), "serializer Mutable loaded"); ok(setting('serializer' => 'Mutable'), "serializer Mutable loaded");
my $s = Dancer::Serializer->engine; my $s = Dancer::Serializer->engine;


Expand Down
4 changes: 4 additions & 0 deletions t/14_serializer/05_live.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use strict;
use warnings; use warnings;
use Test::More import => ['!pass']; use Test::More import => ['!pass'];


use Carp;
$Carp::Verbose = 1;

BEGIN { BEGIN {
use Dancer::ModuleLoader; use Dancer::ModuleLoader;


Expand All @@ -18,6 +21,7 @@ use LWP::UserAgent;
use HTTP::Request; use HTTP::Request;


plan tests => 30; plan tests => 30;
set environment => 'production';


test_json(); test_json();
test_yaml(); test_yaml();
Expand Down
1 change: 1 addition & 0 deletions t/14_serializer/06_serialize_response.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ my $json = JSON::encode_json($data);
use Dancer ':syntax'; use Dancer ':syntax';


set serializer => 'JSON'; set serializer => 'JSON';
set environment => 'production';


get '/data' => sub { get '/data' => sub {
$data; $data;
Expand Down
1 change: 1 addition & 0 deletions t/14_serializer/14_show_errors.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Test::TCP::test_tcp(
setting port => $port; setting port => $port;
setting access_log => 0; setting access_log => 0;
setting serializer => 'JSON'; setting serializer => 'JSON';
set environment => 'production';


get '/with_errors' => sub { get '/with_errors' => sub {
setting show_errors => 1; setting show_errors => 1;
Expand Down
2 changes: 1 addition & 1 deletion t/lib/TestSerializer.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Dancer;
setting serializer => 'JSON'; setting serializer => 'JSON';


get '/json' => sub { get '/json' => sub {
to_json({foo => 'bar'}, pretty => 1); to_json({foo => 'bar'}, { pretty => 1});
}; };


get '/' => sub { get '/' => sub {
Expand Down

0 comments on commit 0466b40

Please sign in to comment.