1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
use strict;
use warnings;
use Test::More tests => 12;
use Plack::Test;
use HTTP::Request::Common;
{
package MyApp;
use Dancer2;
set serializer => 'JSON';
put '/from_params' => sub {
my %p = params();
return join " : ", map { $_ => $p{$_} } sort keys %p;
};
put '/from_data' => sub {
my $p = request->data;
return join " : ", map { $_ => $p->{$_} } sort keys %$p;
};
# This route is used for both toure and body params.
post '/from/:town' => sub {
my $p = params;
return $p;
};
any [qw/del patch/] => '/from/:town' => sub {
my $p = params('body');
return $p;
};
}
my $app = MyApp->to_app;
is( ref $app, 'CODE', 'Got app' );
test_psgi $app, sub {
my $cb = shift;
foreach my $type ( qw<params data> ) {
is(
$cb->(
PUT '/from_params',
'Content-Type' => 'application/json',
Content => '{ "foo": 1, "bar": 2 }'
)->content,
'bar : 2 : foo : 1',
"Using $type",
)
}
};
use utf8;
use JSON;
use Encode;
use Class::Load 'load_class';
note "Verify Serializers decode into characters"; {
my $utf8 = '∮ E⋅da = Q, n → ∞, ∑ f(i) = ∏ g(i)';
test_psgi $app, sub {
my $cb = shift;
for my $type ( qw/Dumper JSON YAML/ ) {
my $class = "Dancer2::Serializer::$type";
load_class($class);
my $serializer = $class->new();
my $body = $serializer->serialize({utf8 => $utf8});
# change the app serializer
# we're overiding a RO attribute only for this test!
Dancer2->runner->apps->[0]->set_serializer_engine(
$serializer
);
my $r = $cb->(
PUT '/from_params',
'Content-Type' => $serializer->content_type,
Content => $body,
);
my $content = Encode::decode( 'UTF-8', $r->content );
is(
$content,
"utf8 : $utf8",
"utf-8 string returns the same using the $type serializer",
);
}
};
}
# default back to JSON for the rest
# we're overiding a RO attribute only for this test!
Dancer2->runner->apps->[0]->set_serializer_engine(
Dancer2::Serializer::JSON->new
);
note "Decoding of mixed route and deserialized body params"; {
# Check integers from request body remain integers
# but route params get decoded.
test_psgi $app, sub {
my $cb = shift;
my @req_params = (
"/from/D\x{c3}\x{bc}sseldorf", # /from/d%C3%BCsseldorf
'Content-Type' => 'application/json',
Content => JSON::to_json({ population => 592393 }),
);
my $r = $cb->( POST @req_params );
my $content = Encode::decode( 'UTF-8', $r->content );
# Watch out for hash order randomization..
like(
$content,
qr/[{,]"population":592393/,
"Integer from JSON body remains integer",
);
like(
$content,
qr/[{,]"town":"Düsseldorf"/,
"Route params are decoded",
);
};
}
# Check body is deserialized on PATCH and DELETE.
# The RFC states the behaviour for DELETE is undefined; We take the lenient
# and deserialize it.
# http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-24#section-4.3.5
note "Deserialze any body content that is allowed or undefined"; {
test_psgi $app, sub {
my $cb = shift;
for my $method ( qw/delete patch/ ) {
my $request = HTTP::Request->new(
$method,
"/from/D\x{c3}\x{bc}sseldorf", # /from/d%C3%BCsseldorf
[ 'Content-Type' => 'application/json' ],
JSON::to_json({ population => 592393 }),
);
my $response = $cb->($request);
my $content = Encode::decode( 'UTF-8', $response->content );
# Only body params returned
is(
$content,
'{"population":592393}',
"JSON body deserialized for " . uc($method) . " requests",
);
}
}
}
note 'Check serialization errors'; {
test_psgi $app, sub {
my $cb = shift;
$cb->(
PUT '/from_params',
'Content-Type' => 'application/json',
Content => '---',
);
ok(
Dancer2->runner->apps->[0]->serializer_engine->has_error,
"Invalid JSON threw error in serializer",
);
like(
Dancer2->runner->apps->[0]->serializer_engine->error,
qr/malformed number/,
".. of a 'malformed number'",
);
}
}
done_testing();
|