| 1 |
# Test extra HTTP::Response methods. Basic operation is tested in the |
| 2 |
# message.t test suite. |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
|
| 7 |
use Test::More; |
| 8 |
plan tests => 68; |
| 9 |
|
| 10 |
use HTTP::Date; |
| 11 |
use HTTP::Request; |
| 12 |
use HTTP::Response; |
| 13 |
|
| 14 |
# make sure we get no failures from undefined response values |
| 15 |
{ |
| 16 |
my $req = HTTP::Response->new(); |
| 17 |
is($req->is_success(), undef, 'Empty res: is_success'); |
| 18 |
is($req->is_info(), undef, 'Empty res: is_info'); |
| 19 |
is($req->is_redirect(), undef, 'Empty res: is_redirect'); |
| 20 |
is($req->is_error(), undef, 'Empty res: is_error'); |
| 21 |
is($req->is_client_error(), undef, 'Empty res: is_client_error'); |
| 22 |
is($req->is_server_error(), undef, 'Empty res: is_server_error'); |
| 23 |
is($req->filename(), undef, 'Empty res: filename'); |
| 24 |
} |
| 25 |
|
| 26 |
my $time = time; |
| 27 |
|
| 28 |
my $req = HTTP::Request->new(GET => 'http://www.sn.no'); |
| 29 |
$req->date($time - 30); |
| 30 |
|
| 31 |
my $r = HTTP::Response->new(200, "OK"); |
| 32 |
$r->client_date($time - 20); |
| 33 |
$r->date($time - 25); |
| 34 |
$r->last_modified($time - 5000000); |
| 35 |
$r->request($req); |
| 36 |
|
| 37 |
#note $r->as_string; |
| 38 |
|
| 39 |
my $current_age = $r->current_age; |
| 40 |
|
| 41 |
ok($current_age >= 35 && $current_age <= 40); |
| 42 |
|
| 43 |
my $freshness_lifetime = $r->freshness_lifetime; |
| 44 |
ok($freshness_lifetime >= 12 * 3600); |
| 45 |
is($r->freshness_lifetime(heuristic_expiry => 0), undef); |
| 46 |
|
| 47 |
my $is_fresh = $r->is_fresh; |
| 48 |
ok($is_fresh); |
| 49 |
is($r->is_fresh(heuristic_expiry => 0), undef); |
| 50 |
|
| 51 |
note "current_age = $current_age"; |
| 52 |
note "freshness_lifetime = $freshness_lifetime"; |
| 53 |
note "response is ", "not " x !$is_fresh, "fresh"; |
| 54 |
note "it will be fresh for ", $freshness_lifetime - $current_age, |
| 55 |
" more seconds"; |
| 56 |
|
| 57 |
# OK, now we add an Expires header |
| 58 |
$r->expires($time); |
| 59 |
note "\n", $r->dump; |
| 60 |
|
| 61 |
$freshness_lifetime = $r->freshness_lifetime; |
| 62 |
is($freshness_lifetime, 25); |
| 63 |
$r->remove_header('expires'); |
| 64 |
|
| 65 |
# Now we try the 'Age' header and the Cache-Control: |
| 66 |
$r->header('Age', 300); |
| 67 |
$r->push_header('Cache-Control', 'junk'); |
| 68 |
$r->push_header(Cache_Control => 'max-age = 10'); |
| 69 |
|
| 70 |
#note $r->as_string; |
| 71 |
|
| 72 |
$current_age = $r->current_age; |
| 73 |
$freshness_lifetime = $r->freshness_lifetime; |
| 74 |
|
| 75 |
note "current_age = $current_age"; |
| 76 |
note "freshness_lifetime = $freshness_lifetime"; |
| 77 |
|
| 78 |
ok($current_age >= 300); |
| 79 |
is($freshness_lifetime, 10); |
| 80 |
|
| 81 |
ok($r->fresh_until); # should return something |
| 82 |
ok($r->fresh_until(heuristic_expiry => 0)); # should return something |
| 83 |
|
| 84 |
my $r2 = HTTP::Response->parse($r->as_string( "\x0d\x0a")); |
| 85 |
is( $r2->message(), 'OK', 'message() returns as expected' ); |
| 86 |
|
| 87 |
my @h = $r2->header('Cache-Control'); |
| 88 |
is(@h, 2); |
| 89 |
|
| 90 |
$r->remove_header("Cache-Control"); |
| 91 |
|
| 92 |
ok($r->fresh_until); # should still return something |
| 93 |
is($r->fresh_until(heuristic_expiry => 0), undef); |
| 94 |
|
| 95 |
is($r->redirects, 0); |
| 96 |
$r->previous($r2); |
| 97 |
is($r->previous, $r2); |
| 98 |
is($r->redirects, 1); |
| 99 |
|
| 100 |
$r2->previous($r->clone); |
| 101 |
is($r->redirects, 2); |
| 102 |
for ($r->redirects) { |
| 103 |
ok($_->is_success); |
| 104 |
} |
| 105 |
|
| 106 |
is($r->base, $r->request->uri); |
| 107 |
$r->push_header("Content-Location", "/1/A/a"); |
| 108 |
is($r->base, $r->request->uri); # we no longer consider Content-Location |
| 109 |
$r->push_header("Content-Base", "/2/;a=/foo/bar"); |
| 110 |
is($r->base, "http://www.sn.no/2/;a=/foo/bar"); |
| 111 |
$r->push_header("Content-Base", "/3/"); |
| 112 |
is($r->base, "http://www.sn.no/2/;a=/foo/bar"); |
| 113 |
|
| 114 |
{ |
| 115 |
my @warn; |
| 116 |
local $SIG{__WARN__} = sub { push @warn, @_ }; |
| 117 |
local $^W = 0; |
| 118 |
$r2 = HTTP::Response->parse( undef ); |
| 119 |
is($#warn, -1); |
| 120 |
local $^W = 1; |
| 121 |
$r2 = HTTP::Response->parse( undef ); |
| 122 |
is($#warn, 0); |
| 123 |
like($warn[0], qr/Undefined argument to parse\(\)/); |
| 124 |
} |
| 125 |
is($r2->code, undef); |
| 126 |
is($r2->message, undef); |
| 127 |
is($r2->protocol, undef); |
| 128 |
is($r2->status_line, "000 Unknown code"); |
| 129 |
$r2->protocol('HTTP/1.0'); |
| 130 |
is($r2->as_string("\n"), "HTTP/1.0 000 Unknown code\n\n"); |
| 131 |
is($r2->dump, "HTTP/1.0 000 Unknown code\n\n(no content)\n"); |
| 132 |
is($r2->current_age, 0); |
| 133 |
is($r2->freshness_lifetime, 3600); |
| 134 |
is($r2->freshness_lifetime(h_default => 900), 900); |
| 135 |
is($r2->freshness_lifetime(h_min => 7200), 7200); |
| 136 |
is($r2->freshness_lifetime(time => time), 3600); |
| 137 |
$r2->last_modified(time - 900); |
| 138 |
is($r2->freshness_lifetime, 90); |
| 139 |
is($r2->freshness_lifetime(h_lastmod_fraction => 0.2), 180); |
| 140 |
is($r2->freshness_lifetime(h_min => 300), 300); |
| 141 |
$r2->last_modified(time - 1000000); |
| 142 |
is($r2->freshness_lifetime(h_max => 7200), 7200); |
| 143 |
is($r2->freshness_lifetime(heuristic_expiry => 0), undef); |
| 144 |
is($r2->freshness_lifetime(heuristic_expiry => 1), 86400); |
| 145 |
ok($r2->is_fresh(time => time)); |
| 146 |
ok($r2->fresh_until(time => time + 10)); |
| 147 |
|
| 148 |
$r2->client_date(1); |
| 149 |
cmp_ok(abs(time - $r2->current_age), '<', 10); # Allow 10s for slow boxes |
| 150 |
is($r2->freshness_lifetime, 60); |
| 151 |
$r2->date(time); |
| 152 |
$r2->header(Age => -1); |
| 153 |
cmp_ok(abs(time - $r2->current_age), '<', 10); # Allow 10s for slow boxes |
| 154 |
is($r2->freshness_lifetime, 86400); |
| 155 |
$req = HTTP::Request->new; |
| 156 |
$r2->request($req); |
| 157 |
cmp_ok(abs(time - $r2->current_age), '<', 10); # Allow 10s for slow boxes |
| 158 |
$req->date(2); |
| 159 |
$r2->request($req); |
| 160 |
cmp_ok(abs(time - $r2->current_age), '<', 10); # Allow 10s for slow boxes |
| 161 |
|
| 162 |
$r2->header ('Content-Disposition' => "attachment; filename=foo.txt\n"); |
| 163 |
is($r2->filename(), 'foo.txt'); |
| 164 |
$r2->header ('Content-Disposition' => "attachment; filename=\n"); |
| 165 |
is($r2->filename(), ''); |
| 166 |
$r2->header ('Content-Disposition' => "attachment\n"); |
| 167 |
is($r2->filename(), undef); |
| 168 |
$r2->header ('Content-Disposition' => "attachment; filename==?US-ASCII?B?Zm9vLnR4dA==?=\n"); |
| 169 |
is($r2->filename(), 'foo.txt'); |
| 170 |
$r2->header ('Content-Disposition' => "attachment; filename==?NOT-A-CHARSET?B?Zm9vLnR4dA==?=\n"); |
| 171 |
is($r2->filename(), '=?NOT-A-CHARSET?B?Zm9vLnR4dA==?='); |
| 172 |
$r2->header ('Content-Disposition' => "attachment; filename==?US-ASCII?Z?Zm9vLnR4dA==?=\n"); |
| 173 |
is($r2->filename(), '=?US-ASCII?Z?Zm9vLnR4dA==?='); |
| 174 |
$r2->header ('Content-Disposition' => "attachment; filename==?US-ASCII?Q?foo.txt?=\n"); |
| 175 |
is($r2->filename(), 'foo.txt'); |
| 176 |
$r2->remove_header ('Content-Disposition'); |
| 177 |
$r2->header ('Content-Location' => '/tmp/baz.txt'); |
| 178 |
is($r2->filename(), 'baz.txt'); |
| 179 |
$r2->remove_header ('Content-Location'); |
| 180 |
$req->uri('http://www.example.com/bar.txt'); |
| 181 |
is($r2->filename(), 'bar.txt'); |