File: 2_request.t

package info (click to toggle)
liblwp-useragent-cached-perl 0.08-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 96 kB
  • sloc: perl: 361; makefile: 2
file content (200 lines) | stat: -rw-r--r-- 7,010 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
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
#!/usr/bin/env perl

use strict;
use Test::More;
use HTTP::Response;
use HTTP::Request;
use Digest::MD5;
use File::Temp 'tempdir';
use LWP::UserAgent::Cached;

my $cache_dir = eval {
	tempdir(CLEANUP => 1)
};

unless ($cache_dir) {
	plan skip_all => "Сan't create temp dir";
}

my %MAP;
no warnings ('redefine', 'once');
*LWP::UserAgent::send_request = sub {
	my ($self, $req) = @_;
	
	if (my $resp = $MAP{$req->uri}) {
		$resp->request($req);
		return $resp;
	}
	
	return HTTP::Response->new(404);
};

*LWP::UserAgent::map = sub {
	my ($self, $uri, $resp) = @_;
	
	$MAP{$uri} = $resp;
	return $uri;
};

*LWP::UserAgent::unmap = sub {
	my ($self, $uri) = @_;
	delete $MAP{$uri};
};
use warnings ('redefine', 'once');

my $ua = LWP::UserAgent::Cached->new(cache_dir => $cache_dir, cookie_jar => {});

# simple request test
my $mid = $ua->map('http://www.google.com/', HTTP::Response->new(200));
$ua->get('http://www.google.com/'); # cache 200 OK
$ua->unmap($mid);
$ua->map('http://www.google.com/', HTTP::Response->new(500));
is($ua->get('http://www.google.com/')->code, 200, 'Cached 200 ok response');

# more complex request test
my $response = HTTP::Response->new(301, 'Moved Permanently', [Location => 'http://www.yahoo.com/']);
$response->request(HTTP::Request->new(GET => 'http://yahoo.com'));
$mid = $ua->map('http://yahoo.com', $response);
my $y_mid = $ua->map('http://www.yahoo.com/', HTTP::Response->new(200, 'Ok', ['Set-Cookie' => 'lwp=true', 'Set-Cookie' => 'cached=yes'], 'This is a test'));
$ua->get('http://yahoo.com'); # make cache
is(scalar($ua->last_cached), 2, '@last_cached length = 2 on redirect');
is(scalar($ua->last_used_cache), 2, '@last_used_cache length = 2 on redirect');
$ua->unmap($mid);
$ua->cookie_jar->clear();
my $resp = $ua->get('http://yahoo.com');
is($resp->code, 200, 'Cached response with redirect');
ok(index($resp->content, 'This is a test')!=-1, 'Cached response content') or diag "Content: ", $resp->content;
my @cookies;
$ua->cookie_jar->scan(sub { my ($ver, $key, $val) = @_; push @cookies, "$key=$val" });
is(@cookies, 2, 'Got correct cookies count from the cache');
@cookies = sort @cookies;
is($cookies[0], 'cached=yes', 'Got correct first cookie');
is($cookies[1], 'lwp=true', 'Got correct second cookie');
is(scalar($ua->last_cached), 0, '@last_cached length = 0 when get from cache');
is(scalar($ua->last_used_cache), 2, '@last_used_cache length = 2 when get from cache');
# cookies bug (#gh5)
$resp = $ua->get('http://www.yahoo.com/');
is(() = $resp->request->header('Cookie') =~ /lwp=true/g, 1, 'correct cookie 1 sent in the request');
is(() = $resp->request->header('Cookie') =~ /cached=yes/g, 1, 'correct cookie 2 sent in the request');

# binary content test
my $content = "\0\1\2\3\4\5\6";
$mid = $ua->map('http://mail.com/bin', HTTP::Response->new(200, 'OK', [], $content));
$ua->get('http://mail.com/bin');
$ua->unmap($mid);
is($ua->get('http://mail.com/bin')->decoded_content, $content, "got right binary content");

$content .= "\n";
$mid = $ua->map('http://mail.com/bin2', HTTP::Response->new(200, 'OK', [], $content));
$ua->get('http://mail.com/bin2');
$ua->unmap($mid);
is($ua->get('http://mail.com/bin2')->decoded_content, $content, "got right binary content with trailing new line");

# nocache_if test
$ua->nocache_if(sub {
	$_[0]->code > 399
});
$mid = $ua->map('http://perl.org', HTTP::Response->new(403, 'Forbbidden'));
$ua->get('http://perl.org');
$ua->unmap($mid);
$ua->map('http://perl.org', HTTP::Response->new(200, 'OK', [], 'Perl there'));
$resp = $ua->get('http://perl.org');
is($resp->code, 200, 'Nocache code');
ok(index($resp->content, 'Perl there')!=-1, 'Nocache content') or diag 'Content: ', $resp->content;
$ua->nocache_if(undef);

# recache_if test
$ua->recache_if(sub {
	my ($resp, $path, $req) = @_;
	isa_ok($resp, 'HTTP::Response');
	isa_ok($req, 'HTTP::Request');
	ok(-e $path, 'Cached file exists') or diag "Path: $path";
	1;
});
$mid = $ua->map('http://perlmonks.org', HTTP::Response->new(407));
$ua->get('http://perlmonks.org');
$ua->unmap($mid);
$ua->map('http://perlmonks.org', HTTP::Response->new(200));
is($ua->get('http://perlmonks.org')->code, 200, 'Recached');
$ua->recache_if(undef);

# on_uncached test
$mid = $ua->map('http://www.modernperlbooks.com/', HTTP::Response->new(200));
my $on_uncached;
$ua->on_uncached(sub { $on_uncached = 1 });
$ua->get('http://www.modernperlbooks.com/');
is($on_uncached, 1, 'on_uncached called');
$on_uncached = undef;
$ua->get('http://www.modernperlbooks.com/');
is($on_uncached, undef, 'on_uncached not called');
$ua->on_uncached(undef);

# uncache test
$mid = $ua->map('http://metacpan.org', HTTP::Response->new(200));
$ua->get('http://metacpan.org');
$ua->uncache();
$ua->unmap($mid);
$ua->map('http://metacpan.org', HTTP::Response->new(503));
is($ua->get('http://metacpan.org')->code, 503, 'Uncache last response');

# collision test
$ua->cookie_jar->clear();
$resp = $ua->get('http://yahoo.com');
$ua->cookie_jar->clear();
my $cache_name = $ua->_get_cache_name($resp->request);
open FH, '>:raw', $cache_name;
print FH "http://google.com\nHTTP/1.1 200 OK\n";
close FH;
$ua->get('http://yahoo.com');
ok(-e "$cache_name-001", "Collision detected");

open FH, '>:raw', "$cache_name-001";
print FH "http://google.com\nHTTP/1.1 200 OK\n";
close FH;
$ua->cookie_jar->clear();
$ua->get('http://yahoo.com');
ok(-e "$cache_name-002", "Double collision detected");

$ua->unmap($y_mid);
$ua->map('http://www.yahoo.com/', HTTP::Response->new(404));
is($ua->get('http://yahoo.com')->code, 200, 'Cached response (collision list)');

# cache name specification test
$mid = $ua->map('http://perl.com', HTTP::Response->new(200));
$ua->agent('Internet-Explorer');
$ua->get('http://perl.com');
$ua->post('http://perl.com', ['q' => 'perl6', 'w' => 'now']);
$ua->unmap($mid);
$mid = $ua->map('http://perl.com', HTTP::Response->new(500));
$ua->cachename_spec({
	'User-Agent' => 'Internet-Explorer',
	'Accept' => undef
});
$ua->agent('Mozilla/5.0');
is($ua->get('http://perl.com', Accept => 'text/html')->code, 200, 'Cache name specification');

$ua->cachename_spec({
	_headers => ['User-Agent'],
	'User-Agent' => 'Internet-Explorer'
});
is($ua->get('http://perl.com', Accept => 'text/html')->code, 200, 'Cache name specification with _headers');

$ua->cachename_spec({
	_body => 'q=perl6&w=now',
	'User-Agent' => 'Internet-Explorer',
	'Content-Length' => 13,
});
is($ua->post('http://perl.com', ['q' => 'perl5', 'w' => 'yesterday'])->code, 200, 'Cache name specification with _body');

$mid = $ua->map('http://pause.perl.org', HTTP::Response->new(200));
$ua->cachename_spec({
	_body => '',
	_headers => []
});
$ua->post('http://pause.perl.org', [u => 'OLEG', act => 'login'], 'Accept' => 'text/html', 'Accept-Charset' => 'iso-8859');
$ua->unmap($mid);
$mid = $ua->map('http://pause.perl.org' => HTTP::Response->new(500));
$ua->agent('Google/Chrome');
is($ua->post('http://pause.perl.org', [u => 'UDGIN', act => 'logout'])->code, 200, 'Cache name based on url and http method');

done_testing;