| 1 |
# vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et : |
| 2 |
use strictures 2; |
| 3 |
use stable 0.031 'postderef'; |
| 4 |
use experimental 'signatures'; |
| 5 |
no autovivification warn => qw(fetch store exists delete); |
| 6 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 7 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 8 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 9 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 10 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 11 |
no feature 'switch'; |
| 12 |
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8 |
| 13 |
use utf8; |
| 14 |
|
| 15 |
use lib 't/lib'; |
| 16 |
use Helper; |
| 17 |
|
| 18 |
use JSON::Schema::Modern::Utilities qw(match_media_type add_media_type delete_media_type decode_media_type encode_media_type); |
| 19 |
|
| 20 |
subtest 'new media-type handler' => sub { |
| 21 |
my @types = ( |
| 22 |
'*/*', |
| 23 |
'mytext/*', |
| 24 |
'mytext/plAin', |
| 25 |
'mytext/foo+plain', |
| 26 |
'mytext/bar+plain', |
| 27 |
'mytext/plain; charset=iso8891-1', |
| 28 |
'mytext/plain;charset=utf-8', |
| 29 |
'mytext/PLAIN; charset=utf-8; x=y', |
| 30 |
'mytext/foo; x="\1y"', # equivalent to text/foo; x=1y |
| 31 |
'foo/bar', |
| 32 |
); |
| 33 |
|
| 34 |
my @tests = ( |
| 35 |
# media-type => best candidate match from media-types added above |
| 36 |
[ '*/*', '*/*' ], # wildcards match themselves |
| 37 |
[ 'any/thing', '*/*' ], # anything matches */* |
| 38 |
[ 'MYTEXT/*', 'mytext/*' ], # "" |
| 39 |
[ 'mytext/PLaiN', 'mytext/plAin' ], # exact type + subtype match |
| 40 |
[ 'mytext/html', 'mytext/*' ], # wildcard subtype match |
| 41 |
[ 'myapplication/json', '*/*' ], # wildcard match |
| 42 |
[ 'mytext/plain; x=y; charset=UtF-8', 'mytext/PLAIN; charset=utf-8; x=y' ], # full param match (2) |
| 43 |
[ 'mytext/plain; charset=UTF-8' => 'mytext/plain;charset=utf-8' ], # full param match (1) |
| 44 |
[ 'mytext/plain; a=b; charset=UtF-8', 'mytext/plain;charset=utf-8' ], # partial param match |
| 45 |
[ 'mytext/foo+plain', 'mytext/foo+plain' ], # match with subtype qualifier |
| 46 |
[ 'mytext/baz+plain', 'mytext/plAin' ], # subtype qualifier mismatch |
| 47 |
[ 'mytext/foo; x="\1\y"', 'mytext/foo; x="\1y"' ], # quoted-pair in parameter |
| 48 |
[ 'mytext/foo; x=1y', 'mytext/foo; x="\1y"' ], # no quotes still matches |
| 49 |
[ 'foo/bar ; charset=blah ; furble=bloop', 'foo/bar' ], # whitespace around ; still matches |
| 50 |
[ 'foo/bar; furble="bl;oop"', 'foo/bar' ], # ; in quoted string still matches |
| 51 |
[ 'foo/bar; foo="blah', undef ], # no trailing quote |
| 52 |
[ 'foo/bar; junk', undef ], # check for trailing junk |
| 53 |
[ 'éclair/yum', undef ], # non-ascii chars in type |
| 54 |
[ 'yum/éclair', undef ], # non-ascii chars in subtype |
| 55 |
[ 'foo/bar; éclair=yum', undef ], # non-ascii chars in parameter name |
| 56 |
[ 'foo/bar; yum=éclair', undef ], # non-ascii chars in parameter value |
| 57 |
[ 'foo/bar; yum=ಠ_ಠ', undef ], # non-ascii chars in parameter value |
| 58 |
[ qq{foo/bar; yum=\xE0\xB2\xA0\x5F\xE0\xB2\xA0}, undef ], # unquoted UTF-8 chars "" |
| 59 |
[ qq{foo/bar; yum="\xE0\xB2\xA0\x5F\xE0\xB2\xA0"}, 'foo/bar' ], # quoted UTF-8 chars "" |
| 60 |
); |
| 61 |
|
| 62 |
# first, run the tests by passing in the list of candidate types |
| 63 |
foreach my $test (@tests) { |
| 64 |
is_equal( |
| 65 |
scalar match_media_type($test->[0], \@types), |
| 66 |
$test->[1], |
| 67 |
'using ad-hoc list: ' |
| 68 |
.(defined $test->[1] ? "$test->[0] matches $test->[1]" : "$test->[0] is not valid"), |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
# then run the tests using our global registry of types |
| 73 |
add_media_type($_) foreach @types; |
| 74 |
|
| 75 |
foreach my $test (@tests) { |
| 76 |
is_equal( |
| 77 |
scalar match_media_type($test->[0]), |
| 78 |
$test->[1], |
| 79 |
'using registry: ' |
| 80 |
.(defined $test->[1] ? "$test->[0] matches $test->[1]" : "$test->[0] is not valid"), |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
delete_media_type('*/*'); |
| 85 |
is_equal( |
| 86 |
(match_media_type('any/thing') // undef), |
| 87 |
undef, |
| 88 |
'after deleting */* entry, this lookup fails', |
| 89 |
); |
| 90 |
|
| 91 |
like( |
| 92 |
dies { add_media_type('FOO-BAR' => sub {}) }, |
| 93 |
qr/^bad media-type string "FOO-BAR"/, |
| 94 |
'bad media-type strings are rejected', |
| 95 |
); |
| 96 |
|
| 97 |
like( |
| 98 |
dies { add_media_type('MYTEXT/PLAIN; CHARSET=UTF-8' => sub {}) }, |
| 99 |
qr/^duplicate media-type found/, |
| 100 |
'cannot add a type twice (when comparing normalized forms)', |
| 101 |
); |
| 102 |
|
| 103 |
is_equal( |
| 104 |
scalar decode_media_type('foo/bar', \'hi'), |
| 105 |
undef, |
| 106 |
'unknown media-type decoder returns undef, not a reference', |
| 107 |
); |
| 108 |
|
| 109 |
is_equal( |
| 110 |
scalar encode_media_type('foo/bar', \'hi'), |
| 111 |
undef, |
| 112 |
'unknown media-type encoder returns undef, not a reference', |
| 113 |
); |
| 114 |
|
| 115 |
like( |
| 116 |
dies { add_media_type('multipart/furble' => sub {}) }, |
| 117 |
qr{^multipart encoders/decoders cannot be defined here: use OpenAPI::Modern}, |
| 118 |
'cannot create an entry for anything multipart here', |
| 119 |
); |
| 120 |
}; |
| 121 |
|
| 122 |
subtest 'application/json' => sub { |
| 123 |
is_equal( |
| 124 |
decode_media_type($_, \'{"a":1,"b":2}')->$*, |
| 125 |
{ a => 1, b => 2 }, |
| 126 |
"decoder for $_", |
| 127 |
) foreach 'application/json', 'application/json; charset=UTF-8'; |
| 128 |
|
| 129 |
is_equal( |
| 130 |
encode_media_type($_, \[ 0, 1, 2, 3, 4 ])->$*, |
| 131 |
'[0,1,2,3,4]', |
| 132 |
"encoder for $_", |
| 133 |
) foreach 'application/json', 'application/json; charset=UTF-8'; |
| 134 |
|
| 135 |
die_result( |
| 136 |
sub { decode_media_type('application/json', \'blargh') }, |
| 137 |
qr/^malformed JSON string/, |
| 138 |
'decoder for "application/json" throws an exception for bad data', |
| 139 |
); |
| 140 |
|
| 141 |
die_result( |
| 142 |
sub { encode_media_type('application/json', \ sub { 1 }) }, |
| 143 |
qr/JSON can only represent references to arrays or hashes/, |
| 144 |
'encoder for "application/json" throws an exception for bad data', |
| 145 |
); |
| 146 |
}; |
| 147 |
|
| 148 |
subtest 'text/*' => sub { |
| 149 |
is_equal( |
| 150 |
decode_media_type('text/plain', \"\xe0\xb2\xa0\x5f\xe0\xb2\xa0")->$*, |
| 151 |
"\xe0\xb2\xa0\x5f\xe0\xb2\xa0", |
| 152 |
'decoder without charset', |
| 153 |
); |
| 154 |
|
| 155 |
is_equal( |
| 156 |
decode_media_type('text/plain; charset=UTF-8', \"\xe0\xb2\xa0\x5f\xe0\xb2\xa0")->$*, |
| 157 |
'ಠ_ಠ', |
| 158 |
'decoder with UTF-8 charset', |
| 159 |
); |
| 160 |
|
| 161 |
is_equal( |
| 162 |
encode_media_type('text/plain; charset=UTF-8', \'ಠ_ಠ')->$*, |
| 163 |
"\xe0\xb2\xa0\x5f\xe0\xb2\xa0", |
| 164 |
'encoder with UTF-8 charset', |
| 165 |
); |
| 166 |
|
| 167 |
is_equal( |
| 168 |
decode_media_type('text/plain; charset=latin1', \"\xe9clair")->$*, |
| 169 |
'éclair', |
| 170 |
'decoder with latin1 charset', |
| 171 |
); |
| 172 |
|
| 173 |
is_equal( |
| 174 |
encode_media_type('text/plain; charset=latin1', \'éclair')->$*, |
| 175 |
"\xe9clair", |
| 176 |
'encoder with latin1 charset', |
| 177 |
); |
| 178 |
}; |
| 179 |
|
| 180 |
subtest 'application/x-www-form-urlencoded'=> sub { |
| 181 |
is_equal( |
| 182 |
decode_media_type('application/x-www-form-urlencoded', \'foo=%E0%B2%A0_%E0%B2%A0')->$*, |
| 183 |
{ foo => 'ಠ_ಠ' }, |
| 184 |
'decoder', |
| 185 |
); |
| 186 |
|
| 187 |
is_equal( |
| 188 |
encode_media_type('application/x-www-form-urlencoded', \{ foo => 'ಠ_ಠ' })->$*, |
| 189 |
'foo=%E0%B2%A0_%E0%B2%A0', |
| 190 |
'encoder with wide characters', |
| 191 |
); |
| 192 |
|
| 193 |
is_equal( |
| 194 |
encode_media_type('application/x-www-form-urlencoded', \[ map +{ (chr)x2 }, 0 .. 0x7f ])->$*, |
| 195 |
join('&', |
| 196 |
map join('=', $_, $_), |
| 197 |
map +(m/^[[:alnum:]\x2A\x2D\x2E\x5F]\z/a ? $_ : sprintf('%%%02X', ord)), |
| 198 |
map chr, 0 .. 0x7f |
| 199 |
), |
| 200 |
'encoder for all ascii characters percent-encodes the right set, as an array of tuples', |
| 201 |
); |
| 202 |
|
| 203 |
is_equal( |
| 204 |
encode_media_type('application/x-www-form-urlencoded', \{ map +((chr)x2), 0 .. 0x7f })->$*, |
| 205 |
join('&', |
| 206 |
map join('=', $_, $_), |
| 207 |
map +(m/^[[:alnum:]\x2A\x2D\x2E\x5F]\z/a ? $_ : sprintf('%%%02X', ord)), |
| 208 |
map chr, 0 .. 0x7f |
| 209 |
), |
| 210 |
'encoder for all ascii characters percent-encodes the right set, as an object of single values', |
| 211 |
); |
| 212 |
|
| 213 |
is_equal( |
| 214 |
encode_media_type('application/x-www-form-urlencoded', \{ map +((chr)x2), 0 .. 0x7f })->$*, |
| 215 |
join('&', |
| 216 |
map join('=', $_, $_), |
| 217 |
map +(m/^[[:alnum:]\x2A\x2D\x2E\x5F]\z/a ? $_ : sprintf('%%%02X', ord)), |
| 218 |
map chr, 0 .. 0x7f |
| 219 |
), |
| 220 |
'encoder for all ascii characters percent-encodes the right set, as an object of single values', |
| 221 |
); |
| 222 |
|
| 223 |
is_equal( |
| 224 |
decode_media_type('application/x-www-form-urlencoded', \'a=x&a=y&b=1&a=z&b=2')->$*, |
| 225 |
{ a => [qw(x y z)], b => [qw(1 2)] }, |
| 226 |
'with array values', |
| 227 |
); |
| 228 |
is_equal( |
| 229 |
encode_media_type('application/x-www-form-urlencoded', \{ a => [qw(x y z)], b => [qw(1 2)] })->$*, |
| 230 |
'a=x&a=y&a=z&b=1&b=2', |
| 231 |
'encoder with array values, normalized', |
| 232 |
); |
| 233 |
|
| 234 |
is_equal( |
| 235 |
decode_media_type('application/x-www-form-urlencoded; type=array', \'a=x&a=y&b=1&a=z&b=2')->$*, |
| 236 |
[ { a => 'x' }, { a => 'y' }, { b => '1' }, { a => 'z' }, { b => '2' } ], |
| 237 |
'decoder preserves order, when decoded to an array of tuples', |
| 238 |
); |
| 239 |
|
| 240 |
is_equal( |
| 241 |
encode_media_type('application/x-www-form-urlencoded', |
| 242 |
\[ { a => 'x' }, { a => 'y' }, { b => '1' }, { a => 'z' }, { b => '2' } ])->$*, |
| 243 |
'a=x&a=y&b=1&a=z&b=2', |
| 244 |
'encoder, from an array of tuples', |
| 245 |
); |
| 246 |
}; |
| 247 |
|
| 248 |
subtest 'application/x-ndjson' => sub { |
| 249 |
is_equal( |
| 250 |
decode_media_type('application/x-ndjson', \qq!{"a":1,"b":2}\n[0,1,2,3,4]!)->$*, |
| 251 |
[ { a => 1, b => 2 }, [ 0, 1, 2, 3, 4 ] ], |
| 252 |
'decoder', |
| 253 |
); |
| 254 |
|
| 255 |
is_equal( |
| 256 |
encode_media_type('application/x-ndjson', \[ { a => 1 }, [ 0, 1, 2, 3, 4 ] ])->$*, |
| 257 |
qq!{"a":1}\n[0,1,2,3,4]!, |
| 258 |
'encoder', |
| 259 |
); |
| 260 |
}; |
| 261 |
|
| 262 |
done_testing; |