diff --git a/t/14_serializer/12_bug_gh106.t b/t/14_serializer/12_bug_gh106.t new file mode 100644 index 000000000..0e4b417f1 --- /dev/null +++ b/t/14_serializer/12_bug_gh106.t @@ -0,0 +1,63 @@ +use strict; +use warnings; +use Test::More import => ['!pass']; + +BEGIN { + use Dancer::ModuleLoader; + + plan skip_all => "LWP::UserAgent is needed to run this tests" + unless Dancer::ModuleLoader->load('LWP::UserAgent'); + plan skip_all => 'Test::TCP is needed to run this test' + unless Dancer::ModuleLoader->load('Test::TCP'); + plan skip_all => 'JSON is needed to run this test' + unless Dancer::ModuleLoader->load('JSON'); + plan skip_all => 'HTTP::Request is needed to run this test' + unless Dancer::ModuleLoader->load('HTTP::Request'); +} + +plan tests => 6; + +use Dancer; + +Test::TCP::test_tcp( + client => sub { + my $port = shift; + my $url = "http://127.0.0.1:$port/"; + my $req = HTTP::Request->new( GET => $url ); + $req->header( 'Content-Type' => 'application/json' ); + my $ua = LWP::UserAgent->new; + ok my $res = $ua->request($req); + is $res->code, 200; + is_deeply(JSON::decode_json($res->content), {foo => 1}); + }, + server => sub { + my $port = shift; + load_app 't::lib::TestSerializer'; + setting access_log => 0; + setting port => $port; + Dancer->dance(); + } +); + +Test::TCP::test_tcp( + client => sub { + my $port = shift; + my $url = "http://127.0.0.1:$port/blessed"; + my $req = HTTP::Request->new( GET => $url ); + $req->header( 'Content-Type' => 'application/json' ); + my $ua = LWP::UserAgent->new; + ok my $res = $ua->request($req); + is $res->code, 200; + is_deeply( JSON::decode_json( $res->content ), { request => undef } ); + }, + server => sub { + my $port = shift; + load_app 't::lib::TestSerializer'; + setting access_log => 0; + setting port => $port; + setting engines => + { JSON => { allow_blessed => 1, convert_blessed => 1 } }; + Dancer->dance(); + } +); + diff --git a/t/lib/TestSerializer.pm b/t/lib/TestSerializer.pm index 9df8db64e..efafc0917 100644 --- a/t/lib/TestSerializer.pm +++ b/t/lib/TestSerializer.pm @@ -2,14 +2,20 @@ package t::lib::TestSerializer; use Dancer; -set serializer => 'JSON'; +setting serializer => 'JSON'; get '/' => sub { { foo => 1 } }; +get '/blessed' => sub { + require HTTP::Headers; + my $r = HTTP::Request->new(GET => 'http://localhost'); + {request => $r}; +}; + post '/' => sub { - request->params; + request->params; }; get '/error' => sub {