Skip to content

Commit

Permalink
test plan
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonjr committed Sep 9, 2024
1 parent 30f6ba2 commit 70ea1fb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
2 changes: 1 addition & 1 deletion ddclient.in
Original file line number Diff line number Diff line change
Expand Up @@ -7702,7 +7702,7 @@ sub nic_ionos_update {
my $reply = geturl(proxy => opt('proxy'), url => $url);
last if !header_ok($reply);

if ($reply =~ /code = 200/) {
if ($reply =~ /200 OK/) {
$recap{$h}{'ip'} = $ip;
$recap{$h}{'mtime'} = $now;
$recap{$h}{'status'} = 'good';
Expand Down
95 changes: 64 additions & 31 deletions t/protocol_ionos.pl
Original file line number Diff line number Diff line change
@@ -1,39 +1,72 @@
use Test::More;
# Your test dependencies go here.

SKIP: { eval { require Test::Warnings; } or skip($@, 1); }
eval { require 'ddclient'; } or BAIL_OUT($@);
BEGIN { eval { require Test::Warnings; } or skip($@, 1); }
BEGIN { eval { require 'ddclient'; } or BAIL_OUT($@); }
BEGIN {
eval { require ddclient::t::HTTPD; 1; } or plan(skip_all => $@);
ddclient::t::HTTPD->import();
plan tests => 2;
}

{
package Logger;
use parent qw(-norequire ddclient::Logger);
sub new {
my ($class, $parent) = @_;
my $self = $class->SUPER::new(undef, $parent);
$self->{logs} = [];
return $self;
}
sub _log {
my ($self, $args) = @_;
push(@{$self->{logs}}, $args)
if ($args->{label} // '') =~ qr/^(?:WARNING|FATAL|SUCCESS|FAILED)$/;
return $self->SUPER::_log($args);
}
}

httpd()->run(sub {
my ($req) = @_;
diag('==============================================================================');
diag("Test server received request:\n" . $req->as_string());
return undef if $req->uri()->path() eq '/control';
return [401, [@$textplain, 'www-authenticate' => 'Basic realm="realm", charset="UTF-8"'],
['authentication required']] if ($req->header('authorization') // '') ne $wantauthn;
return [400, $textplain, ['invalid method: ' . $req->method()]] if $req->method() ne 'GET';
return undef;
});

local %ddclient::config = (
'host.my.example.com' => {
'protocol' => 'ionos',
'password' => 'mytestingpassword',
'server' => httpd()->endpoint(),
'wantip' => '1.2.3.4',
});

ddclient::nic_ionos_update(undef, 'host.my.example.com');

my @requests = httpd()->reset();
is(scalar(@requests), 1, "$tc->{desc}: single update request");

my $req = shift(@requests);
is($req->uri()->path(), '/dns/v1/dyndns', "$tc->{desc}: request path");
is($req->uri()->query(), 'q=mytestingpassword', "$tc->{desc}: request query");
# Subtest for the protocol_ionos
subtest 'Testing protocol_ionos' => sub {
# Mock HTTP server
httpd()->run(sub {
my ($req) = @_;
diag('==============================================================================');
diag("Test server received request:\n" . $req->as_string());

return undef if $req->uri()->path() eq '/control';
return [400, $textplain, ['invalid method: ' . $req->method()]] if $req->method() ne 'GET';
return undef
});

local $ddclient::globals{debug} = 1;
local $ddclient::globals{verbose} = 1;
my $l = Logger->new($ddclient::_l);

# Mock ddclient config
local %ddclient::config = (
'host.my.example.com' => {
'protocol' => 'ionos',
'password' => 'mytestingpassword',
'server' => httpd()->endpoint(),
'wantip' => '1.2.3.4',
},
);

httpd()->reset([200, $textplain, []]);

{
local $ddclient::_l = $l;

# Run the protocol update
ddclient::nic_ionos_update(undef, 'host.my.example.com');
}

# Capture HTTP requests
my @requests = httpd()->reset();
is(scalar(@requests), 1, "Single update request");

my $req = shift(@requests);
is($req->uri()->path(), '/dns/v1/dyndns', "Correct request path");
is($req->uri()->query(), 'q=mytestingpassword', "Correct request query");
};

done_testing();

0 comments on commit 70ea1fb

Please sign in to comment.