1b39c5158SmillertBEGIN { chdir 't' if -d 't' }; 2b39c5158Smillert 3b39c5158Smillertuse strict; 4b39c5158Smillertuse lib '../lib'; 5b39c5158Smillert 6b39c5158Smillertuse Test::More 'no_plan'; 7b39c5158Smillert 8b39c5158Smillertuse Cwd qw[cwd]; 9b39c5158Smillertuse File::Basename qw[basename]; 106fb12b70Safresh1use File::Path qw[rmtree]; 11b39c5158Smillertuse Data::Dumper; 12b39c5158Smillert 13b39c5158Smillertuse_ok('File::Fetch'); 14b39c5158Smillert 15b39c5158Smillert### optionally set debugging ### 16b39c5158Smillert$File::Fetch::DEBUG = $File::Fetch::DEBUG = 1 if $ARGV[0]; 17b39c5158Smillert$IPC::Cmd::DEBUG = $IPC::Cmd::DEBUG = 1 if $ARGV[0]; 18b39c5158Smillert 196fb12b70Safresh1$File::Fetch::FORCEIPV4=1; 206fb12b70Safresh1 21b39c5158Smillertunless( $ENV{PERL_CORE} ) { 22b39c5158Smillert warn qq[ 23b39c5158Smillert 24b39c5158Smillert####################### NOTE ############################## 25b39c5158Smillert 26b39c5158SmillertSome of these tests assume you are connected to the 27b39c5158Smillertinternet. If you are not, or if certain protocols or hosts 28b39c5158Smillertare blocked and/or firewalled, these tests could fail due 29b39c5158Smillertto no fault of the module itself. 30b39c5158Smillert 31b39c5158Smillert########################################################### 32b39c5158Smillert 33b39c5158Smillert]; 34b39c5158Smillert 35b39c5158Smillert sleep 3 unless $File::Fetch::DEBUG; 36b39c5158Smillert} 37b39c5158Smillert 38b39c5158Smillert### show us the tools IPC::Cmd will use to run binary programs 39b39c5158Smillertif( $File::Fetch::DEBUG ) { 40b39c5158Smillert ### stupid 'used only once' warnings ;( 41b39c5158Smillert diag( "IPC::Run enabled: " . 42b39c5158Smillert $IPC::Cmd::USE_IPC_RUN || $IPC::Cmd::USE_IPC_RUN ); 43b39c5158Smillert diag( "IPC::Run available: " . IPC::Cmd->can_use_ipc_run ); 44b39c5158Smillert diag( "IPC::Run vesion: $IPC::Run::VERSION" ); 45b39c5158Smillert diag( "IPC::Open3 enabled: " . 46b39c5158Smillert $IPC::Cmd::USE_IPC_OPEN3 || $IPC::Cmd::USE_IPC_OPEN3 ); 47b39c5158Smillert diag( "IPC::Open3 available: " . IPC::Cmd->can_use_ipc_open3 ); 48b39c5158Smillert diag( "IPC::Open3 vesion: $IPC::Open3::VERSION" ); 49b39c5158Smillert} 50b39c5158Smillert 5191f110e0Safresh1### Heuristics 526fb12b70Safresh1my %heuristics = map { $_ => 1 } qw(http ftp rsync file git); 53b39c5158Smillert### _parse_uri tests 54b39c5158Smillert### these go on all platforms 55b39c5158Smillertmy @map = ( 56b39c5158Smillert { uri => 'ftp://cpan.org/pub/mirror/index.txt', 57b39c5158Smillert scheme => 'ftp', 58b39c5158Smillert host => 'cpan.org', 59b39c5158Smillert path => '/pub/mirror/', 60b39c5158Smillert file => 'index.txt' 61b39c5158Smillert }, 62b39c5158Smillert { uri => 'rsync://cpan.pair.com/CPAN/MIRRORING.FROM', 63b39c5158Smillert scheme => 'rsync', 64b39c5158Smillert host => 'cpan.pair.com', 65b39c5158Smillert path => '/CPAN/', 66b39c5158Smillert file => 'MIRRORING.FROM', 67b39c5158Smillert }, 685759b3d2Safresh1 { uri => 'git://github.com/Perl-Toolchain-Gang/file-fetch.git', 696fb12b70Safresh1 scheme => 'git', 706fb12b70Safresh1 host => 'github.com', 715759b3d2Safresh1 path => '/Perl-Toolchain-Gang/', 726fb12b70Safresh1 file => 'file-fetch.git', 736fb12b70Safresh1 }, 74b39c5158Smillert { uri => 'http://localhost/tmp/index.txt', 75b39c5158Smillert scheme => 'http', 76b39c5158Smillert host => 'localhost', # host is empty only on 'file://' 77b39c5158Smillert path => '/tmp/', 78b39c5158Smillert file => 'index.txt', 79b39c5158Smillert }, 80b39c5158Smillert 81b39c5158Smillert ### only test host part, the rest is OS dependant 82b39c5158Smillert { uri => 'file://localhost/tmp/index.txt', 83b39c5158Smillert host => '', # host should be empty on 'file://' 84b39c5158Smillert }, 85b39c5158Smillert); 86b39c5158Smillert 87b39c5158Smillert### these only if we're not on win32/vms 88b39c5158Smillertpush @map, ( 89b39c5158Smillert { uri => 'file:///usr/local/tmp/foo.txt', 90b39c5158Smillert scheme => 'file', 91b39c5158Smillert host => '', 92b39c5158Smillert path => '/usr/local/tmp/', 93b39c5158Smillert file => 'foo.txt', 94b39c5158Smillert }, 95b39c5158Smillert { uri => 'file://hostname/tmp/foo.txt', 96b39c5158Smillert scheme => 'file', 97b39c5158Smillert host => 'hostname', 98b39c5158Smillert path => '/tmp/', 99b39c5158Smillert file => 'foo.txt', 100b39c5158Smillert }, 101b39c5158Smillert) if not &File::Fetch::ON_WIN and not &File::Fetch::ON_VMS; 102b39c5158Smillert 103b39c5158Smillert### these only on win32 104b39c5158Smillertpush @map, ( 105b39c5158Smillert { uri => 'file:////hostname/share/tmp/foo.txt', 106b39c5158Smillert scheme => 'file', 107b39c5158Smillert host => 'hostname', 108b39c5158Smillert share => 'share', 109b39c5158Smillert path => '/tmp/', 110b39c5158Smillert file => 'foo.txt', 111b39c5158Smillert }, 112b39c5158Smillert { uri => 'file:///D:/tmp/foo.txt', 113b39c5158Smillert scheme => 'file', 114b39c5158Smillert host => '', 115b39c5158Smillert vol => 'D:', 116b39c5158Smillert path => '/tmp/', 117b39c5158Smillert file => 'foo.txt', 118b39c5158Smillert }, 119b39c5158Smillert { uri => 'file:///D|/tmp/foo.txt', 120b39c5158Smillert scheme => 'file', 121b39c5158Smillert host => '', 122b39c5158Smillert vol => 'D:', 123b39c5158Smillert path => '/tmp/', 124b39c5158Smillert file => 'foo.txt', 125b39c5158Smillert }, 126b39c5158Smillert) if &File::Fetch::ON_WIN; 127b39c5158Smillert 128b39c5158Smillert 129b39c5158Smillert### sanity tests 130b39c5158Smillert{ 131b39c5158Smillert no warnings; 132b39c5158Smillert like( $File::Fetch::USER_AGENT, qr/$File::Fetch::VERSION/, 133b39c5158Smillert "User agent contains version" ); 134b39c5158Smillert like( $File::Fetch::FROM_EMAIL, qr/@/, 135b39c5158Smillert q[Email contains '@'] ); 136b39c5158Smillert} 137b39c5158Smillert 138b39c5158Smillert### parse uri tests ### 139b39c5158Smillertfor my $entry (@map ) { 140b39c5158Smillert my $uri = $entry->{'uri'}; 141b39c5158Smillert 142b39c5158Smillert my $href = File::Fetch->_parse_uri( $uri ); 143b39c5158Smillert ok( $href, "Able to parse uri '$uri'" ); 144b39c5158Smillert 145b39c5158Smillert for my $key ( sort keys %$entry ) { 146b39c5158Smillert is( $href->{$key}, $entry->{$key}, 147b39c5158Smillert " '$key' ok ($entry->{$key}) for $uri"); 148b39c5158Smillert } 149b39c5158Smillert} 150b39c5158Smillert 151b39c5158Smillert### File::Fetch->new tests ### 152b39c5158Smillertfor my $entry (@map) { 153b39c5158Smillert my $ff = File::Fetch->new( uri => $entry->{uri} ); 154b39c5158Smillert 155b39c5158Smillert ok( $ff, "Object for uri '$entry->{uri}'" ); 156b39c5158Smillert isa_ok( $ff, "File::Fetch", " Object" ); 157b39c5158Smillert 158b39c5158Smillert for my $acc ( keys %$entry ) { 159b39c5158Smillert is( $ff->$acc(), $entry->{$acc}, 160b39c5158Smillert " Accessor '$acc' ok ($entry->{$acc})" ); 161b39c5158Smillert } 162b39c5158Smillert} 163b39c5158Smillert 164b39c5158Smillert### fetch() tests ### 165b39c5158Smillert 166b39c5158Smillert### file:// tests ### 167b39c5158Smillert{ 168b39c5158Smillert my $prefix = &File::Fetch::ON_UNIX ? 'file://' : 'file:///'; 169b39c5158Smillert my $uri = $prefix . cwd() .'/'. basename($0); 170b39c5158Smillert 171b39c5158Smillert for (qw[lwp lftp file]) { 172b39c5158Smillert _fetch_uri( file => $uri, $_ ); 173b39c5158Smillert } 174b39c5158Smillert} 175b39c5158Smillert 17691f110e0Safresh1### Heuristics 17791f110e0Safresh1{ 17891f110e0Safresh1 require IO::Socket::INET; 1795759b3d2Safresh1 my $sock = IO::Socket::INET->new( PeerAddr => 'mirror.bytemark.co.uk', PeerPort => 21, Timeout => 20 ) 18091f110e0Safresh1 or $heuristics{ftp} = 0; 18191f110e0Safresh1} 18291f110e0Safresh1 183b39c5158Smillert### ftp:// tests ### 1845759b3d2Safresh1{ my $uri = 'ftp://mirror.bytemark.co.uk/CPAN/index.html'; 1855759b3d2Safresh1 for (qw[wget curl lftp fetch ncftp]) { 186b39c5158Smillert 187b39c5158Smillert ### STUPID STUPID warnings ### 188b39c5158Smillert next if $_ eq 'ncftp' and $File::Fetch::FTP_PASSIVE 189b39c5158Smillert and $File::Fetch::FTP_PASSIVE; 190b39c5158Smillert 191b39c5158Smillert _fetch_uri( ftp => $uri, $_ ); 192b39c5158Smillert } 193b39c5158Smillert} 194b39c5158Smillert 19591f110e0Safresh1### Heuristics 19691f110e0Safresh1{ 19791f110e0Safresh1 require IO::Socket::INET; 1985759b3d2Safresh1 my $sock = IO::Socket::INET->new( PeerAddr => 'httpbin.org', PeerPort => 80, Timeout => 20 ) 19991f110e0Safresh1 or $heuristics{http} = 0; 20091f110e0Safresh1} 20191f110e0Safresh1 202b39c5158Smillert### http:// tests ### 2035759b3d2Safresh1{ for my $uri ( 'http://httpbin.org/html', 2045759b3d2Safresh1 'http://httpbin.org/response-headers?q=1', 2055759b3d2Safresh1 'http://httpbin.org/response-headers?q=1&y=2', 2065759b3d2Safresh1 #'http://www.cpan.org/index.html?q=1&y=2', 2075759b3d2Safresh1 #'http://user:passwd@httpbin.org/basic-auth/user/passwd', 208b39c5158Smillert ) { 209898184e3Ssthen for (qw[lwp httptiny wget curl lftp fetch lynx httplite iosock]) { 210b39c5158Smillert _fetch_uri( http => $uri, $_ ); 211b39c5158Smillert } 212b39c5158Smillert } 213b39c5158Smillert} 214b39c5158Smillert 21591f110e0Safresh1### Heuristics 21691f110e0Safresh1{ 21791f110e0Safresh1 require IO::Socket::INET; 21891f110e0Safresh1 my $sock = IO::Socket::INET->new( PeerAddr => 'cpan.pair.com', PeerPort => 873, Timeout => 20 ) 21991f110e0Safresh1 or $heuristics{rsync} = 0; 22091f110e0Safresh1} 22191f110e0Safresh1 222b39c5158Smillert### rsync:// tests ### 223b39c5158Smillert{ my $uri = 'rsync://cpan.pair.com/CPAN/MIRRORING.FROM'; 224b39c5158Smillert 225b39c5158Smillert for (qw[rsync]) { 226b39c5158Smillert _fetch_uri( rsync => $uri, $_ ); 227b39c5158Smillert } 228b39c5158Smillert} 229b39c5158Smillert 2306fb12b70Safresh1### Heuristics 2316fb12b70Safresh1{ 2326fb12b70Safresh1 require IO::Socket::INET; 2336fb12b70Safresh1 my $sock = IO::Socket::INET->new( PeerAddr => 'github.com', PeerPort => 9418, Timeout => 20 ) 2346fb12b70Safresh1 or $heuristics{git} = 0; 2356fb12b70Safresh1} 2366fb12b70Safresh1 2376fb12b70Safresh1### git:// tests ### 238*256a93a4Safresh1{ my $uri = 'https://github.com/Perl-Toolchain-Gang/file-fetch.git'; 2396fb12b70Safresh1 2406fb12b70Safresh1 for (qw[git]) { 2415759b3d2Safresh1 local $ENV{GIT_CONFIG_NOSYSTEM} = 1; 2425759b3d2Safresh1 local $ENV{XDG_CONFIG_HOME}; 2435759b3d2Safresh1 local $ENV{HOME}; 2446fb12b70Safresh1 _fetch_uri( git => $uri, $_ ); 2456fb12b70Safresh1 } 2466fb12b70Safresh1} 2476fb12b70Safresh1 248b39c5158Smillertsub _fetch_uri { 249b39c5158Smillert my $type = shift; 250b39c5158Smillert my $uri = shift; 251b39c5158Smillert my $method = shift or return; 252b39c5158Smillert 253b39c5158Smillert SKIP: { 254b39c5158Smillert skip "'$method' fetching tests disabled under perl core", 4 255b39c5158Smillert if $ENV{PERL_CORE}; 256b39c5158Smillert 25791f110e0Safresh1 skip "'$type' fetching tests disabled due to heuristic failure", 4 25891f110e0Safresh1 unless $heuristics{ $type }; 25991f110e0Safresh1 260b39c5158Smillert ### stupid warnings ### 261b39c5158Smillert $File::Fetch::METHODS = 262b39c5158Smillert $File::Fetch::METHODS = { $type => [$method] }; 263b39c5158Smillert 264b39c5158Smillert ### fetch regularly 265b39c5158Smillert my $ff = File::Fetch->new( uri => $uri ); 266b39c5158Smillert 267b39c5158Smillert ok( $ff, "FF object for $uri (fetch with $method)" ); 268b39c5158Smillert 269b39c5158Smillert for my $to ( 'tmp', do { \my $o } ) { SKIP: { 270b39c5158Smillert 271b39c5158Smillert 2726fb12b70Safresh1 my $how = ref $to && $type ne 'git' ? 'slurp' : 'file'; 273b39c5158Smillert my $skip = ref $to ? 4 : 3; 274b39c5158Smillert 275b39c5158Smillert ok( 1, " Fetching '$uri' in $how mode" ); 276b39c5158Smillert 277b39c5158Smillert my $file = $ff->fetch( to => $to ); 278b39c5158Smillert 279b39c5158Smillert skip "You do not have '$method' installed/available", $skip 280b39c5158Smillert if $File::Fetch::METHOD_FAIL->{$method} && 281b39c5158Smillert $File::Fetch::METHOD_FAIL->{$method}; 282b39c5158Smillert 283b39c5158Smillert ### if the file wasn't fetched, it may be a network/firewall issue 284b39c5158Smillert skip "Fetch failed; no network connectivity for '$type'?", $skip 285b39c5158Smillert unless $file; 286b39c5158Smillert 287b39c5158Smillert ok( $file, " File ($file) fetched with $method ($uri)" ); 288b39c5158Smillert 289b39c5158Smillert ### check we got some contents if we were meant to slurp 2906fb12b70Safresh1 if( ref $to && $type ne 'git' ) { 291b39c5158Smillert ok( $$to, " Contents slurped" ); 292b39c5158Smillert } 293b39c5158Smillert 294b39c5158Smillert ok( $file && -s $file, 295b39c5158Smillert " File has size" ); 296b39c5158Smillert is( $file && basename($file), $ff->output_file, 297b39c5158Smillert " File has expected name" ); 298b39c5158Smillert 2996fb12b70Safresh1 rmtree $file; 300b39c5158Smillert }} 301b39c5158Smillert } 302b39c5158Smillert} 303b39c5158Smillert 304b39c5158Smillert 305b39c5158Smillert 306b39c5158Smillert 307b39c5158Smillert 308b39c5158Smillert 309b39c5158Smillert 310b39c5158Smillert 311