forked from ddclient/ddclient
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
65 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |