Skip to content

Commit 5639cef

Browse files
simbabqueoalders
authored andcommitted
mech-dump errors when a request failed #292
Note the output capturing seems to not work properly on Windows for Perls 5.14 and possibly 5.16. While I found a source claiming the weird error message 'Can't spawn "cmd.exe": No such file or directory' captured from STDOUT after calling system() has to do with $? being -1 and only occurs in 5.14 (and maybe later, but had subsequently been fixed) I have not found anything in any perldeltas following 5.14. I have decided to simply exclude this test on these Perl versions until we know more.
1 parent 9b21afa commit 5639cef

File tree

3 files changed

+74
-36
lines changed

3 files changed

+74
-36
lines changed

Changes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Revision history for WWW::Mechanize
44
[ENHANCEMENTS]
55
- Add autocheck() to enable or disable autochecking at run time in
66
addition to setting it at object creation (GH#232) (Julien Fiegehenn)
7+
- mech_dump now errors appropriately when it cannot open a URL or file
8+
instead of claiming it has the wrong MIME type (GH#292) (Julien Fiegehenn)
79

810
2.13 2022-07-29 09:44:46Z
911
[ENHANCEMENTS]

script/mech-dump

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,19 @@ foreach my $uri (@uris) {
113113
}
114114

115115
my $response = $mech->get( $uri );
116-
if (!$response->is_success and defined ($response->www_authenticate)) {
117-
if (!defined $user or !defined $pass) {
118-
die("Page requires username and password, but none specified.\n");
116+
if ( !$response->is_success ) {
117+
if ( defined( $response->www_authenticate ) ) {
118+
if ( !defined $user or !defined $pass ) {
119+
die("Page requires username and password, but none specified.\n");
119120
}
120-
$mech->credentials($user,$pass);
121-
$response = $mech->get( $uri );
122-
$response->is_success or die "Can't fetch $uri with username and password\n", $response->status_line, "\n";
121+
$mech->credentials( $user, $pass );
122+
$response = $mech->get($uri);
123+
$response->is_success
124+
or die "Can't fetch $uri with username and password\n",
125+
$response->status_line, "\n";
126+
} else {
127+
die "$uri returns status ", $response->code, "\n";
128+
}
123129
}
124130

125131
unless ($no_ct_check) {

t/mech-dump/mech-dump.t

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,37 @@ use warnings;
44
use strict;
55

66
use Test::More;
7-
use File::Spec ();
8-
use LWP ();
7+
use Test::Output qw( output_like );
8+
use File::Spec ();
9+
use LWP ();
910

1011
BEGIN {
11-
delete @ENV{ qw( IFS CDPATH ENV BASH_ENV PATH ) };
12+
delete @ENV{qw( IFS CDPATH ENV BASH_ENV PATH )};
1213
}
1314

14-
plan skip_all => 'Not installing mech-dump' if -e File::Spec->catfile( qw( t SKIP-MECH-DUMP ) );
15-
plan tests => 4;
15+
plan skip_all => 'Not installing mech-dump'
16+
if -e File::Spec->catfile(qw( t SKIP-MECH-DUMP ));
1617

17-
my $exe = File::Spec->catfile( qw( script mech-dump ) );
18+
my $exe = File::Spec->catfile(qw( script mech-dump ));
1819
if ( $^O eq 'VMS' ) {
19-
$exe = qq[mcr $^X -Ilib $exe];
20+
$exe = qq[mcr $^X -Ilib $exe];
2021
}
2122

23+
my $perl;
24+
$perl = $1 if $^X =~ /^(.+)$/;
25+
26+
subtest 'Success' => sub {
27+
2228
# Simply use a file: uri instead of the filename to make this test
2329
# more independent of what URI::* thinks.
24-
my $source = 'file:t/google.html t/find_inputs.html t/html_file.txt';
30+
my $source = 'file:t/google.html t/find_inputs.html t/html_file.txt';
2531

26-
my $perl;
27-
$perl = $1 if $^X =~ /^(.+)$/;
28-
my $command = "$perl -Ilib $exe --forms --images --links $source";
32+
my $command = "$perl -Ilib $exe --forms --images --links $source";
2933

30-
my $actual = `$command`;
34+
my $actual = `$command`;
3135

32-
my $expected;
33-
if ( $LWP::VERSION < 5.800 ) {
36+
my $expected;
37+
if ( $LWP::VERSION < 5.800 ) {
3438
$expected = <<'EOF';
3539
GET file:/target-page [bob-the-form]
3640
hl=en (hidden)
@@ -78,7 +82,8 @@ POST http://localhost/ [3rd_form]
7882
GET http://localhost [text-form]
7983
one= (text)
8084
EOF
81-
} else {
85+
}
86+
else {
8287
$expected = <<'EOF';
8388
GET file:/target-page [bob-the-form]
8489
hl=en (hidden readonly)
@@ -126,18 +131,43 @@ POST http://localhost/ [3rd_form]
126131
GET http://localhost [text-form]
127132
one= (text)
128133
EOF
129-
}
130-
131-
my @actual = split /\s*\n/, $actual;
132-
my @expected = split /\s*\n/, $expected;
133-
134-
# First line is platform-dependent, so handle it accordingly.
135-
shift @expected;
136-
my $first = shift @actual;
137-
like( $first, qr/^GET file:.*\/target-page \[bob-the-form\]/, 'First line matches' );
138-
139-
cmp_ok( @expected, '>', 0, 'Still some expected' );
140-
cmp_ok( @actual, '>', 0, 'Still some actual' );
141-
142-
is_deeply( \@actual, \@expected, 'Rest of the lines match' );
143-
134+
}
135+
136+
my @actual = split /\s*\n/, $actual;
137+
my @expected = split /\s*\n/, $expected;
138+
139+
# First line is platform-dependent, so handle it accordingly.
140+
shift @expected;
141+
my $first = shift @actual;
142+
like( $first,
143+
qr/^GET file:.*\/target-page \[bob-the-form\]/,
144+
'First line matches' );
145+
146+
cmp_ok( @expected, '>', 0, 'Still some expected' );
147+
cmp_ok( @actual, '>', 0, 'Still some actual' );
148+
149+
is_deeply( \@actual, \@expected, 'Rest of the lines match' );
150+
};
151+
152+
subtest 'Local file not found' => sub {
153+
SKIP: {
154+
# See https://stackoverflow.com/a/32054866/1331451
155+
skip 'capturing output from system() is broken in 5.14 and 5.16 on Windows', 1
156+
if $^O eq 'MSWin32' && ( $] >= 5.014 && $] < 5.017 );
157+
158+
# The following file should not exist.
159+
my $source = 'file:not_found.404';
160+
161+
my $command = "$perl -Ilib $exe $source";
162+
163+
output_like(
164+
sub {
165+
system $command;
166+
},
167+
undef,
168+
qr/file:not_found.404 returns status 404/,
169+
'Errors when a local file is not found' );
170+
};
171+
};
172+
173+
done_testing;

0 commit comments

Comments
 (0)