#!/usr/bin/env perl

package MyApp;

use strict;
use warnings;
use Dancer2;
use Data::Dumper;

set serializer => 'JSON';

any '/ok' => sub {
    return {};
};

any '/nok' => sub {
    my $a = request->content;
    #print STDERR Dumper $a;
    #print STDERR Dumper { params('body') };
    return {};
};

# dance();


package main;

use strict;
use warnings;
use Test::More;
use Plack::Test;
use HTTP::Request::Common;
use JSON;

my $app = MyApp->to_app;
isa_ok( $app, 'CODE' );

test_psgi $app, sub {
    my $cb = shift;
    
    my $res = $cb->( GET '/ok' );
    ok($res->code == 200, "GET route without request->content call");

    $res = $cb->( GET '/nok' );
    ok($res->code == 200, "GET route with request->content call");

    my $json_data = { foo => 'bar' };
    $res = $cb->( 
        POST '/ok', 
        'Content-Type'    => 'application/json',
        Content => JSON::to_json($json_data),
    );
    ok($res->code == 200, "JSON POST to route without request->content call");

    $res = $cb->( 
        POST '/nok', 
        'Content-Type'    => 'application/json',
        Content => JSON::to_json($json_data),
    );
    # this test fails.
    ok($res->code == 200, "JSON POST to route with request->content call");

};

done_testing;


1;
