| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
use Plack::Test; |
| 6 |
use HTTP::Request; |
| 7 |
use HTTP::Request::Common; |
| 8 |
use JSON::MaybeXS (); |
| 9 |
use Path::Tiny (); |
| 10 |
|
| 11 |
# Serializer round trip and failure handling, through a real request. |
| 12 |
# |
| 13 |
# The two halves are asymmetric and both matter: |
| 14 |
# |
| 15 |
# out - a route's return value is serialized, and the response must carry |
| 16 |
# the serializer's own content type rather than the text/html default. |
| 17 |
# in - a request body is deserialized, but only for the methods that are |
| 18 |
# supposed to have one, and never for a multipart upload. |
| 19 |
# |
| 20 |
# A malformed body has to fail as a 400: the client sent something wrong, so |
| 21 |
# there is no point telling it to try again. |
| 22 |
|
| 23 |
my $UPLOAD; |
| 24 |
BEGIN { |
| 25 |
$UPLOAD = Path::Tiny->tempdir; |
| 26 |
$UPLOAD->child('up.txt')->spew_raw('FILE-CONTENT'); |
| 27 |
} |
| 28 |
|
| 29 |
{ |
| 30 |
package JsonApp; |
| 31 |
use Dancer2; |
| 32 |
set logger => 'null'; |
| 33 |
set serializer => 'JSON'; |
| 34 |
|
| 35 |
# Echoes what the deserializer produced, so the tests can tell "no body |
| 36 |
# was deserialized" from "the body deserialized to nothing". |
| 37 |
sub _report { |
| 38 |
return { |
| 39 |
method => request->method, |
| 40 |
data => ( defined request->data ? request->data : 'NO-DATA' ), |
| 41 |
body => body_parameters->as_hashref, |
| 42 |
}; |
| 43 |
} |
| 44 |
|
| 45 |
get '/echo' => sub { _report() }; |
| 46 |
post '/echo' => sub { _report() }; |
| 47 |
put '/echo' => sub { _report() }; |
| 48 |
patch '/echo' => sub { _report() }; |
| 49 |
del '/echo' => sub { _report() }; |
| 50 |
|
| 51 |
get '/hash' => sub { { a => 1 } }; |
| 52 |
get '/array' => sub { [ 1, 2, 3 ] }; |
| 53 |
get '/string' => sub { 'plain string' }; |
| 54 |
get '/utf8' => sub { { m => "caf\x{e9}\x{263A}" } }; |
| 55 |
get '/empty' => sub { '' }; |
| 56 |
get '/undef' => sub { undef }; |
| 57 |
|
| 58 |
post '/upload' => sub { |
| 59 |
return { |
| 60 |
files => [ sort keys %{ request->uploads } ], |
| 61 |
data => ( defined request->data ? 'DESERIALIZED' : 'NO-DATA' ), |
| 62 |
name => ( body_parameters->get('name') // 'undef' ), |
| 63 |
}; |
| 64 |
}; |
| 65 |
} |
| 66 |
|
| 67 |
my $test = Plack::Test->create( JsonApp->to_app ); |
| 68 |
|
| 69 |
sub json_body { |
| 70 |
my $response = shift; |
| 71 |
return JSON::MaybeXS::decode_json( $response->content ); |
| 72 |
} |
| 73 |
|
| 74 |
# A request with an explicit method, headers and body - HTTP::Request::Common |
| 75 |
# will not give a GET a body, and a GET with a body is one of the cases here. |
| 76 |
sub raw_request { |
| 77 |
my ( $method, $path, $type, $body ) = @_; |
| 78 |
return HTTP::Request->new( |
| 79 |
$method, $path, |
| 80 |
[ defined $type ? ( 'Content-Type' => $type ) : () ], |
| 81 |
$body, |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
subtest 'a serialized response carries the serializer content type' => sub { |
| 86 |
for my $case ( |
| 87 |
[ '/hash', { a => 1 } ], |
| 88 |
[ '/array', [ 1, 2, 3 ] ], |
| 89 |
) { |
| 90 |
my ( $path, $expected ) = @$case; |
| 91 |
my $response = $test->request( GET $path ); |
| 92 |
|
| 93 |
is( $response->code, 200, "$path: responds" ); |
| 94 |
is( $response->header('Content-Type'), 'application/json', |
| 95 |
"$path: content type is the serializer's, not text/html" ); |
| 96 |
is_deeply( json_body($response), $expected, |
| 97 |
"$path: round-trips through JSON" ); |
| 98 |
} |
| 99 |
|
| 100 |
# A route returning a plain, non-reference string still reaches |
| 101 |
# Response->content (Dancer2/Core/Route.pm:174-178: blessed($content) is |
| 102 |
# false for a plain string just as it is for an unblessed hash/arrayref, |
| 103 |
# so it goes to App::_prep_response, which does $response->content($content) |
| 104 |
# like any other value). content()'s 'around' modifier serializes |
| 105 |
# whenever a serializer is configured, with no ref-ness check |
| 106 |
# (Dancer2/Core/Response.pm:141-171). |
| 107 |
# |
| 108 |
# What happens next depends on the JSON module underneath JSON::MaybeXS, |
| 109 |
# because Dancer2::Serializer::JSON::serialize does not pass allow_nonref: |
| 110 |
# |
| 111 |
# refuses a non-ref encode() dies ("hash- or arrayref expected"). The |
| 112 |
# die is caught and logged rather than propagated (the |
| 113 |
# 'around serialize' in |
| 114 |
# Dancer2::Core::Role::Serializer), so |
| 115 |
# Response::serialize's own "or return" bails out |
| 116 |
# before ever setting the Content-Type |
| 117 |
# (Dancer2/Core/Response.pm:295-306). The string is |
| 118 |
# silently swallowed: the default text/html content |
| 119 |
# type, never overwritten, and an empty body. |
| 120 |
# |
| 121 |
# encodes a non-ref an ordinary serialized response: application/json, |
| 122 |
# with '"plain string"' as the body. |
| 123 |
# |
| 124 |
# This is not just a question of which backend is installed: it varies by |
| 125 |
# version within one backend. Cpanel::JSON::XS 4.37 refuses a top-level |
| 126 |
# non-reference, while 4.43 encodes it -- RFC 8259 permits any value at the |
| 127 |
# top level, unlike the older RFC 4627 that required an object or array. |
| 128 |
# So naming backends here would be wrong twice over, and the capability is |
| 129 |
# probed instead: whatever is installed, the test asserts what Dancer2 |
| 130 |
# actually does with it. Neither outcome is pinned as the correct one. |
| 131 |
my $backend_encodes_nonref = |
| 132 |
eval { JSON::MaybeXS->new( { utf8 => 1 } )->encode('probe'); 1 } ? 1 : 0; |
| 133 |
|
| 134 |
note( "JSON backend is $JSON::MaybeXS::JSON_Class, which ", |
| 135 |
( $backend_encodes_nonref ? 'encodes' : 'refuses' ), |
| 136 |
' a top-level non-reference' ); |
| 137 |
|
| 138 |
my $string = $test->request( GET '/string' ); |
| 139 |
is( $string->code, 200, '/string: responds' ); |
| 140 |
|
| 141 |
if ($backend_encodes_nonref) { |
| 142 |
is( $string->header('Content-Type'), 'application/json', |
| 143 |
'/string: a backend that encodes a non-ref gives the serializer content type' ); |
| 144 |
is( $string->content, '"plain string"', |
| 145 |
'/string: and the plain string is JSON-encoded into the body' ); |
| 146 |
} |
| 147 |
else { |
| 148 |
is( $string->header('Content-Type'), 'text/html', |
| 149 |
'/string: a backend that refuses a non-ref leaves the default content type' ); |
| 150 |
is( $string->content, '', |
| 151 |
'/string: and the serializer silently swallows the value, leaving no body' ); |
| 152 |
} |
| 153 |
|
| 154 |
# Characters must arrive as UTF-8 bytes, not as wide characters and not |
| 155 |
# as escaped ASCII. |
| 156 |
my $utf8 = $test->request( GET '/utf8' ); |
| 157 |
is( $utf8->header('Content-Type'), 'application/json', |
| 158 |
'/utf8: content type is the serializer\'s' ); |
| 159 |
is( $utf8->content, qq[{"m":"caf\xc3\xa9\xe2\x98\xba"}], |
| 160 |
'/utf8: the body is UTF-8 encoded bytes' ); |
| 161 |
is_deeply( json_body($utf8), { m => "caf\x{e9}\x{263A}" }, |
| 162 |
'/utf8: and decodes back to the original characters' ); |
| 163 |
}; |
| 164 |
|
| 165 |
subtest 'an empty serialized body loses the serializer content type' => sub { |
| 166 |
|
| 167 |
# Pinned as current behavior, not endorsed. Response::serialize bails out |
| 168 |
# before setting the content type when serialization produces something |
| 169 |
# falsey (Dancer2/Core/Response.pm:301), so a route returning '' or undef |
| 170 |
# under a JSON serializer answers text/html. A JSON client gets a content |
| 171 |
# type it did not ask for. |
| 172 |
for my $path ( '/empty', '/undef' ) { |
| 173 |
my $response = $test->request( GET $path ); |
| 174 |
is( $response->code, 200, "$path: still a 200" ); |
| 175 |
is( $response->content, '', "$path: with an empty body" ); |
| 176 |
is( $response->header('Content-Type'), 'text/html', |
| 177 |
"$path: but the content type falls back to text/html" ); |
| 178 |
} |
| 179 |
}; |
| 180 |
|
| 181 |
subtest 'a malformed body is a 400, not a 500' => sub { |
| 182 |
my $response = $test->request( |
| 183 |
raw_request( POST => '/echo', 'application/json', '{"a":' ) ); |
| 184 |
|
| 185 |
# 400, because the client sent bad input - re-sending will not help. |
| 186 |
is( $response->code, 400, 'a malformed JSON body is refused as 400' ); |
| 187 |
|
| 188 |
# The error itself is serialized, so a JSON client can read the failure. |
| 189 |
is( $response->header('Content-Type'), 'application/json', |
| 190 |
'and the error is reported in the serializer\'s content type' ); |
| 191 |
my $error = json_body($response); |
| 192 |
like( $error->{message}, qr/deserialize/i, |
| 193 |
'the message says deserialization is what failed' ); |
| 194 |
|
| 195 |
# A valid body on the same route is the control. |
| 196 |
my $ok = $test->request( |
| 197 |
raw_request( POST => '/echo', 'application/json', '{"a":1}' ) ); |
| 198 |
is( $ok->code, 200, 'a valid body on the same route is accepted' ); |
| 199 |
is_deeply( json_body($ok)->{data}, { a => 1 }, |
| 200 |
'and reaches the route deserialized' ); |
| 201 |
}; |
| 202 |
|
| 203 |
subtest 'an empty body is not a failure' => sub { |
| 204 |
my $response = $test->request( |
| 205 |
raw_request( POST => '/echo', 'application/json', '' ) ); |
| 206 |
|
| 207 |
is( $response->code, 200, 'an empty body is not treated as malformed' ); |
| 208 |
is( json_body($response)->{data}, 'NO-DATA', |
| 209 |
'there is simply nothing to deserialize' ); |
| 210 |
}; |
| 211 |
|
| 212 |
subtest 'only methods that may carry a body are deserialized' => sub { |
| 213 |
# These four are deserialized. DELETE is included deliberately: the RFC |
| 214 |
# leaves it undefined and Dancer2 takes the lenient route |
| 215 |
# (Dancer2/Core/Request.pm:202-206). |
| 216 |
for my $method (qw< POST PUT PATCH DELETE >) { |
| 217 |
my $response = $test->request( |
| 218 |
raw_request( $method => '/echo', 'application/json', '{"a":1}' ) ); |
| 219 |
is( $response->code, 200, "$method: accepted" ); |
| 220 |
is_deeply( json_body($response)->{data}, { a => 1 }, |
| 221 |
"$method: body is deserialized" ); |
| 222 |
} |
| 223 |
|
| 224 |
# A GET body is ignored entirely. |
| 225 |
my $get = $test->request( |
| 226 |
raw_request( GET => '/echo', 'application/json', '{"a":1}' ) ); |
| 227 |
is( $get->code, 200, 'GET: accepted' ); |
| 228 |
is( json_body($get)->{data}, 'NO-DATA', |
| 229 |
'GET: the body is not deserialized even when it is valid' ); |
| 230 |
|
| 231 |
# The sharpest version: a GET carrying a *malformed* body must still be a |
| 232 |
# 200, which proves the deserializer was never reached rather than that it |
| 233 |
# ran and happened to succeed. |
| 234 |
my $bad_get = $test->request( |
| 235 |
raw_request( GET => '/echo', 'application/json', '{"a":' ) ); |
| 236 |
is( $bad_get->code, 200, |
| 237 |
'GET: a malformed body does not even produce a 400' ); |
| 238 |
is( json_body($bad_get)->{data}, 'NO-DATA', |
| 239 |
'GET: because nothing tried to deserialize it' ); |
| 240 |
}; |
| 241 |
|
| 242 |
subtest 'a multipart upload is not run through the deserializer' => sub { |
| 243 |
my $response = $test->request( |
| 244 |
POST '/upload', |
| 245 |
Content_Type => 'form-data', |
| 246 |
Content => [ |
| 247 |
name => 'ovid', |
| 248 |
file => [ $UPLOAD->child('up.txt')->stringify ], |
| 249 |
], |
| 250 |
); |
| 251 |
|
| 252 |
is( $response->code, 200, 'the upload is accepted' ); |
| 253 |
my $got = json_body($response); |
| 254 |
|
| 255 |
# The deserializer is skipped for multipart/form-data |
| 256 |
# (Dancer2/Core/Request.pm:191-196) - otherwise it would be handed the |
| 257 |
# MIME envelope and fail the request. |
| 258 |
is( $got->{data}, 'NO-DATA', 'the body was not deserialized' ); |
| 259 |
|
| 260 |
# ...but the upload and the ordinary form field are still parsed. |
| 261 |
is_deeply( $got->{files}, ['file'], 'the uploaded file is still available' ); |
| 262 |
is( $got->{name}, 'ovid', 'and so is a plain form field alongside it' ); |
| 263 |
}; |
| 264 |
|
| 265 |
done_testing(); |