| 1 | package Test::Varnish; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use HTTP::Cookies; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Test::More; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | ▶ | sub analyze_response { | ||||||||||||||||||||||||||
|
Subroutine
analyze_response: called CC=7 SCAR=24.6
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 5 | my ($self, $res) = @_; | |||||||||||||||||||||||||||
| 18 | 5 | my $cached = 0; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 5 | ▶ | if ($self->verbose) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | my $hdr_obj = $res->headers; | |||||||||||||||||||||||||||
| 23 | 0 | my @hdr_names = $hdr_obj->header_field_names; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Only "X-Varnish" is the standard, but some people use | ||||||||||||||||||||||||||||
| 26 | # custom and/or debugging headers | ||||||||||||||||||||||||||||
| 27 | 0 | for my $name (@hdr_names) { | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | next unless $name =~ m{^X\-Varnish}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | my $value = $res->header($name) || q{}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$res->header($name) || ''
MC/DC0%0/1
$res->header($name) || ''
$res->header($name)
Truth table0%0/2
A
A= $res->header($name)
| |||||||||||||||||||||||||||||
| 30 | 0 | diag("$name: $value"); | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 5 | my $main_header = $res->header("X-Varnish"); | |||||||||||||||||||||||||||
| 36 | #my $status = $res->header("X-Varnish-Status"); | ||||||||||||||||||||||||||||
| 37 | #my $cacheable = $res->header("X-Varnish-Cacheable"); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # "X-Varnish: 2131920313 1299858343" means cached | ||||||||||||||||||||||||||||
| 40 | # "X-Varnish: 2039442137" means not cached | ||||||||||||||||||||||||||||
| 41 | 5 | ▶ | if (defined $main_header && $main_header =~ m{^ \s* \d+ \s+ \d+ \s* $}mx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $main_header and $main_header =~ /^ \s* \d+ \s+ \d+ \s* $/mx
MC/DC100%2/2
defined $main_header and $main_header =~ /^ \s* \d+ \s+ \d+ \s* $/mx
defined $main_header
$main_header =~ /^ \s* \d+ \s+ \d+ \s* $/mx
Truth table100%3/3
A and B
A= defined $main_headerB= $main_header =~ /^ \s* \d+ \s+ \d+ \s* $/mx
| |||||||||||||||||||||||||||||
| 42 | 1 | $cached = 1; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 5 | return $cached; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 1 | my ($class, $opt) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | $opt ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opt ||= {}
MC/DC0%0/1
$opt ||= {}
$opt
Truth table50%1/2
A
A= $opt
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | my $self = { | ||||||||||||||||||||||||||||
| 55 | _verbose => $opt->{verbose}, | ||||||||||||||||||||||||||||
| 56 | 1 | }; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub is_cached { | ||||||||||||||||||||||||||
|
Subroutine
is_cached: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 0 | my ($self, $args, $message) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my $is_cached = $self->_is_cached($args); | |||||||||||||||||||||||||||
| 65 | 0 | my $url = $args->{url}; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if (! defined $is_cached) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $message ||= qq{Request to url '$url' failed}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$message ||= "Request to url '${url}' failed"
MC/DC0%0/1
$message ||= "Request to url '${url}' failed"
$message
Truth table0%0/2
A
A= $message
| |||||||||||||||||||||||||||||
| 69 | 0 | return ok(0 => $message); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | $message ||= qq{Request to url '$url' should be cached by Varnish}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$message ||= "Request to url '${url}' should be cached by Varnish"
MC/DC0%0/1
$message ||= "Request to url '${url}' should be cached by Varnish"
$message
Truth table0%0/2
A
A= $message
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | return ok($is_cached, $message); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub isnt_cached { | ||||||||||||||||||||||||||
|
Subroutine
isnt_cached: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 0 | my ($self, $args, $message) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $is_cached = $self->_is_cached($args); | |||||||||||||||||||||||||||
| 81 | 0 | my $url = $args->{url}; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if (! defined $is_cached) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $message ||= qq{Request to url '$url' failed}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$message ||= "Request to url '${url}' failed"
MC/DC0%0/1
$message ||= "Request to url '${url}' failed"
$message
Truth table0%0/2
A
A= $message
| |||||||||||||||||||||||||||||
| 85 | 0 | return ok(0 => $message); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | $message ||= qq{Request to $url should not be cached by Varnish}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$message ||= "Request to $url should not be cached by Varnish"
MC/DC0%0/1
$message ||= "Request to $url should not be cached by Varnish"
$message
Truth table0%0/2
A
A= $message
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | return ok(! $is_cached, $message); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub _is_cached { | ||||||||||||||||||||||||||
|
Subroutine
_is_cached: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 94 | 0 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if (! $args || ref $args ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $args or ref $args ne "HASH"
MC/DC0%0/2
not $args or ref $args ne "HASH"
not $args
ref $args ne "HASH"
Truth table0%0/3
not(A) or B
A= not $argsB= ref $args ne "HASH"
| |||||||||||||||||||||||||||||
| 97 | 0 | croak q{is_cached() requires a hashref}; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # 'headers' is optional, 'url' is mandatory | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if (! $args->{url}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | croak q{is_cached() requires a 'url'}; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my $res = $self->request($args); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Request failed, assert a test failure | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if (! $res) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | return; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # Request successful, check if varnish has cached it | ||||||||||||||||||||||||||||
| 113 | 0 | return $self->analyze_response($res); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub request { | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 0 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | my $method = $args->{method} || q(GET); | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'method'} || 'GET'
MC/DC0%0/1
$args->{'method'} || 'GET'
$args->{'method'}
Truth table0%0/2
A
A= $args->{'method'}
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my $headers = $args->{headers} || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'headers'} || {}
MC/DC0%0/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table0%0/2
A
A= $args->{'headers'}
| |||||||||||||||||||||||||||||
| 122 | 0 | my $url = $args->{url}; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if (! $url) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | croak(q(No 'url' argument?)); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | #if (! exists $headers->{Host} || ! $headers->{Host}) { | ||||||||||||||||||||||||||||
| 129 | # croak(q(No 'host' header?)); | ||||||||||||||||||||||||||||
| 130 | #} | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Init user agent object | ||||||||||||||||||||||||||||
| 133 | 0 | my $ua = $self->user_agent(); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # Avoid the '//' or varnish rules don't fire properly | ||||||||||||||||||||||||||||
| 136 | 0 | my $host = $headers->{Host}; | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | if (! $host) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | my $url_obj = URI->new($url); | |||||||||||||||||||||||||||
| 139 | 0 | ▶ | if (! $url_obj) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | croak(qq(URI failed parsing url '$url'. Can't continue without a "Host" header.)); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 0 | $host = $url_obj->host(); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | my $req = HTTP::Request->new($method => $url); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # We need to set HTTP/1.1 Host: header or the varnish | ||||||||||||||||||||||||||||
| 148 | # rules based on hostname won't kick in (my.cn. vs my.) | ||||||||||||||||||||||||||||
| 149 | 0 | $req->header(Host => $host); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ($headers) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | while (my ($name, $value) = each %{ $headers }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | if ($name eq 'Cookie') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | ($name, $value) = split '=', $value, 2; | |||||||||||||||||||||||||||
| 155 | #diag ("Setting cookie [$name] => [$value]"); | ||||||||||||||||||||||||||||
| 156 | 0 | $ua->cookie_jar->set_cookie(undef, $name, $value, '/', $host); | |||||||||||||||||||||||||||
| 157 | #$req->header(Cookie => "$name=$value"); | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | else { | ||||||||||||||||||||||||||||
| 160 | 0 | $req->header($name => $value); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | my $res = $ua->request($req); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | #if ($headers && exists $headers->{Cookie}) { | ||||||||||||||||||||||||||||
| 168 | # $ua->cookie_jar->clear_temporary_cookies(); | ||||||||||||||||||||||||||||
| 169 | #} | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | return $res; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | sub _response_sets_cookies { | ||||||||||||||||||||||||||
|
Subroutine
_response_sets_cookies: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 175 | 0 | my ($res) = @_; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | my $cookie_header = $res->header("Set-Cookie"); | |||||||||||||||||||||||||||
| 178 | #diag("cookie_header: " . ($cookie_header || "")); | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | return defined $cookie_header && $cookie_header ne q{} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $cookie_header and $cookie_header ne ""
MC/DC0%0/2
defined $cookie_header and $cookie_header ne ""
defined $cookie_header
$cookie_header ne ""
Truth table0%0/3
A and B
A= defined $cookie_headerB= $cookie_header ne ""
| |||||||||||||||||||||||||||||
| 181 | ? 1 | ||||||||||||||||||||||||||||
| 182 | : 0; | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub user_agent { | ||||||||||||||||||||||||||
|
Subroutine
user_agent: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | my $ua = LWP::UserAgent->new( max_redirect => 0 ); | |||||||||||||||||||||||||||
| 189 | 0 | my $jar = HTTP::Cookies->new(); | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | $ua->agent($self->user_agent_string()); | |||||||||||||||||||||||||||
| 192 | 0 | $ua->cookie_jar($jar); | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | return $ua; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub user_agent_string { | ||||||||||||||||||||||||||
|
Subroutine
user_agent_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | return qq{Test-Varnish/$VERSION}; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 5 | ▶ | sub verbose { | ||||||||||||||||||||||||||
|
Subroutine
verbose: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 204 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 5 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 0 | ▶ | $self->{_verbose} = shift(@_) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 5 | return $self->{_verbose}; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 3 | 1; # End of Test::Varnish | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | __END__ | ||||||||||||||||||||||||||||