| 1 |
#!/usr/bin/perl -w |
| 2 |
use strict; |
| 3 |
use FindBin; |
| 4 |
|
| 5 |
use lib './inc'; |
| 6 |
use IO::Catch; |
| 7 |
our ( $_STDOUT_, $_STDERR_ ); |
| 8 |
use URI; |
| 9 |
use Test::HTTP::LocalServer; |
| 10 |
|
| 11 |
# pre-5.8.0's warns aren't caught by a tied STDERR. |
| 12 |
tie *STDOUT, 'IO::Catch', '_STDOUT_' or die $!; |
| 13 |
|
| 14 |
# Disable all ReadLine functionality |
| 15 |
$ENV{PERL_RL} = 0; |
| 16 |
|
| 17 |
use Test::More tests => 4; |
| 18 |
|
| 19 |
use WWW::Mechanize::Shell; |
| 20 |
|
| 21 |
delete @ENV{qw(HTTP_PROXY http_proxy CGI_HTTP_PROXY)}; |
| 22 |
|
| 23 |
my $server = Test::HTTP::LocalServer->spawn(); |
| 24 |
|
| 25 |
my $user = 'foo'; |
| 26 |
my $pass = 'bar'; |
| 27 |
|
| 28 |
my $url = URI->new( $server->basic_auth($user => $pass)); |
| 29 |
my $host = $url->host; |
| 30 |
|
| 31 |
my $s = WWW::Mechanize::Shell->new( 'test', rcfile => undef, warnings => undef ); |
| 32 |
|
| 33 |
# Try without credentials: |
| 34 |
my $bare_url = $url; |
| 35 |
diag "get $bare_url"; |
| 36 |
$s->cmd( "get $bare_url" ); |
| 37 |
|
| 38 |
my $code = $s->agent->response->code; |
| 39 |
my $got_url = $s->agent->uri; |
| 40 |
|
| 41 |
if (! is $code, 401, "Request without credentials gives 401") { |
| 42 |
diag "Page location : " . $s->agent->uri; |
| 43 |
}; |
| 44 |
|
| 45 |
# Now try the shell command for authentication with bad credentials |
| 46 |
$s->cmd( "auth x$user x$pass" ); |
| 47 |
$bare_url = $url; |
| 48 |
diag "get $bare_url"; |
| 49 |
eval { |
| 50 |
$s->cmd( "get $bare_url" ); |
| 51 |
}; |
| 52 |
is $s->agent->res->code, 401, "Wrong password still results in a 401"; |
| 53 |
like $@, qr/Auth Required/, "We die because of that"; |
| 54 |
|
| 55 |
# Now try the shell command for authentication with correct credentials |
| 56 |
$s->cmd( "auth $user $pass" ); |
| 57 |
$s->cmd( "get $bare_url" ); |
| 58 |
is $s->agent->res->code, 200, "Right password results in 200"; |
| 59 |
|
| 60 |
#diag "Shutting down test server at $url"; |
| 61 |
$server->stop; |
| 62 |
|