File: history.t

package info (click to toggle)
libwww-mechanize-perl 2.03-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 988 kB
  • sloc: perl: 4,088; makefile: 6
file content (167 lines) | stat: -rw-r--r-- 4,580 bytes parent folder | download
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
#!perl

use warnings;
use strict;

use lib qw( t/local );

use LocalServer ();
use Path::Tiny qw( path );
use Test::Deep;
use Test::Fatal;
use Test::More;
use URI::file ();
use WWW::Mechanize ();

BEGIN {
    delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}
        ;    # Placates taint-unsafe Cwd.pm in 5.6.1
    use_ok('WWW::Mechanize');
}

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

    my $uri = URI::file->new_abs('t/history_1.html')->as_string;

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

    is( $mech->history_count, 1, "... and it was recorded in the history" );
    cmp_deeply(
        $mech->history(0),
        {
            req => isa('HTTP::Request'),
            res => all(
                isa('HTTP::Response'),
                methods( 'content' => re(qr/Testing the history_1/) ),
            ),
        },
        "... and the first history item is of the correct format"
    );
    $mech->follow_link( n => 1 );

    is( $mech->history_count, 2, "... and it was recorded in the history" );
    cmp_deeply(
        $mech->history(0),
        {
            req => isa('HTTP::Request'),
            res => all(
                isa('HTTP::Response'),
                methods( 'content' => re(qr/Testing the history_2/) ),
            ),
        },
        "... and the second history item is of the correct format"
    );

    ok(
        $mech->submit_form( form_name => "get_form" ),
        "Submit form using 'get' method"
    );

    is( $mech->history_count, 3, "... and it was recorded in the history" );
    cmp_deeply(
        $mech->history(0),
        {
            req => isa('HTTP::Request'),
            res => all(
                isa('HTTP::Response'),
                methods( 'content' => re(qr/Testing the history_3/) ),
            ),
        },
        "... and the third history item is of the correct format"
    );

    is(
        exception {
            $mech->clear_history;
        },
        undef,
        "Clear the history"
    );

    is(
        $mech->history_count, 1,
        "... and the history contains only one item"
    );

    my $history_item_after_clearing = $mech->history(0);
    cmp_deeply(
        $history_item_after_clearing,
        {
            req => isa('HTTP::Request'),
            res => all(
                isa('HTTP::Response'),
                methods( 'content' => re(qr/Testing the history_3/) ),
            ),
        },
        "... and the latest history item is of the correct format"
    );

    cmp_deeply(
        $mech->res,
        $history_item_after_clearing->{res},
        "... and we are still 'displaying' the page we were on when we cleared the history"
    );
    ok( !$mech->back, "... and we cannot go back in the history" );

    $mech->follow_link( n => 1 );
    ok( $mech->success, "Click a link in the page we are 'displaying'" )
        or die q{Can't get test page};
    is( $mech->history_count, 2, "... and it was recorded in the history" );
    like(
        $mech->res->content, qr/Testing the history_1/,
        "... and we are 'displaying' a different page"
    );

    ok( $mech->back, "We can go back in history" );
    cmp_deeply(
        $mech->res,
        $history_item_after_clearing->{res},
        "... and we are 'displaying' the page we were on when we cleared the history again"
    );
}

{
    my $html = path('t/history_2.html')->slurp;
    my $server = LocalServer->spawn( html => $html );
    my $mech = WWW::Mechanize->new( cookie_jar => undef, autocheck => 0 );
    $mech->get( $server->url );

    ok(
        $mech->submit_form( form_name => "post_form" ),
        "Submit form using 'post' method"
    );
    is( $mech->history_count, 2, "... and it was recorded in the history" );
    is(
        $mech->history(0)->{req}->uri, $server->url,
        "... and the correct request was saved"
    );
}

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

    my $uri = URI::file->new_abs('t/history_1.html')->as_string;
    $mech->stack_depth(0);
    is( $mech->stack_depth(), 0, "stack_depth can be changed" );

    $mech->get($uri) for 1, 2, 3;
    is(
        $mech->history_count(), 1,
        "No history saved when history is turned off"
    );

    $mech->stack_depth(1);

    $mech->get($uri) for 1, 2, 3;
    is(
        $mech->history_count(), 2,
        "Limited history is saved when stack_depth is explicitly set"
    );

}

done_testing();