| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More import => ['!pass']; |
| 5 |
use Plack::Test; |
| 6 |
use HTTP::Request::Common; |
| 7 |
|
| 8 |
{ |
| 9 |
package DummyObj; |
| 10 |
use Moo; |
| 11 |
has foo => (is => 'ro', default => 'bar'); |
| 12 |
sub TO_JSON { |
| 13 |
{foo => shift->foo}; |
| 14 |
} |
| 15 |
1; |
| 16 |
} |
| 17 |
|
| 18 |
{ |
| 19 |
package Test::App::SendAs; |
| 20 |
use Dancer2; |
| 21 |
|
| 22 |
set engines => { |
| 23 |
serializer => { |
| 24 |
JSON => { |
| 25 |
allow_blessed => 1, |
| 26 |
convert_blessed => 1, |
| 27 |
} |
| 28 |
} |
| 29 |
}; |
| 30 |
set logger => 'Capture'; |
| 31 |
set serializer => 'YAML'; |
| 32 |
set template => 'TemplateToolkit'; |
| 33 |
|
| 34 |
get '/html' => sub { |
| 35 |
send_as html => '<html></html>' |
| 36 |
}; |
| 37 |
|
| 38 |
get '/plain' => sub { |
| 39 |
send_as plain => 'some plain text with <html></html>'; |
| 40 |
}; |
| 41 |
|
| 42 |
get '/json/**' => sub { |
| 43 |
send_as JSON => splat; |
| 44 |
}; |
| 45 |
|
| 46 |
get '/json-object' => sub { |
| 47 |
send_as JSON => { data => DummyObj->new() }; |
| 48 |
}; |
| 49 |
|
| 50 |
get '/json-utf8/**' => sub { |
| 51 |
send_as JSON => splat, { content_type => 'application/json', charset => 'utf-8' }; |
| 52 |
}; |
| 53 |
|
| 54 |
get '/yaml/**' => sub { |
| 55 |
my @params = splat; |
| 56 |
\@params; |
| 57 |
}; |
| 58 |
|
| 59 |
get '/sendas/:type?' => sub { |
| 60 |
send_as route_parameters->{'type'} => 'test string'; |
| 61 |
}; |
| 62 |
} |
| 63 |
|
| 64 |
my $test = Plack::Test->create( Test::App::SendAs->to_app ); |
| 65 |
|
| 66 |
subtest "default serializer" => sub { |
| 67 |
my $res = $test->request( GET '/yaml/is/useful' ); |
| 68 |
is $res->code, '200'; |
| 69 |
is $res->content_type, 'text/x-yaml'; |
| 70 |
|
| 71 |
my $expected = <<'YAML'; |
| 72 |
--- |
| 73 |
- |
| 74 |
- is |
| 75 |
- useful |
| 76 |
YAML |
| 77 |
|
| 78 |
is $res->content, $expected; |
| 79 |
|
| 80 |
}; |
| 81 |
|
| 82 |
subtest "send_as json" => sub { |
| 83 |
my $res = $test->request( GET '/json/is/wonderful' ); |
| 84 |
is $res->code, '200'; |
| 85 |
is $res->content_type, 'application/json'; |
| 86 |
|
| 87 |
is $res->content, '["is","wonderful"]'; |
| 88 |
}; |
| 89 |
|
| 90 |
subtest "send_as json object" => sub { |
| 91 |
my $res = $test->request( GET '/json-object' ); |
| 92 |
is $res->code, '200'; |
| 93 |
is $res->content_type, 'application/json'; |
| 94 |
|
| 95 |
is $res->content, '{"data":{"foo":"bar"}}'; |
| 96 |
}; |
| 97 |
|
| 98 |
subtest "send_as json custom content-type" => sub { |
| 99 |
my $res = $test->request( GET '/json-utf8/is/wonderful' ); |
| 100 |
is $res->code, '200'; |
| 101 |
is $res->content_type, 'application/json'; |
| 102 |
is $res->content_type_charset, 'UTF-8'; |
| 103 |
|
| 104 |
is $res->content, '["is","wonderful"]'; |
| 105 |
}; |
| 106 |
|
| 107 |
subtest "send_as html" => sub { |
| 108 |
my $res = $test->request( GET '/html' ); |
| 109 |
is $res->code, '200'; |
| 110 |
is $res->content_type, 'text/html'; |
| 111 |
is $res->content_type_charset, 'UTF-8'; |
| 112 |
|
| 113 |
is $res->content, '<html></html>'; |
| 114 |
}; |
| 115 |
|
| 116 |
subtest "send_as plain" => sub { |
| 117 |
my $res = $test->request( GET '/plain' ); |
| 118 |
is $res->code, '200'; |
| 119 |
is $res->content_type, 'text/plain'; |
| 120 |
is $res->content_type_charset, 'UTF-8'; |
| 121 |
|
| 122 |
is $res->content, 'some plain text with <html></html>'; |
| 123 |
}; |
| 124 |
|
| 125 |
subtest "send_as error cases" => sub { |
| 126 |
my $logger = Test::App::SendAs::app->logger_engine; |
| 127 |
|
| 128 |
{ |
| 129 |
my $res = $test->request( GET '/sendas/' ); |
| 130 |
is $res->code, '500', "send_as dies with no defined type"; |
| 131 |
|
| 132 |
my $logs = $logger->trapper->read; |
| 133 |
like $logs->[0]->{message}, |
| 134 |
qr!Route exception: Can not send_as using an undefined type!, |
| 135 |
".. throws route exception"; |
| 136 |
} |
| 137 |
|
| 138 |
{ |
| 139 |
local $SIG{__WARN__} = sub { |
| 140 |
return if $_[0] =~ /Subroutine .* redefined/; |
| 141 |
CORE::warn @_; |
| 142 |
}; |
| 143 |
|
| 144 |
my $res = $test->request( GET '/sendas/jSoN' ); |
| 145 |
is $res->code, '500', |
| 146 |
"send_as dies with incorrectly cased serializer name"; |
| 147 |
|
| 148 |
my $logs = $logger->trapper->read; |
| 149 |
like $logs->[0]->{message}, |
| 150 |
qr!Route exception: Unable to load serializer class for jSoN!, |
| 151 |
".. throws route exception"; |
| 152 |
} |
| 153 |
|
| 154 |
{ |
| 155 |
my $res = $test->request( GET '/sendas/SomeSerializerThatDoesNotExist' ); |
| 156 |
is $res->code, '500', |
| 157 |
"send_as dies when called with non-existant serializer"; |
| 158 |
|
| 159 |
my $logs = $logger->trapper->read; |
| 160 |
like $logs->[0]->{message}, |
| 161 |
qr!Route exception: Unable to load serializer class for SomeSerializerThatDoesNotExist!, |
| 162 |
".. throws route exception"; |
| 163 |
} |
| 164 |
}; |
| 165 |
|
| 166 |
done_testing(); |