| 1 |
=pod |
| 2 |
|
| 3 |
Same as t/serializer_mutable.t, but exercise the configurable |
| 4 |
mappings |
| 5 |
|
| 6 |
=cut |
| 7 |
|
| 8 |
use strict; |
| 9 |
use warnings; |
| 10 |
|
| 11 |
use Test::More tests => 6; |
| 12 |
use Dancer2::Serializer::Mutable; |
| 13 |
use Plack::Test; |
| 14 |
use HTTP::Request::Common; |
| 15 |
use Encode; |
| 16 |
use JSON::MaybeXS; |
| 17 |
use YAML; |
| 18 |
use Ref::Util qw<is_coderef>; |
| 19 |
use Test::Fatal; |
| 20 |
|
| 21 |
{ |
| 22 |
package Dancer2::Serializer::Other; |
| 23 |
|
| 24 |
use Moo; |
| 25 |
with 'Dancer2::Core::Role::Serializer'; |
| 26 |
|
| 27 |
has '+content_type' => ( default => 'text/other' ); |
| 28 |
|
| 29 |
sub serialize { '{thing}' } |
| 30 |
sub deserialize { '{thing}' } |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
{ |
| 35 |
package MyApp; |
| 36 |
use Dancer2; |
| 37 |
|
| 38 |
BEGIN { |
| 39 |
setting engines => { serializer => { Mutable => { mapping => { |
| 40 |
'text/x-yaml' => 'YAML', |
| 41 |
'text/x-json' => 'JSON', |
| 42 |
'application/json' => 'JSON', |
| 43 |
'text/other' => 'Other', |
| 44 |
} } } }; |
| 45 |
} |
| 46 |
|
| 47 |
use Ref::Util qw<is_hashref>; |
| 48 |
|
| 49 |
set serializer => 'Mutable'; |
| 50 |
|
| 51 |
get '/serialize' => sub { +{ bar => 'baz' } }; |
| 52 |
post '/deserialize' => sub { |
| 53 |
return request->data && |
| 54 |
is_hashref( request->data ) && |
| 55 |
request->data->{bar} ? { bar => request->data->{bar} } : { ret => '?' }; |
| 56 |
}; |
| 57 |
} |
| 58 |
|
| 59 |
my $app = MyApp->to_app; |
| 60 |
ok is_coderef($app), 'Got app'; |
| 61 |
|
| 62 |
test_psgi $app, sub { |
| 63 |
my $cb = shift; |
| 64 |
|
| 65 |
# Configure all test cases |
| 66 |
my $d = { |
| 67 |
yaml => { |
| 68 |
types => [ qw(text/x-yaml) ], |
| 69 |
value => encode('UTF-8', YAML::Dump({ bar => 'baz' })), |
| 70 |
last_val => "---bar:baz", |
| 71 |
}, |
| 72 |
other => { |
| 73 |
types => [ qw(text/other) ], |
| 74 |
value => '{thing}', |
| 75 |
last_val => "{thing}", |
| 76 |
}, |
| 77 |
json => { |
| 78 |
types => [ qw(text/x-json application/json) ], |
| 79 |
value => JSON::MaybeXS::encode_json({ bar => 'baz' }), |
| 80 |
last_val => '{"bar":"baz"}', |
| 81 |
}, |
| 82 |
}; |
| 83 |
|
| 84 |
for my $format (keys %$d) { |
| 85 |
subtest "Format: $format" => sub { |
| 86 |
|
| 87 |
my $s = $d->{$format}; |
| 88 |
|
| 89 |
# Response with implicit call to the serializer |
| 90 |
for my $content_type ( @{ $s->{types} } ) { |
| 91 |
subtest $content_type => sub { |
| 92 |
for my $ct (qw/Content-Type Accept/) { |
| 93 |
|
| 94 |
# Test getting the value serialized in the correct format |
| 95 |
my $res = $cb->( GET '/serialize', $ct => $content_type ); |
| 96 |
|
| 97 |
is( $res->code, 200, "status" ); |
| 98 |
is( $res->content, $s->{value}, "content" ); |
| 99 |
is( |
| 100 |
$res->headers->content_type, |
| 101 |
$content_type, |
| 102 |
"content-type headers", |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
# Test sending the value serialized in the correct format |
| 107 |
# needs to be de-serialized and returned |
| 108 |
my $req = $cb->( POST '/deserialize', |
| 109 |
'Content-Type' => $content_type, |
| 110 |
content => $s->{value} ); |
| 111 |
|
| 112 |
my $content = $req->content; |
| 113 |
$content =~ s/\s//g; |
| 114 |
is( $req->code, 200, "status" ); |
| 115 |
is( $content, $s->{last_val}, "content" ); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
}; |
| 122 |
|
| 123 |
{ |
| 124 |
my $mutable = Dancer2::Serializer::Mutable->new( |
| 125 |
config => { mapping => { 'text/x-data-dumper' => 'Dumper' } }, |
| 126 |
); |
| 127 |
|
| 128 |
like( |
| 129 |
exception { $mutable->mapping }, |
| 130 |
qr/enable_dumper/, |
| 131 |
'Mapping Dumper without enable_dumper croaks', |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
SKIP: { |
| 136 |
eval { require Dancer2::Serializer::Dumper; 1 } |
| 137 |
or skip 'Dancer2::Serializer::Dumper is not installed', 1; |
| 138 |
|
| 139 |
my $mutable = Dancer2::Serializer::Mutable->new( enable_dumper => 1 ); |
| 140 |
my $mapping = $mutable->mapping; |
| 141 |
|
| 142 |
is( |
| 143 |
$mapping->{'text/x-data-dumper'}, |
| 144 |
'Dumper', |
| 145 |
'enable_dumper adds text/x-data-dumper to the default mapping', |
| 146 |
); |
| 147 |
} |