Add query string encoding & decoding functions

This commit is contained in:
Yorhel 2025-02-18 14:08:05 +01:00
parent 90cfd66069
commit 67e6d99f01
5 changed files with 120 additions and 15 deletions

View file

@ -13,7 +13,7 @@ sub start {
sub record($id, $type, $data, $pad=undef) {
$pad //= rand > 0.5 ? int rand(50) : 0;
my $msg = pack('CCnnCC', 1, $type, $id, length($data), $pad, 0) . $data . ("\0"x$pad);
is $remote->syswrite($msg, length($msg)), length($msg);
die "Short write" if $remote->syswrite($msg, length($msg)) != length($msg);
}
sub begin($id=1, $role=1, $keep=0) {

27
t/query.t Normal file
View file

@ -0,0 +1,27 @@
use v5.36;
use Test::More;
use FU::Util qw/query_decode query_encode/;
use experimental 'builtin';
is_deeply
query_decode('a&a&%c3%be=%26%3d%c3%be&a=3'),
{ a => [ builtin::true, builtin::true, 3 ], "\xfe" => "&=\xfe" };
ok !eval { query_decode('%10'); 1 };
like $@, qr/Invalid control character/;
is query_encode
{ a => builtin::true, b => undef, c => builtin::false, d => 'string', e => "&=\xfe" },
'a&d=string&e=%26%3d%c3%be';
is query_encode
{ "\xfe" => [ 1, undef, 3, builtin::false, builtin::true ] },
"%c3%be=1&%c3%be=3&%c3%be";
sub FUTILTEST::TO_QUERY { '&'.($_[0][0] + 1) }
is query_encode
{ -ab => bless [2], 'FUTILTEST' },
'-ab=%263';
done_testing;