Skip to content

Commit

Permalink
Adding resolve_override method
Browse files Browse the repository at this point in the history
  • Loading branch information
david-dick committed Aug 26, 2024
1 parent de1c63f commit 8da6e80
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/Firefox/Marionette.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,19 @@ sub downloads {
return $self->_directory_listing( {}, $download_directory );
}

sub resolve_override {
my ( $self, $host_name, $ip_address ) = @_;
my $old = $self->_context('chrome');
my $result = $self->script(
$self->_compress_script(
<<'_JS_'), args => [ $host_name, $ip_address ] );
const override = Components.classes["@mozilla.org/network/native-dns-override;1"].getService(Components.interfaces.nsINativeDNSResolverOverride);
override.addIPOverride(arguments[0], arguments[1]);
_JS_
$self->_context($old);
return $self;
}

sub resolve {
my ( $self, $host_name, %options ) = @_;
my $old = $self->_context('chrome');
Expand Down Expand Up @@ -14384,6 +14397,27 @@ accepts a hostname as an argument and resolves it to a list of matching IP addre
}


=head2 resolve_override

accepts a hostname and an IP address as parameters. This method then forces the browser to override any future DNS requests for the supplied hostname.

use Firefox::Marionette();
use v5.10;

my $ssh_server = 'remote.example.org';
my $firefox = Firefox::Marionette->new( host => $ssh_server );
my $hostname = 'metacpan.org';
my $ip_address = '127.0.0.1';
foreach my $result ($firefox->resolve_override($hostname, $ip_address)->resolve($hostname)) {
if ($result eq $ip_address) {
die "local metacpan time?";
} else {
die "This should not happen";
}
}

This method returns L<itself|Firefox::Marionette> to aid in chaining methods.

=head2 restart

restarts the browser. After the restart, L<capabilities|Firefox::Marionette::Capabilities> should be restored. The same profile settings should be applied, but the current state of the browser (such as the L<uri|/uri> will be reset (like after a normal browser restart). This method is primarily intended for use by the L<update|/update> method. Not sure if this is useful by itself.
Expand Down
7 changes: 7 additions & 0 deletions t/01-marionette.t
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,13 @@ SKIP: {
foreach my $result ($firefox->resolve('localhost', type => 0, flags => 0)) {
ok($result =~ /^(127[.]0[.]0[.]1|::1)/smx, "\$firefox->resolve('localhost', type => 0, flags => 0) returned correctly:$result");
}
if ($major_version >= 78) {
my $test_dns_name = 'custom-weird.example.com';
my $ip_address = '127.0.0.84';
foreach my $result ($firefox->resolve_override($test_dns_name, $ip_address)->resolve($test_dns_name)) {
ok($result eq $ip_address, "\$firefox->resolve_override('$test_dns_name', '$ip_address') worked correctly:$result");
}
}
}
my $json;
if ($major_version < 50) {
Expand Down

0 comments on commit 8da6e80

Please sign in to comment.