diff --git a/lib/Firefox/Marionette.pm b/lib/Firefox/Marionette.pm index f835600..bbacb45 100644 --- a/lib/Firefox/Marionette.pm +++ b/lib/Firefox/Marionette.pm @@ -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'); @@ -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 to aid in chaining methods. + =head2 restart restarts the browser. After the restart, L should be restored. The same profile settings should be applied, but the current state of the browser (such as the L will be reset (like after a normal browser restart). This method is primarily intended for use by the L method. Not sure if this is useful by itself. diff --git a/t/01-marionette.t b/t/01-marionette.t index 79a2913..138686e 100755 --- a/t/01-marionette.t +++ b/t/01-marionette.t @@ -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) {