1use strict;
2use warnings;
3
4use Test::More tests => 1;
5use Plack::Test;
6use Plack::Builder;
7use Plack::Request;
8use HTTP::Request::Common;
9use Encode qw(encode_utf8);
10
11{
12    package App;
13    use Dancer2;
14
15    # default, we're actually overriding this later
16    set serializer => 'JSON';
17
18    # for now
19    set logger     => 'Capture';
20
21    post '/json' => sub {
22        my $p = body_parameters;
23        return [ map +( $_ => $p->get($_) ), sort $p->keys ];
24    };
25}
26
27my $psgi = builder {
28    # inline middleware FTW!
29    # Create a Plack::Request object and parse body to tickle #1232
30    enable sub {
31        my $app = shift;
32        sub {
33            my $req = Plack::Request->new($_[0])->body_parameters;
34            return $app->($_[0]);
35        }
36    };
37    App->to_app;
38};
39
40my $test = Plack::Test->create( $psgi );
41
42subtest 'POST request with parameters' => sub {
43    my $characters = encode_utf8("∑∏");
44
45    my $res = $test->request(
46        POST "/json",
47            'Content-Type' => 'application/json',
48            'Content'      => qq!{ "foo": 1, "bar": 2, "baz": "$characters" }!
49    );
50
51    is(
52        $res->content,
53        qq!["bar",2,"baz","$characters","foo",1]!,
54        "Body parameters deserialized",
55    );
56};
57
58done_testing();