File: back.t

package info (click to toggle)
libwww-mechanize-perl 1.18-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 488 kB
  • ctags: 109
  • sloc: perl: 2,315; makefile: 39
file content (172 lines) | stat: -rw-r--r-- 4,369 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!perl

use strict;
use Test::More tests => 38;
use lib 't/local';
use LocalServer;
use HTTP::Daemon;
use HTTP::Response;

=head1 NAME

=head1 SYNOPSIS

This tests Mech's Back "button". Tests were converted from t/live/back.t,
and subsequently enriched to deal with RT ticket #8109.

=cut

BEGIN {
    use_ok( 'WWW::Mechanize' );
    delete @ENV{ qw( http_proxy HTTP_PROXY ) };
    delete @ENV{ qw( IFS CDPATH ENV BASH_ENV ) };

}

my $mech = WWW::Mechanize->new(cookie_jar => {});
isa_ok( $mech, "WWW::Mechanize" );
ok(defined($mech->cookie_jar()),
   'this $mech starts with a cookie jar');

my $html = <<'HTML';
<html>
<head><title>%s</title></head>
<body>Whatever.
<a href="images/">Images</a>
<a href="/scripts">Scripts</a>
<a href="/ports/">Ports</a>
<a href="modules/">Modules</a>
<form action="/search.cgi">
<input type="text" name="q">
<input type="submit">
</form>
</body>
</html>
HTML

my $server = LocalServer->spawn( html => $html );
isa_ok( $server, "LocalServer" );

$mech->get($server->url);
ok( $mech->success, 'Fetched OK' );

my $first_base = $mech->base;
my $title = $mech->title;

$mech->follow_link( n=>2 );
ok( $mech->success, 'Followed OK' );

$mech->back();
is( $mech->base, $first_base, "Did the base get set back?" );
is( $mech->title, $title, "Title set back?" );

$mech->follow( "Images" );
ok( $mech->success, 'Followed OK' );

$mech->back();
is( $mech->base, $first_base, "Did the base get set back?" );
is( $mech->title, $title, "Title set back?" );

is( scalar @{$mech->{page_stack}}, 0, "Pre-search check" );
$mech->submit_form(
    fields => { 'q' => "perl" },
);
ok( $mech->success, "Searched for Perl" );
like( $mech->title, qr/search.cgi/, "Right page title" );
is( scalar @{$mech->{page_stack}}, 1, "POST is in the stack" );

$mech->head( $server->url );
ok( $mech->success, "HEAD succeeded" );
is( scalar @{$mech->{page_stack}}, 1, "HEAD is not in the stack" );

$mech->back();
ok( $mech->success, "Back" );
is( $mech->base, $first_base, "Did the base get set back?" );
is( $mech->title, $title, "Title set back?" );
is( scalar @{$mech->{page_stack}}, 0, "Post-search check" );

=head2 Back and misc. internal fields

RT ticket #8109 reported that back() is broken after reload(), and
that the cookie_jar was also damaged by back(). We test for that:
reload() should not alter the back() stack, and the cookie jar should
not be versioned (once a cookie is set, hitting the back button in a
browser does not cause it to go away).

=cut

$mech->follow( "Images" );
$mech->reload();
$mech->back();
is($mech->title, $title, "reload() does not push page to stack" );

ok(defined($mech->cookie_jar()),
   '$mech still has a cookie jar after a number of back()');

# Now some other weird stuff. Start with a fresh history by recreating
# $mech.
SKIP: {
    eval "use Test::Memory::Cycle";
    skip "Test::Memory::Cycle not installed", 1 if $@;

    memory_cycle_ok( $mech, "No memory cycles found" );
}

$mech = WWW::Mechanize->new();
isa_ok( $mech, "WWW::Mechanize" );
$mech->get( $server->url );
ok( $mech->success, 'Got root URL' );

my @links = qw(
    /scripts
    /ports/
    modules/
);

is( scalar @{$mech->{page_stack}}, 0, "Pre-404 check" );

my $server404 = HTTP::Daemon->new or die;
my $server404url = $server404->url;

die "Cannot fork" if (! defined (my $pid404 = fork()));
END {
    local $?;
    kill KILL => $pid404; # Extreme prejudice intended, because we do not
    # want the global cleanup to be done twice.
}

if (! $pid404) { # Fake HTTP server code: a true 404-compliant server!
    while ( my $c = $server404->accept() ) {
        while ( $c->get_request() ) {
            $c->send_response( new HTTP::Response(404) );
            $c->close();
        }
    }
}

$mech->get($server404url);
is( $mech->status, 404 , "404 check");

is( scalar @{$mech->{page_stack}}, 1, "Even 404s get on the stack" );

$mech->back();
is( $mech->uri, $server->url, "Back from the 404" );
is( scalar @{$mech->{page_stack}}, 0, "Post-404 check" );

for my $link ( @links ) {
    $mech->get( $link );
    warn $mech->status() if (! $mech->success());
    is( $mech->status, 200, "Get $link" );

    $mech->back();
    is( $mech->uri, $server->url, "Back from $link" );
}

SKIP: {
    eval "use Test::Memory::Cycle";
    skip "Test::Memory::Cycle not installed", 1 if $@;

    memory_cycle_ok( $mech, "No memory cycles found" );
}