#!perl use warnings; use strict; use Test::More tests => 19 ; use URI; BEGIN { use_ok( 'WWW::Mechanize' ); delete @ENV{ qw( http_proxy HTTP_PROXY ) }; delete @ENV{ qw( IFS CDPATH ENV BASH_ENV ) }; } sub data_url { my $u = new URI 'data:'; $u->media_type('text/html'); $u->data(shift); $u } my $mech = WWW::Mechanize->new; is $mech->history_length, 0,'history_length\'s initial retval'; my $back_and_forth; $mech->add_handler(forward => sub { $back_and_forth .= '-push' }); $mech->add_handler(back => sub { $back_and_forth .= '-pop' }); $mech->get(data_url 'first page'); $mech->get(data_url 'second page'); $mech->get(data_url 'third page'); is $mech->history_length, 3, 'history_length after fetching pages'; $mech->back; is $mech->history_length, 3, 'history_length after going back'; is $mech->title, 'second page', 'back'; $mech->back; is $mech->title, 'first page', 'back again'; $mech->forward; is $mech->title, 'second page', 'forward'; $mech->forward; is $mech->title, 'third page', 'forward again'; $mech->back; is $mech->title, 'second page', 'back yet again'; $mech->get(data_url 'new page'); is $mech->history_length, 3, 'history_length after a page fetch erases fwd history'; $mech->forward; is $mech->title, 'new page', '->request erases the forward stack'; $mech->back; is $mech->title, 'second page', 'Does ->forward at the end of history mess things up?'; is $back_and_forth, '-push-push-pop-pop-push-push-pop-push-pop', 'back/forward handlers'; $mech->clear_history; $mech->back; is $mech->history_length, 1, 'make sure back messes nothing up when you can\'t go back'; # state info stuff $mech->get(data_url 'third page'); my @scratch; sub record_state { push @scratch, [$mech->title, $mech->state_info]; } $mech->push_state_info(37); record_state; $mech->push_state_info(43); record_state; $mech->get(data_url 'fourth page'); record_state; $mech->push_state_info(\'phoo'); record_state; $mech->back, record_state for 1..5; $mech->forward, record_state for 1..6; $mech->back; $mech->push_state_info(\'barr'); # make sure it erases fwd history $mech->forward; record_state; is_deeply \@scratch, [ ['third page', 37], ['third page', 43], ['fourth page', undef], ['fourth page', \'phoo'], ['fourth page', undef], ['third page', 43], ['third page', 37], ['third page', undef], ['second page', undef], ['third page', undef], ['third page', 37], ['third page', 43], ['fourth page', undef], ['fourth page', \'phoo'], ['fourth page', \'phoo'], # can't go forward beyond the last state ['fourth page', \'barr'], ], '(push_)state_info'; { my $mech = new WWW::Mechanize; my $r; $mech->add_handler($_ => sub { $r = shift }) for qw/back forward/; $mech->get('data:text/plain,'); is $r, undef, 'response passed to fwd handler the first time'; $mech->get('data:text/plain,page%202'); isa_ok $r, 'HTTP::Response', '2nd fwd handler response object'; is $r->request->uri, 'data:text/plain,', 'response object is from before the page stack manipulation'; $mech->back; is $r->request->uri, 'data:text/plain,page%202', 'response passed to back handler'; }