From 70ea1fb75e6a5414f89ffc7e01e0fd7eff87340e Mon Sep 17 00:00:00 2001 From: Nelson Araujo Date: Sun, 8 Sep 2024 21:31:03 -0700 Subject: [PATCH] test plan --- ddclient.in | 2 +- t/protocol_ionos.pl | 95 ++++++++++++++++++++++++++++++--------------- 2 files changed, 65 insertions(+), 32 deletions(-) diff --git a/ddclient.in b/ddclient.in index 4cdc6a63..40f47590 100755 --- a/ddclient.in +++ b/ddclient.in @@ -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'; diff --git a/t/protocol_ionos.pl b/t/protocol_ionos.pl index 12f945f2..aa12f14f 100644 --- a/t/protocol_ionos.pl +++ b/t/protocol_ionos.pl @@ -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();