WWW-Mechanize-Firefox

 view release on metacpan or  search on metacpan

t/55-basic-auth.t  view on Meta::CPAN

#!/usr/bin/perl -w
use strict;
use FindBin;

use lib './inc', '../inc';

use WWW::Mechanize::Firefox;

use Test::More;
plan skip_all => "presetting auth does not work yet";

if (! eval { require HTTP::Daemon; 1 }) {
    plan skip_all => "HTTP::Daemon required to test basic authentication";
    exit
};

# We want to be safe from non-resolving local host names
delete @ENV{qw(HTTP_PROXY http_proxy CGI_HTTP_PROXY)};

my $mech = eval { WWW::Mechanize::Firefox->new( 
        autodie => 0,
        #log => [qw[debug]],
    )
};

if (! $mech) {
    my $err = $@;
    plan skip_all => "Couldn't connect to MozRepl: $@";
    exit
};

my $user = 'foo';
my $pass = 'bar';

# Now start a fake webserver, fork, and connect to ourselves
my $server_pid = open SERVER, qq{"$^X" "$FindBin::Bin/401-server" $user $pass |}
  or die "Couldn't spawn fake server : $!";
sleep 1; # give the child some time
my $url = <SERVER>;
chomp $url;
die "Couldn't decipher host/port from '$url'"
    unless $url =~ m!^http://([^/]+)/!;
my $host = $1;
my $res;

# First try with an inline username/password
# FF asks for confirmation to navigate here :(
if (0) {
    my $pwd_url = $url;
    $pwd_url =~ s!^http://!http://$user:$pass\@!;
    $pwd_url .= 'thisshouldpass';
    diag "get $pwd_url";
    my $res = $mech->get( $pwd_url );
    diag $mech->content
        unless is($res->code, 200, "Request with inline credentials gives 200");
    like($mech->content, qr/user = 'foo' pass = 'bar'/, "Credentials are good");
};

# https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsILoginManager
my $passwordManager = $mech->repl->expr(<<'JS');
    Components.classes["@mozilla.org/login-manager;1"].
        getService(Components.interfaces.nsILoginManager)
JS
isa_ok $passwordManager, 'MozRepl::RemoteObject::Instance';

diag $url;
my $u = "$url";
$u =~ s!/$!!;
diag $u;

# https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsILoginInfo
my $nsiLoginInfo = $mech->repl->expr(<<"JS");
    var nsiL = new Components.Constructor("\@mozilla.org/login-manager/loginInfo;1",
        Components.interfaces.nsILoginInfo,
        "init");
    new nsiL("$u",
                       null, 'testing realm',
                       "$user", "$pass", "", "")



( run in 0.402 second using v1.01-cache-2.11-cpan-71847e10f99 )