Debian Codegraph @

t/serializer_mutable.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More tests => 4
5 ;
6 use Dancer2::Serializer::Mutable;
7 use Plack::Test;
8 use HTTP::Request::Common;
9 use Encode;
10 use JSON::MaybeXS;
11 use YAML;
12 use Ref::Util qw<is_coderef>;
13
14 {
15 package MyApp;
16 use Dancer2;
17 use Ref::Util qw<is_hashref>;
18
19 set serializer => 'Mutable';
20
21 get '/serialize' => sub { +{ bar => 'baz' } };
22 post '/deserialize' => sub {
23 return request->data &&
24 is_hashref( request->data ) &&
25 request->data->{bar} ? { bar => request->data->{bar} } : { ret => '?' };
26 };
27 }
28
29 my $test = Plack::Test->create( MyApp->to_app );
30
31 subtest "serializer returns to default state" => sub {
32
33 my $res = $test->request( GET '/serialize' );
34 is(
35 $res->headers->content_type,
36 'application/json',
37 "Default content-type header",
38 );
39
40 $res = $test->request( GET '/serialize', 'Accept' => 'text/x-data-dumper' );
41 is(
42 $res->headers->content_type,
43 'application/json',
44 "Dumper content type falls back to default as it is not enabled",
45 );
46 $res = $test->request( GET '/serialize', 'Accept' => 'text/x-json' );
47 is(
48 $res->headers->content_type,
49 'text/x-json',
50 "Correct content-type header",
51 );
52 $res = $test->request( GET '/serialize' );
53 is(
54 $res->headers->content_type,
55 'application/json',
56 "Correct default content-type header after a request that used another",
57 );
58 };
59
60
61
62 # Configure test content-type cases
63 my $d = {
64 yaml => {
65 types => [ qw(text/x-yaml text/html) ],
66 value => encode('UTF-8', YAML::Dump({ bar => 'baz' })),
67 last_val => "---bar:baz",
68 },
69 json => {
70 types => [ qw(text/x-json application/json) ],
71 value => JSON::MaybeXS::encode_json({ bar => 'baz' }),
72 last_val => '{"bar":"baz"}',
73 },
74 default => {
75 types => [ '*/*', '' ],
76 value => JSON::MaybeXS::encode_json({ bar => 'baz' }),
77 last_val => '{"bar":"baz"}',
78 return_content_type => 'application/json',
79 },
80 };
81
82 for my $format (keys %$d) {
83
84 subtest "Format: $format" => sub {
85 my $s = $d->{$format};
86
87 # Response with implicit call to the serializer
88 for my $content_type ( @{ $s->{types} } ) {
89
90 for my $ct (qw/Content-Type Accept/) {
91
92 # Test getting the value serialized in the correct format
93 my $res = $test->request( GET '/serialize', $ct => $content_type );
94
95 is( $res->code, 200, "[/$format] Correct status" );
96 is( $res->content, $s->{value}, "[/$format] Correct content" );
97 is(
98 $res->headers->content_type,
99 $s->{return_content_type} || $content_type,
100 "[/$format] Correct content-type headers",
101 );
102 }
103
104 # Test sending the value serialized in the correct format
105 # needs to be de-serialized and returned
106 my $req = $test->request( POST '/deserialize',
107 'Content-Type' => $content_type,
108 content => $s->{value} );
109
110 my $content = $req->content;
111 $content =~ s/\s//g;
112 is( $req->code, 200, "[/$format] Deserialize: correct status" );
113 is( $content, $s->{last_val}, "[/$format] Deserialize: correct content" );
114
115 } #/ for my $content_type
116 }; #/ subtest
117
118 } #/ for my $format