5 use Test::More tests => 38;
16 This tests Mech's Back "button". Tests were converted from t/live/back.t,
17 and subsequently enriched to deal with RT ticket #8109.
22 delete @ENV{ grep { lc eq 'http_proxy' } keys %ENV };
23 delete @ENV{ qw( IFS CDPATH ENV BASH_ENV ) };
24 use_ok( 'WWW::Mechanize' );
27 my $mech = WWW::Mechanize->new(cookie_jar => {});
28 isa_ok( $mech, 'WWW::Mechanize' );
29 ok(defined($mech->cookie_jar()),
30 'this $mech starts with a cookie jar');
34 <head><title>%s</title></head>
36 <a href="images/">Images</a>
37 <a href="/scripts">Scripts</a>
38 <a href="/ports/">Ports</a>
39 <a href="modules/">Modules</a>
40 <form action="/search.cgi">
41 <input type="text" name="q">
48 my $server = LocalServer->spawn( html => $html );
49 isa_ok( $server, 'LocalServer' );
51 $mech->get($server->url);
52 ok( $mech->success, 'Fetched OK' );
54 my $first_base = $mech->base;
55 my $title = $mech->title;
57 $mech->follow_link( n=>2 );
58 ok( $mech->success, 'Followed OK' );
61 is( $mech->base, $first_base, 'Did the base get set back?' );
62 is( $mech->title, $title, 'Title set back?' );
64 $mech->follow_link( text => 'Images' );
65 ok( $mech->success, 'Followed OK' );
68 is( $mech->base, $first_base, 'Did the base get set back?' );
69 is( $mech->title, $title, 'Title set back?' );
71 is( scalar @{$mech->{page_stack}}, 0, 'Pre-search check' );
73 fields => { 'q' => 'perl' },
75 ok( $mech->success, 'Searched for Perl' );
76 like( $mech->title, qr/search.cgi/, 'Right page title' );
77 is( scalar @{$mech->{page_stack}}, 1, 'POST is in the stack' );
79 $mech->head( $server->url );
80 ok( $mech->success, 'HEAD succeeded' );
81 is( scalar @{$mech->{page_stack}}, 1, 'HEAD is not in the stack' );
84 ok( $mech->success, 'Back' );
85 is( $mech->base, $first_base, 'Did the base get set back?' );
86 is( $mech->title, $title, 'Title set back?' );
87 is( scalar @{$mech->{page_stack}}, 0, 'Post-search check' );
89 =head2 Back and misc. internal fields
91 RT ticket #8109 reported that back() is broken after reload(), and
92 that the cookie_jar was also damaged by back(). We test for that:
93 reload() should not alter the back() stack, and the cookie jar should
94 not be versioned (once a cookie is set, hitting the back button in a
95 browser does not cause it to go away).
99 $mech->follow_link( text => 'Images' );
102 is($mech->title, $title, 'reload() does not push page to stack' );
104 ok(defined($mech->cookie_jar()),
105 '$mech still has a cookie jar after a number of back()');
107 # Now some other weird stuff. Start with a fresh history by recreating
110 eval 'use Test::Memory::Cycle';
111 skip 'Test::Memory::Cycle not installed', 1 if $@;
113 memory_cycle_ok( $mech, 'No memory cycles found' );
116 $mech = WWW::Mechanize->new();
117 isa_ok( $mech, 'WWW::Mechanize' );
118 $mech->get( $server->url );
119 ok( $mech->success, 'Got root URL' );
127 is( scalar @{$mech->{page_stack}}, 0, 'Pre-404 check' );
129 my $server404 = HTTP::Daemon->new(LocalAddr => 'localhost') or die;
130 my $server404url = $server404->url;
132 die 'Cannot fork' if (! defined (my $pid404 = fork()));
135 kill KILL => $pid404; # Extreme prejudice intended, because we do not
136 # want the global cleanup to be done twice.
139 if (! $pid404) { # Fake HTTP server code: a true 404-compliant server!
140 while ( my $c = $server404->accept() ) {
141 while ( $c->get_request() ) {
142 $c->send_response( new HTTP::Response(404) );
148 $mech->get($server404url);
149 is( $mech->status, 404 , '404 check') or
150 diag( qq{\$server404url=$server404url\n\$mech->content="}, $mech->content, qq{"\n} );
152 is( scalar @{$mech->{page_stack}}, 1, 'Even 404s get on the stack' );
155 is( $mech->uri, $server->url, 'Back from the 404' );
156 is( scalar @{$mech->{page_stack}}, 0, 'Post-404 check' );
158 for my $link ( @links ) {
160 warn $mech->status() if (! $mech->success());
161 is( $mech->status, 200, "Get $link" );
164 is( $mech->uri, $server->url, "Back from $link" );
168 eval 'use Test::Memory::Cycle';
169 skip 'Test::Memory::Cycle not installed', 1 if $@;
171 memory_cycle_ok( $mech, 'No memory cycles found' );