Debian Codegraph @

t/serializer_json.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Plack::Test;
6 use HTTP::Request::Common;
7 use JSON::MaybeXS;
8
9 use Dancer2::Serializer::JSON;
10
11 # config
12 {
13 package MyApp;
14
15 use Dancer2;
16 our $entity;
17
18 set engines => {
19 serializer => {
20 JSON => {
21 canonical => 1,
22 pretty => 1,
23 }
24 }
25 };
26 set serializer => 'JSON';
27
28 get '/serialize' => sub {
29 return $entity;
30 };
31 }
32
33 my @tests = (
34 { entity => { a => 1, b => 2, },
35 options => { canonical => 1, pretty => 1 },
36 name => "basic hash",
37 },
38 { entity =>
39 { c => [ { d => 3, e => { f => 4, g => 'word', } } ], h => 6 },
40 options => { canonical => 1, pretty => 1 },
41 name => "nested",
42 },
43 { entity => { data => "\x{2620}" x 10 },
44 options => { canonical => 1, pretty => 1, utf8 => 1 },
45 name => "utf8",
46 }
47 );
48
49 my $app = MyApp->to_app;
50
51 for my $test (@tests) {
52 my $expected = JSON::MaybeXS->new($test->{options})->encode($test->{entity});
53
54 # Helpers pass options
55 my $actual =
56 Dancer2::Serializer::JSON::to_json( $test->{entity}, $test->{options} );
57 is( $actual, $expected, "to_json: $test->{name}" );
58
59 # Options from config
60 my $serializer = Dancer2::Serializer::JSON->new(config => $test->{options});
61 my $output = $serializer->serialize( $test->{entity} );
62 is( $output, $expected, "serialize: $test->{name}" );
63
64 $MyApp::entity = $test->{entity};
65 test_psgi $app, sub {
66 my $cb = shift;
67
68 my $res = $cb->( GET '/serialize' );
69 is($res->content, $expected,
70 "serialized content in response: $test->{name}");
71 };
72
73 }
74
75
76 done_testing();