Debian Codegraph @

t/integration/serializer/mutable.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Plack::Test;
6 use HTTP::Request;
7
8 # Dancer2::Serializer::Mutable picks a format per request from the HTTP
9 # headers. It makes that choice twice, by two different rules:
10 #
11 # deserialize - _get_content_type('content_type'), so the request's
12 # Content-Type decides how the incoming body is read.
13 # serialize - _get_content_type('accept'), so the request's Accept
14 # decides how the outgoing body is written.
15 #
16 # That asymmetry is intentional and documented: Accept is the header a
17 # client uses to say what it wants back, so it is consulted first when
18 # choosing a response format, while Content-Type - what the client says it
19 # sent - is consulted first when reading the request body.
20 #
21 # The lookup matches on the content type alone: any ';'-separated parameters
22 # (such as '; charset=utf-8') are stripped, and the result is lowercased,
23 # before comparing against the mapping. That used to not be true - a
24 # charset parameter was an exact-match miss that silently fell back to JSON,
25 # turning a valid YAML request into a 400. It is asserted below as fixed.
26
27 {
28 package MutableApp;
29 use Dancer2;
30 set logger => 'null';
31 set serializer => 'Mutable';
32
33 get '/out' => sub { { a => 1 } };
34 post '/round' => sub { { got => request->data } };
35 }
36
37 my $test = Plack::Test->create( MutableApp->to_app );
38
39 sub hit {
40 my ( $method, $path, $headers, $body ) = @_;
41 return $test->request(
42 HTTP::Request->new( $method, $path, $headers || [], $body ) );
43 }
44
45 subtest 'Accept chooses the outgoing format' => sub {
46 my %expected = (
47 'application/json' => [ 'application/json', qr/^\{"a":1\}$/ ],
48 'text/x-json' => [ 'text/x-json', qr/^\{"a":1\}$/ ],
49 'text/x-yaml' => [ 'text/x-yaml', qr/^---\na: 1\n$/ ],
50 'text/html' => [ 'text/html', qr/^---\na: 1\n$/ ],
51 );
52
53 for my $accept ( sort keys %expected ) {
54 my ( $content_type, $body_like ) = @{ $expected{$accept} };
55 my $response = hit( GET => '/out', [ Accept => $accept ] );
56
57 is( $response->code, 200, "Accept: $accept responds" );
58 is( $response->header('Content-Type'), $content_type,
59 "Accept: $accept sets Content-Type to match" );
60 like( $response->content, $body_like,
61 "Accept: $accept serializes in that format" );
62 }
63 };
64
65 subtest 'an unrecognised or absent Accept falls back to JSON' => sub {
66 # 'text/x-data-dumper' is in this list rather than the one above because
67 # the Dumper serializer is no longer part of the default mapping: it
68 # deserializes by evaluating the request body as perl, so it must now be
69 # opted into with engines.serializer.Mutable.enable_dumper. This app does
70 # not set it, so the type is simply unrecognised and falls through to the
71 # default - which is the point worth asserting, since falling back to
72 # JSON rather than quietly reaching for Dumper is what keeps an
73 # unconfigured app away from the eval.
74 for my $accept (
75 'application/xml', 'text/plain', '*/*', 'text/x-data-dumper' )
76 {
77 my $response = hit( GET => '/out', [ Accept => $accept ] );
78 is( $response->header('Content-Type'), 'application/json',
79 "Accept: $accept falls back to JSON" );
80 is( $response->content, '{"a":1}', "Accept: $accept body is JSON" );
81 }
82
83 my $bare = hit( GET => '/out' );
84 is( $bare->header('Content-Type'), 'application/json',
85 'no Accept header at all falls back to JSON' );
86 is( $bare->content, '{"a":1}', 'with a JSON body' );
87 };
88
89 subtest 'Content-Type chooses the incoming format' => sub {
90 # YAML in, and (with no Accept header) JSON is *not* what comes back -
91 # _get_content_type('accept') falls through to content_type, so the
92 # response follows the request's Content-Type here.
93 my $yaml = hit( POST => '/round', [ 'Content-Type' => 'text/x-yaml' ],
94 "---\na: 1\n" );
95 is( $yaml->code, 200, 'a YAML body is accepted' );
96 like( $yaml->content, qr/^---\n/, 'and answered in YAML' );
97 like( $yaml->content, qr/a: 1/, 'with the deserialized value' );
98
99 my $json = hit( POST => '/round', [ 'Content-Type' => 'application/json' ],
100 '{"a":1}' );
101 is( $json->code, 200, 'a JSON body is accepted' );
102 is( $json->content, '{"got":{"a":1}}',
103 'deserialized and answered in JSON' );
104
105 # An unrecognised Content-Type falls back to JSON for the *input* too,
106 # which is why a JSON body under a bogus type still works.
107 my $unknown = hit( POST => '/round',
108 [ 'Content-Type' => 'application/xml' ], '{"a":1}' );
109 is( $unknown->code, 200,
110 'an unrecognised Content-Type falls back to JSON' );
111 is( $unknown->content, '{"got":{"a":1}}',
112 'so a JSON body under a bogus content type is still read' );
113 };
114
115 subtest 'Accept wins over Content-Type when serializing (documented)' => sub {
116
117 # The module's DESCRIPTION documents two different priority orders:
118 # Content-Type first when deserializing a request body, but Accept first
119 # when serializing a response - because Accept is the header a client
120 # uses to say what format it wants *back*, which need not match what it
121 # sent. serialize() calls _get_content_type('accept'), which checks the
122 # headers in the order accept, content_type, accept
123 # (Dancer2/Serializer/Mutable.pm:68 and :95), so Accept wins on the way
124 # out while Content-Type still wins on the way in. The round trip is
125 # therefore asymmetric by design: YAML in, JSON out below.
126
127 my $response = hit(
128 POST => '/round',
129 [ 'Content-Type' => 'text/x-yaml', 'Accept' => 'application/json' ],
130 "---\na: 1\n",
131 );
132
133 is( $response->code, 200, 'the request succeeds' );
134
135 # The body was read as YAML, per the documented rule for deserializing.
136 like( $response->content, qr/"got"/,
137 'the response is JSON, chosen from Accept' );
138 is( $response->header('Content-Type'), 'application/json',
139 'and says so, per the documented rule for serializing' );
140
141 # The mirror image, to show it is the Accept header doing this and not
142 # something about YAML: JSON in, YAML out.
143 my $mirror = hit(
144 POST => '/round',
145 [ 'Content-Type' => 'application/json', 'Accept' => 'text/x-yaml' ],
146 '{"a":1}',
147 );
148 is( $mirror->header('Content-Type'), 'text/x-yaml',
149 'a JSON request is answered in YAML when Accept asks for it' );
150 like( $mirror->content, qr/^---\n/, 'with a YAML body' );
151 };
152
153 subtest 'a charset parameter on Content-Type is stripped before the lookup (fixed)' => sub {
154
155 # The lookup now strips any ';'-separated parameters and lowercases
156 # what remains before matching against the mapping
157 # (Dancer2/Serializer/Mutable.pm:96-105), so a charset parameter no
158 # longer causes a miss. Previously 'text/x-yaml; charset=utf-8' missed
159 # the 'text/x-yaml' key, fell through to the JSON default, and then JSON
160 # was handed a YAML body and failed the request as a 400.
161 #
162 # A charset parameter on Content-Type is entirely ordinary, so this is
163 # reachable from any normal client.
164
165 # The control: the identical request without the parameter works.
166 # No Accept header is sent, so serialize() falls through to
167 # Content-Type for the outgoing format too (see the previous subtest) -
168 # these all round-trip as YAML, not JSON.
169 my $plain = hit( POST => '/round', [ 'Content-Type' => 'text/x-yaml' ],
170 "---\na: 1\n" );
171 is( $plain->code, 200, 'text/x-yaml alone is understood' );
172 like( $plain->content, qr/got:\s*\n\s*a: 1/,
173 'and the YAML body is deserialized' );
174
175 my $with_charset = hit( POST => '/round',
176 [ 'Content-Type' => 'text/x-yaml; charset=utf-8' ], "---\na: 1\n" );
177 is( $with_charset->code, 200,
178 'the same body with "; charset=utf-8" is now understood too' );
179 is( $with_charset->content, $plain->content,
180 'and deserializes exactly as the bare header does' );
181
182 # Case is folded too: the mapping keys are matched case-insensitively.
183 my $uppercase = hit( POST => '/round',
184 [ 'Content-Type' => 'TEXT/X-YAML' ], "---\na: 1\n" );
185 is( $uppercase->code, 200, 'an uppercase spelling is understood as well' );
186 is( $uppercase->content, $plain->content,
187 'and deserializes the same way' );
188
189 # JSON continues to work, now for the right reason rather than by
190 # accident of the fallback also being JSON.
191 my $json_charset = hit( POST => '/round',
192 [ 'Content-Type' => 'application/json; charset=utf-8' ], '{"a":1}' );
193 is( $json_charset->code, 200,
194 'JSON with a charset parameter still works' );
195 is( $json_charset->content, '{"got":{"a":1}}',
196 'because the parameter is stripped, not because of the fallback' );
197
198 # Same fix on the way out: the Accept lookup strips parameters too, so
199 # this no longer silently falls back to JSON.
200 my $accept_charset = hit( GET => '/out',
201 [ Accept => 'text/x-yaml; charset=utf-8' ] );
202 is( $accept_charset->header('Content-Type'), 'text/x-yaml',
203 'an Accept with a charset parameter now chooses YAML, not JSON' );
204 like( $accept_charset->content, qr/^---\na: 1\n$/,
205 'with a YAML body' );
206 };
207
208 done_testing();