File: 67save.t

package info (click to toggle)
libhttp-proxy-perl 0.301-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 636 kB
  • ctags: 164
  • sloc: perl: 2,403; makefile: 2
file content (221 lines) | stat: -rw-r--r-- 6,618 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use strict;
use warnings;
use Test::More;
use HTTP::Proxy::BodyFilter::save;
use File::Temp qw( tempdir );
use File::Spec::Functions;

# a sandbox to play in
my $dir = tempdir( CLEANUP => 1 );

my @errors = (
    [   [ keep_old => 1, timestamp => 1 ] =>
            qr/^Can't timestamp and keep older files at the same time/
    ],
    [ [ status => 200 ] => qr/^status must be an array reference/ ],
    [   [ status => [qw(200 007 )] ] =>
            qr/status must contain only HTTP codes/
    ],
    [ [ filename => 'zlonk' ] => qr/^filename must be a code reference/ ],
);
my @data = (
    'recusandae veritatis illum quos tempor aut quidem',
    'necessitatibus lorem aperiam facere consequuntur incididunt similique'
);
my @d = ( prefix => $dir );    # defaults
my @templates = (

    # args, URL => filename
    [   [@d],
        'http://bam.fr/zok/awk.html' =>
            catfile( $dir, qw(bam.fr zok awk.html) )
    ],
    [   [ @d, multiple => 0 ],
        'http://bam.fr/zok/awk.html' =>
            catfile( $dir, qw(bam.fr zok awk.html) )
    ],
    [   [@d],
        'http://bam.fr/zok/awk.html' =>
            catfile( $dir, qw(bam.fr zok awk.html.1) )
    ],
    [   [ @d, no_host => 1 ],
        'http://bam.fr/zok/awk.html' => catfile( $dir, qw(zok awk.html ) )
    ],
    [   [ @d, no_dirs => 1 ],
        'http://bam.fr/zok/awk.html' => catfile( $dir, qw(bam.fr awk.html) )
    ],
    [   [ @d, no_host => 1, no_dirs => 1 ],
        'http://bam.fr/zok/awk.html' => catfile( $dir, 'awk.html' )
    ],
    [   [ @d, no_dirs => 1 ],
        'http://bam.fr/zok/' => catfile( $dir, qw(bam.fr index.html) )
    ],
    #[ [@d], 'http://bam.fr/zok/' => "$dir/bam.fr/index.html" ],
    [   [ template => "$dir/%p" ],
        'http://bam.fr/pow/zok.html' => catfile( $dir, qw(pow zok.html) )
    ],
    [   [ template => "$dir/%f" ],
        'http://bam.fr/pow/zok.html' => catfile( $dir, 'zok.html' )
    ],
    [   [ template => "$dir/%p" ],
        'http://bam.fr/zam.html?q=pow' => catfile( $dir, 'zam.html' )
    ],
    # Win32 does not accept '?' in file names
    (   [   [ template => "$dir/%P" ],
            'http://bam.fr/zam.html?q=pow' =>
                catfile( $dir, 'zam.html?q=pow' )
        ]
        ) x ( $^O ne 'MSWin32' ),
    [   [ @d, cut_dirs => 2 ],
        'http://bam.fr/a/b/c/d/e.html' =>
            catfile( $dir, qw(bam.fr c d e.html) )
    ],
    [   [ @d, cut_dirs => 2, no_host => 1 ],
        'http://bam.fr/a/b/c/d/e.html' => catfile( $dir, qw(c d e.html) )
    ],
    [   [ @d, cut_dirs => 5, no_host => 1 ],
        'http://bam.fr/a/b/c/d/e.html' => catfile( $dir, 'e.html' )
    ],

    # won't save
    [ [ @d, keep_old => 1 ], 'http://bam.fr/zok/awk.html' => undef ],
);
my @responses = (
    [   [@d],
        'http://bam.fr/a.html' => 200,
        catfile( $dir, qw(bam.fr a.html) )
    ],
    [ [@d], 'http://bam.fr/b.html' => 404, undef ],
    [   [ @d, status => [ 200, 404 ] ],
        'http://bam.fr/c.html' => 404,
        catfile( $dir, qw(bam.fr c.html) )
    ],
);

plan tests => 2 * @errors    # error checking
    + 1                      # simple test
    + 7 * 2                  # filename tests: 2 that save
    + 5 * 2                  # filename tests: 2 that don't
    + 2 * @templates         # all template tests
    + 2 * @responses         # all responses tests
    ;

# some variables
my $proxy = HTTP::Proxy->new( port => 0 );
my ( $filter, $data, $file, $buffer );

# test the save filter
# 1) errors in new
for my $t (@errors) {
    my ( $args, $regex ) = @$t;
    ok( !eval { HTTP::Proxy::BodyFilter::save->new(@$args); 1; },
        "new( @$args ) fails" );
    like( $@, $regex, "Error matches $regex" );
}

# 2) code for filenames
$filter = HTTP::Proxy::BodyFilter::save->new( filename => sub {$file} );
$filter->proxy($proxy);

# simple check
ok( !$filter->will_modify, 'Filter does not modify content' );

# loop on four requests
# two that save, and two that won't
for my $name ( qw( zlonk.pod kayo.html ), undef, '' ) {
    $file = $name ? catfile( $dir, $name ) : $name;

    my $req = HTTP::Request->new( GET => 'http://www.example.com/' );
    ok( my $ok = eval {
            $filter->begin($req);
            1;
        },
        'Initialized filter without error'
    );
    diag $@ if !$ok;

    if ($file) {
        is( $filter->{_hpbf_save_filename}, $file, "Got filename ($file)" );
    }
    else {
        ok( !$filter->{_hpbf_save_filename}, 'No filename' );
    }

    my $filter_fh;
    if ($name) {
        ok( $filter->{_hpbf_save_fh}->opened, 'Filehandle opened' );
        $filter_fh = $filter->{_hpbf_save_fh};
    }
    else {
        ok( !exists $filter->{_hpbf_save_fh}, 'No filehandle' );
    }

    # add some data
    $buffer = '';
    ok( eval {
            $filter->filter( \$data[0], $req, '', \$buffer );
            $filter->filter( \$data[1], $req, '', undef );
            $filter->end();
            1;
        },
        'Filtered data without error'
    );
    diag $@ if $@;

    # file closed now
    ok( !defined $filter->{_hpbf_save_fh}, 'No filehandle' );
    if ($filter_fh) {
        ok( !$filter_fh->opened, 'Filehandle closed' );

        # check the data
        open my $fh, $file or diag "Can't open $file: $!";
        is( join( '', <$fh> ), join( '', @data ), 'All data saved' );
        close $fh;
    }

}

# 3) the multiple templating cases
for my $t (@templates) {
    my ( $args, $url, $filename ) = @$t;
    my $filter = HTTP::Proxy::BodyFilter::save->new(@$args);
    $filter->proxy($proxy);
    my $req = HTTP::Request->new( GET => $url );

    # filter initialisation
    ok( my $ok = eval {
            $filter->begin($req);
            1;
        },
        'Initialized filter without error'
    );
    diag $@ if !$ok;
    my $mesg = defined $filename ? "$url => $filename" : "Won't save $url";
    is( $filter->{_hpbf_save_filename}, $filename, $mesg );
}

# 4) some cases that depend on the response
for my $t (@responses) {
    my ( $args, $url, $status, $filename ) = @$t;
    my $filter = HTTP::Proxy::BodyFilter::save->new(@$args);
    $filter->proxy($proxy);
    my $res = HTTP::Response->new($status);
    $res->request( HTTP::Request->new( GET => $url ) );

    ok( my $ok = eval {
            $filter->begin($res);
            1;
        },
        'Initialized filter without error'
    );
    diag $@ if !$ok;
    if ($filename) {
        is( $filter->{_hpbf_save_filename},
            $filename, "$url ($status) => $filename" );
    }
    else {
        ok( !$filter->{_hpbf_save_filename},
            "$url ($status) => No filename" );
    }
}