Skip to content

Commit

Permalink
Merge pull request #47 from Varadinsky/new_dump_tests
Browse files Browse the repository at this point in the history
added tests for all dump methods
  • Loading branch information
oalders committed Sep 8, 2016
2 parents 8319eb0 + a46d3db commit 5b1b301
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 17 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ my %WriteMakefileArgs = (
"LWP" => 0,
"LWP::Simple" => 0,
"Test::More" => 0,
"Test::Output" => 0,
"Test::RequiresInternet" => 0,
"URI" => 0,
"URI::Escape" => 0,
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ on 'test' => sub {
requires "LWP" => "0";
requires "LWP::Simple" => "0";
requires "Test::More" => "0";
requires "Test::Output" => "0";
requires "Test::RequiresInternet" => "0";
requires "URI" => "0";
requires "URI::Escape" => "0";
Expand Down
123 changes: 106 additions & 17 deletions t/dump.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,120 @@

use warnings;
use strict;
use Test::More tests => 5;
use Test::More tests => 6;
use Test::Output;
use URI::file;
use File::Temp qw/tempdir/;
use File::Spec;



BEGIN {
use_ok( 'WWW::Mechanize' );
}

my $mech = WWW::Mechanize->new( cookie_jar => undef );
isa_ok( $mech, 'WWW::Mechanize' );

my $uri = URI::file->new_abs( 't/find_inputs.html' )->as_string;
my $dir = tempdir( CLEANUP => 1 );

subtest "dump_headers", sub {
plan tests => 5;
my $mech = create_mech('t/find_inputs.html');
my $tmp_name = File::Spec->catfile($dir, 'headers.tmp');

$mech->dump_headers($tmp_name);
ok( -e $tmp_name, 'Dump file created');

fh_test($mech, 'dump_headers', qr/Content-Length/);
};


subtest "dump_links test", \&dump_tests, 'dump_links', 't/find_link.html', <<'EXPECTED';
http://www.drphil.com/
HTTP://WWW.UPCASE.COM/
styles.css
http://blargle.com/
http://a.cpan.org/
http://b.cpan.org/
foo.html
bar.html
http://c.cpan.org/
http://d.cpan.org/
http://www.msnbc.com/
http://www.oreilly.com/
http://www.cnn.com/
http://www.bbc.co.uk/
http://www.msnbc.com/
http://www.cnn.com/
http://www.bbc.co.uk/
/cgi-bin/MT/mt.cgi
http://www.msnbc.com/area
http://www.cnn.com/area
http://www.cpan.org/area
http://nowhere.org/
http://nowhere.org/padded
blongo.html
EXPECTED

subtest "dump_images test", \&dump_tests, 'dump_images', 't/image-parse.html', <<'EXPECTED';
wango.jpg
bongo.gif
linked.gif
EXPECTED

subtest "dump_forms test", \&dump_tests, 'dump_forms', 't/form_with_fields.html', <<'EXPECTED';
POST http://localhost/ (multipart/form-data) [1st_form]
1a= (text)
1b= (text)
submit=Submit (submit)
POST http://localhost/ [2nd_form]
opt[2]= (text)
1b= (text)
submit=Submit (submit)
EXPECTED

$mech->get( $uri );
ok( $mech->success, "Fetched $uri" ) or die q{Can't get test page};
my $fn = 'headers.tmp';
$mech->dump_headers($fn);
ok( -e $fn );
unlink('headers.tmp');
subtest "dump_text test", \&dump_tests, 'dump_text', 't/image-parse.html', <<'EXPECTED';
Testing image extractionblargle And now, the dreaded wango CNN BBC Blongo!
EXPECTED

my $content;
open my $fh, '>', \$content;
$mech->dump_headers( $fh );
like( $content, qr/Content-Length/ );
close $fh;
sub dump_tests {
my ($method, $fp, $expected) = @_;
my $mech = create_mech($fp);

END {
unlink('headers.tmp');
fh_test($mech, $method, $expected);
};

sub create_mech {

my $filepath = shift;
my $mech = WWW::Mechanize->new( cookie_jar => undef );
isa_ok( $mech, 'WWW::Mechanize' );

my $uri = URI::file->new_abs( $filepath )->as_string;

$mech->get( $uri );
ok( $mech->success, "Fetched $uri" ) or die q{Can't get test page};

return $mech;
}


sub fh_test {
my ($mech, $method, $expected) = @_;
my ($content);
open my $fh, '>', \$content or die ($!);

$mech->$method( $fh );

close $fh;

if (ref $expected eq 'Regexp') {
like( $content, $expected, 'Dump has valid values');
stdout_like( sub {$mech->$method()}, $expected, 'Valid STDOUT');
} else {
is( $content, $expected, 'Dump has valid values');
stdout_is ( sub {$mech->$method()}, $expected, 'Valid STDOUT');
}
}

0 comments on commit 5b1b301

Please sign in to comment.