test: simplify utils test cases with tables
This commit is contained in:
181
t/05_utils.t
181
t/05_utils.t
@@ -4,61 +4,68 @@ use Urupam::Utils ();
|
|||||||
use_ok('Urupam::Utils');
|
use_ok('Urupam::Utils');
|
||||||
|
|
||||||
subtest 'sanitize_input' => sub {
|
subtest 'sanitize_input' => sub {
|
||||||
is( Urupam::Utils::sanitize_input(undef), '', 'undef becomes empty' );
|
my @cases = (
|
||||||
is( Urupam::Utils::sanitize_input(" spaced out\t\n"),
|
[ undef, '', 'undef becomes empty' ],
|
||||||
'spaced out', 'trims leading and trailing whitespace' );
|
[ " spaced out\t\n", 'spaced out', 'trims leading/trailing' ],
|
||||||
is( Urupam::Utils::sanitize_input('a b'),
|
[ 'a b', 'a b', 'preserves internal whitespace' ],
|
||||||
'a b', 'preserves internal whitespace' );
|
[ 0, '0', 'handles numeric zero' ],
|
||||||
is( Urupam::Utils::sanitize_input(0), '0', 'handles numeric zero' );
|
[ '0', '0', 'handles string zero' ],
|
||||||
is( Urupam::Utils::sanitize_input('0'), '0', 'handles string zero' );
|
);
|
||||||
|
|
||||||
|
for my $case (@cases) {
|
||||||
|
is( Urupam::Utils::sanitize_input( $case->[0] ),
|
||||||
|
$case->[1], $case->[2] );
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
subtest 'get_error_status' => sub {
|
subtest 'get_error_status' => sub {
|
||||||
is( Urupam::Utils::get_error_status('SSL certificate error'),
|
my @cases = (
|
||||||
422, 'SSL error maps to 422' );
|
[ 'SSL certificate error', 422, 'SSL error maps to 422' ],
|
||||||
is( Urupam::Utils::get_error_status('Cannot reach URL: timeout'),
|
[ 'Cannot reach URL: timeout', 422, 'connection error maps to 422' ],
|
||||||
422, 'connection error maps to 422' );
|
[ 'DNS resolution failed', 422, 'DNS error maps to 422' ],
|
||||||
is( Urupam::Utils::get_error_status('DNS resolution failed'),
|
[ 'server error: 500', 422, 'server error maps to 422' ],
|
||||||
422, 'DNS error maps to 422' );
|
[ 'SeRvEr ErRoR', 422, 'mixed case matches' ],
|
||||||
is( Urupam::Utils::get_error_status('server error: 500'),
|
[ 'Database error: connection failed', 400, 'default maps to 400' ],
|
||||||
422, 'server error maps to 422' );
|
[ 'unrelated error', 400, 'non-matching message maps to 400' ],
|
||||||
is( Urupam::Utils::get_error_status('SeRvEr ErRoR'),
|
);
|
||||||
422, 'mixed case matches' );
|
|
||||||
is( Urupam::Utils::get_error_status('Database error: connection failed'),
|
for my $case (@cases) {
|
||||||
400, 'default maps to 400' );
|
is( Urupam::Utils::get_error_status( $case->[0] ),
|
||||||
is( Urupam::Utils::get_error_status('unrelated error'),
|
$case->[1], $case->[2] );
|
||||||
400, 'non-matching message maps to 400' );
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
subtest 'sanitize_error_message' => sub {
|
subtest 'sanitize_error_message' => sub {
|
||||||
is(
|
my @cases = (
|
||||||
Urupam::Utils::sanitize_error_message(undef),
|
[ undef, 'An error occurred', 'undef gets default message' ],
|
||||||
'An error occurred',
|
[
|
||||||
'undef gets default message'
|
"Database error: <bad>\n\t!!",
|
||||||
);
|
|
||||||
is(
|
|
||||||
Urupam::Utils::sanitize_error_message("Database error: <bad>\n\t!!"),
|
|
||||||
'Database error: bad',
|
'Database error: bad',
|
||||||
'strips unsafe chars and collapses whitespace'
|
'strips unsafe chars and collapses whitespace'
|
||||||
);
|
],
|
||||||
is(
|
[
|
||||||
Urupam::Utils::sanitize_error_message('Allowed: ./-_: ok'),
|
'Allowed: ./-_: ok',
|
||||||
'Allowed: ./-_: ok',
|
'Allowed: ./-_: ok',
|
||||||
'preserves allowed punctuation'
|
'preserves allowed punctuation'
|
||||||
);
|
],
|
||||||
is(
|
[
|
||||||
Urupam::Utils::sanitize_error_message("Error with spaces"),
|
"Error with spaces",
|
||||||
'Error with spaces',
|
'Error with spaces',
|
||||||
'collapses multiple spaces'
|
'collapses multiple spaces'
|
||||||
);
|
],
|
||||||
is( Urupam::Utils::sanitize_error_message("Bad\x01\x02Chars"),
|
[ "Bad\x01\x02Chars", 'BadChars', 'strips control characters' ],
|
||||||
'BadChars', 'strips control characters' );
|
[
|
||||||
is(
|
"Cafe\x{00E9} error",
|
||||||
Urupam::Utils::sanitize_error_message("Cafe\x{00E9} error"),
|
|
||||||
"Cafe\x{00E9} error",
|
"Cafe\x{00E9} error",
|
||||||
'preserves Unicode characters'
|
'preserves Unicode characters'
|
||||||
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
for my $case (@cases) {
|
||||||
|
is( Urupam::Utils::sanitize_error_message( $case->[0] ),
|
||||||
|
$case->[1], $case->[2] );
|
||||||
|
}
|
||||||
|
|
||||||
my $long_error = 'Error: ' . ( 'a' x 210 );
|
my $long_error = 'Error: ' . ( 'a' x 210 );
|
||||||
my $expected = substr( $long_error, 0, 200 ) . '...';
|
my $expected = substr( $long_error, 0, 200 ) . '...';
|
||||||
is( Urupam::Utils::sanitize_error_message($long_error),
|
is( Urupam::Utils::sanitize_error_message($long_error),
|
||||||
@@ -66,51 +73,71 @@ subtest 'sanitize_error_message' => sub {
|
|||||||
};
|
};
|
||||||
|
|
||||||
subtest 'sanitize_url' => sub {
|
subtest 'sanitize_url' => sub {
|
||||||
is( Urupam::Utils::sanitize_url(undef), undef, 'undef stays undef' );
|
my @cases = (
|
||||||
is( Urupam::Utils::sanitize_url(' '), undef, 'blank is undef' );
|
[ undef, undef, 'undef stays undef' ],
|
||||||
is( Urupam::Utils::sanitize_url('example.com/path'),
|
[ ' ', undef, 'blank is undef' ],
|
||||||
'http://example.com/path', 'adds scheme when missing' );
|
[
|
||||||
is( Urupam::Utils::sanitize_url(' example.com/path '),
|
'example.com/path', 'http://example.com/path',
|
||||||
'http://example.com/path', 'trims before processing' );
|
'adds scheme when missing'
|
||||||
is( Urupam::Utils::sanitize_url('https://example.com/path'),
|
],
|
||||||
'https://example.com/path', 'preserves http(s) scheme' );
|
[
|
||||||
is( Urupam::Utils::sanitize_url('http://example.com'),
|
' example.com/path ',
|
||||||
'http://example.com', 'does not double-add scheme' );
|
'http://example.com/path',
|
||||||
is( Urupam::Utils::sanitize_url('HTTP://Example.com/Path'),
|
'trims before processing'
|
||||||
'HTTP://Example.com/Path', 'accepts mixed-case scheme' );
|
],
|
||||||
is( Urupam::Utils::sanitize_url('https://example.com/%7Euser'),
|
[
|
||||||
'https://example.com/~user', 'unescapes percent-encoded path' );
|
'https://example.com/path', 'https://example.com/path',
|
||||||
is(
|
'preserves http(s) scheme'
|
||||||
Urupam::Utils::sanitize_url('https://example.com/%7Euser%2Fdocs'),
|
],
|
||||||
|
[
|
||||||
|
'http://example.com', 'http://example.com',
|
||||||
|
'does not double-add scheme'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'HTTP://Example.com/Path', 'HTTP://Example.com/Path',
|
||||||
|
'accepts mixed-case scheme'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'https://example.com/%7Euser', 'https://example.com/~user',
|
||||||
|
'unescapes percent-encoded path'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'https://example.com/%7Euser%2Fdocs',
|
||||||
'https://example.com/~user/docs',
|
'https://example.com/~user/docs',
|
||||||
'unescapes multiple percent-encoded segments'
|
'unescapes multiple percent-encoded segments'
|
||||||
);
|
],
|
||||||
is(
|
[
|
||||||
Urupam::Utils::sanitize_url('https://example.com?q=hello%20world'),
|
'https://example.com?q=hello%20world',
|
||||||
'https://example.com?q=hello%20world',
|
'https://example.com?q=hello%20world',
|
||||||
'preserves percent-encoded query'
|
'preserves percent-encoded query'
|
||||||
);
|
],
|
||||||
is(
|
[
|
||||||
Urupam::Utils::sanitize_url('https://example.com#frag%20ment'),
|
'https://example.com#frag%20ment',
|
||||||
'https://example.com#frag%20ment',
|
'https://example.com#frag%20ment',
|
||||||
'preserves percent-encoded fragment'
|
'preserves percent-encoded fragment'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'https://example.com/%257Euser', 'https://example.com/%7Euser',
|
||||||
|
'unescapes only once'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'https://ex%61mple.com/path', undef,
|
||||||
|
'rejects percent-encoded hostname'
|
||||||
|
],
|
||||||
|
[ 'example.com:8080/path', undef, 'rejects missing scheme with colon' ],
|
||||||
|
[ 'user@example.com', undef, 'rejects missing scheme with at-sign' ],
|
||||||
|
[
|
||||||
|
'http://user@example.com/path', undef,
|
||||||
|
'rejects userinfo with scheme'
|
||||||
|
],
|
||||||
|
[ 'http://[::1]/path', 'http://[::1]/path', 'accepts IPv6 host' ],
|
||||||
|
[ 'http://[::1/path', undef, 'rejects malformed IPv6 host' ],
|
||||||
|
[ 'http://exa mple.com/path', undef, 'rejects whitespace in host' ],
|
||||||
);
|
);
|
||||||
is( Urupam::Utils::sanitize_url('https://example.com/%257Euser'),
|
|
||||||
'https://example.com/%7Euser', 'unescapes only once' );
|
for my $case (@cases) {
|
||||||
is( Urupam::Utils::sanitize_url('https://ex%61mple.com/path'),
|
is( Urupam::Utils::sanitize_url( $case->[0] ), $case->[1], $case->[2] );
|
||||||
undef, 'rejects percent-encoded hostname' );
|
}
|
||||||
is( Urupam::Utils::sanitize_url('example.com:8080/path'),
|
|
||||||
undef, 'rejects missing scheme with colon' );
|
|
||||||
is( Urupam::Utils::sanitize_url('user@example.com'),
|
|
||||||
undef, 'rejects missing scheme with at-sign' );
|
|
||||||
is( Urupam::Utils::sanitize_url('http://user@example.com/path'),
|
|
||||||
undef, 'rejects userinfo with scheme' );
|
|
||||||
is( Urupam::Utils::sanitize_url('http://[::1]/path'),
|
|
||||||
'http://[::1]/path', 'accepts IPv6 host' );
|
|
||||||
is( Urupam::Utils::sanitize_url('http://[::1/path'),
|
|
||||||
undef, 'rejects malformed IPv6 host' );
|
|
||||||
is( Urupam::Utils::sanitize_url('http://exa mple.com/path'),
|
|
||||||
undef, 'rejects whitespace in host' );
|
|
||||||
};
|
};
|
||||||
|
|
||||||
done_testing();
|
done_testing();
|
||||||
|
|||||||
Reference in New Issue
Block a user