header_ok: Add unit tests

This commit is contained in:
Richard Hansen 2024-05-17 22:22:02 -04:00
parent a486d4f976
commit b58a10b3e3
2 changed files with 68 additions and 0 deletions

View file

@ -68,6 +68,7 @@ handwritten_tests = \
t/geturl_connectivity.pl \ t/geturl_connectivity.pl \
t/geturl_response.pl \ t/geturl_response.pl \
t/group_hosts_by.pl \ t/group_hosts_by.pl \
t/header_ok.pl \
t/interval_expired.pl \ t/interval_expired.pl \
t/is-and-extract-ipv4.pl \ t/is-and-extract-ipv4.pl \
t/is-and-extract-ipv6.pl \ t/is-and-extract-ipv6.pl \

67
t/header_ok.pl Normal file
View file

@ -0,0 +1,67 @@
use Test::More;
SKIP: { eval { require Test::Warnings; } or skip($@, 1); }
eval { require 'ddclient'; } or BAIL_OUT($@);
my $have_mock = eval { require Test::MockModule; };
my $failmsg;
my $module;
if ($have_mock) {
$module = Test::MockModule->new('ddclient');
# Note: 'mock' is used instead of 'redefine' because 'redefine' is not available in the versions
# of Test::MockModule distributed with old Debian and Ubuntu releases.
$module->mock('failed', sub { $failmsg //= ''; $failmsg .= sprintf(shift, @_) . "\n"; });
}
my @test_cases = (
{
desc => 'malformed not OK',
input => 'malformed',
want => 0,
wantmsg => qr/unexpected/,
},
{
desc => 'HTTP/1.1 200 OK',
input => 'HTTP/1.1 200 OK',
want => 1,
},
{
desc => 'HTTP/2 200 OK',
input => 'HTTP/2 200 OK',
want => 1,
},
{
desc => 'HTTP/3 200 OK',
input => 'HTTP/3 200 OK',
want => 1,
},
{
desc => '401 not OK',
input => 'HTTP/1.1 401 bad',
want => 0,
wantmsg => qr/authentication failed/,
},
{
desc => '403 not OK',
input => 'HTTP/1.1 403 bad',
want => 0,
wantmsg => qr/not authorized/,
},
{
desc => 'other 4xx not OK',
input => 'HTTP/1.1 456 bad',
want => 0,
},
);
for my $tc (@test_cases) {
subtest $tc->{desc} => sub {
$failmsg = '';
is(ddclient::header_ok('host', $tc->{input}), $tc->{want}, 'return value matches');
SKIP: {
skip('Test::MockModule not available') if !$have_mock;
like($failmsg, $tc->{wantmsg} // qr/^$/, 'fail message matches');
}
};
}
done_testing();