Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/Dancer2/Core/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ sub deserialize {
return;
}

# don't attempt to deserialize if the form is 'application/x-www-form-urlencoded'
if (
$self->content_type
&& $self->content_type =~ /^application\/x-www-form-urlencoded/i
&& $self->body
&& $self->body !~ /^\s*[[{]/ # Not JSON
&& $self->body =~ /=/ # has an '='
) {
return;
}

my $serializer = $self->serializer
or return;
Expand Down
37 changes: 37 additions & 0 deletions t/classes/Dancer2-Core-Request/serializers.t
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,41 @@ subtest 'Testing with JSON' => sub {
);
};

{
package App::UrlEncoded;
use Dancer2;

# Due to the random hash order, the response will randomly fail the test.
# Turn on canonical to mitigate the problem.
set engines => {
serializer => {
JSON => {
canonical => 1
}
}
};

set serializer => 'JSON';

post '/' => sub {
my %body_parameters = body_parameters->flatten;
::is_deeply(\%body_parameters, { foo => 'bar', bar => '' }, 'Correct body parameters');
return \%body_parameters;
};
}

subtest 'Testing with Form-UrlEncoded Data' => sub {
my $app = Plack::Test->create( App::UrlEncoded->to_app );
my $res = $app->request(
POST '/',
Content_Type => 'application/x-www-form-urlencoded',
Content => 'foo=bar&bar='
);
is(
$res->content,
'{"bar":"","foo":"bar"}',
"Successful response"
);
};

done_testing();