Client.pm

filter: Statementstmt 91.4% 107 / 117 Branchbran 60.5% 23 / 38 Conditioncond 54.5% 6 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 30 / 30 Podpod 100% 8 / 8 totaltotal 83.4% 176 / 211 CC 25 SCAR 15.3
CC
25
Cov
82%
new
14.2
_parse_request_args
6.0
backend
6.0
CRAP 4.6
↑ Summary Client/Backend/psgi_local.pm »
1 package Plack::Client;
2 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 $Plack::Client::VERSION = '0.06';
4 }
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 # ABSTRACT: abstract interface to remote web servers and local PSGI apps
8  
9 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use HTTP::Message::PSGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18  
19 8 ▶sub new {
Subroutine new: called CC=9 SCAR=26.5
Pod documented
20 8 my $class = shift;
21 8 my %params = @_;
22  
23 8 my %backends;
24 8 for my $scheme (keys %params) {
25 8 my $backend = $params{$scheme};
26 8 ▶ if (blessed($backend) && $backend->isa('Plack::Client::Backend')) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($backend) and $backend->isa('Plack::Client::Backend')) { } 0 8 8
elsif (builtin::reftype($backend) eq 'CODE') { } 0 8 8
elsif (ref $backend) { } 8 0 8
Condition33%1/3
builtin::blessed($backend) and $backend->isa("Plack::Client::Backend")
!ll&&!rl&&r
800
MC/DC0%0/2
builtin::blessed($backend) and $backend->isa("Plack::Client::Backend")
builtin::blessed($backend) $backend->isa("Plack::Client::Backend")
Truth table33%1/3
A and B
A= builtin::blessed($backend)B= $backend->isa("Plack::Client::Backend")
ABresult
0X0
100
111
27 0 $backends{$scheme} = $backend->as_code;
28 }
29 elsif (reftype($backend) eq 'CODE') {
30 0 $backends{$scheme} = $backend;
31 }
32 elsif (ref($backend)) {
33 8 (my $normal_scheme = $scheme) =~ s/-/_/g;
34 8 my $backend_class = "Plack::Client::Backend::$normal_scheme";
35 8 Class::Load::load_class($backend_class);
36 8 ▶ croak "Backend classes must inherit from Plack::Client::Backend"
Branch: 1 branch
BranchTrueFalseTotal
unless $backend_class->isa("Plack::Client::Backend") 0 8 8
37 unless $backend_class->isa('Plack::Client::Backend');
38 8 ▶ $backends{$scheme} = $backend_class->new(
Branch: 2 branches
BranchTrueFalseTotal
builtin::reftype($backend) eq 'HASH' ? : 8 0 8
builtin::reftype($backend) eq 'ARRAY' ? : 0 0 0
39 reftype($backend) eq 'HASH' ? %$backend
40 : reftype($backend) eq 'ARRAY' ? @$backend
41 : $$backend
42 )->as_code;
43 }
44 else {
45 0 croak "Backends must be a coderef or a Plack::Client::Backend instance";
46 }
47 }
48  
49 bless {
50 8 backends => \%backends,
51 }, $class;
52 }
53  
54  
55 76 ▶sub backend {
Subroutine backend: called CC=4 SCAR=17.9
Pod documented
56 76 my $self = shift;
57 76 my ($scheme) = @_;
58 76 ▶ $scheme = $scheme->scheme if blessed($scheme);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($scheme) 76 0 76
59 76 my $backend = $self->_backend($scheme);
60 76 ▶ return $backend if defined $backend;
Branch: 1 branch
BranchTrueFalseTotal
if defined $backend 76 0 76
61 0 ▶ $scheme = 'http' if $scheme eq 'https';
Branch: 1 branch
BranchTrueFalseTotal
if $scheme eq "https" 0 0 0
62 0 $scheme =~ s/-ssl$//;
63 0 return $self->_backend($scheme);
64 }
65  
66 76 ▶sub _backend {
Subroutine _backend: called CC=1 SCAR=0.0
67 76 my $self = shift;
68 76 my ($scheme) = @_;
69 76 return $self->{backends}->{$scheme};
70 }
71  
72  
73 76 ▶sub request {
Subroutine request: called CC=2 SCAR=6.9
Pod documented
74 76 my $self = shift;
75  
76 76 my ($app, $env) = $self->_parse_request_args(@_);
77  
78 76 my $psgi_res = $self->_resolve_response($app->($env));
79 # is there a better place to do this? Plack::App::Proxy already takes care
80 # of this (since it's making a real http request)
81 76 ▶ $psgi_res->[2] = [] if $env->{REQUEST_METHOD} eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"REQUEST_METHOD"} eq "HEAD" 6 70 76
82  
83 # XXX: or just return the arrayref?
84 76 return Plack::Response->new(@$psgi_res);
85 }
86  
87 76 ▶sub _parse_request_args {
Subroutine _parse_request_args: called CC=6 SCAR=18.0
88 76 my $self = shift;
89  
90 76 ▶ if (blessed($_[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($_[0])) { } 12 64 76
elsif ((builtin::reftype($_[0]) || '') eq 'HASH') { } 6 58 64
91 12 ▶ if ($_[0]->isa('HTTP::Request')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0]->isa('HTTP::Request')) { } 6 6 12
elsif ($_[0]->isa('Plack::Request')) { } 6 0 6
92 6 return $self->_request_from_http_request(@_);
93 }
94 elsif ($_[0]->isa('Plack::Request')) {
95 6 return $self->_request_from_plack_request(@_);
96 }
97 else {
98 0 croak 'Request object must be either an HTTP::Request or a Plack::Request';
99 }
100 }
101 ▶ elsif ((reftype($_[0]) || '') eq 'HASH') {
Condition100%2/2
builtin::reftype($_[0]) || ''
l!l
658
MC/DC100%1/1
builtin::reftype($_[0]) || ''
builtin::reftype($_[0])
Truth table100%2/2
A
A= builtin::reftype($_[0])
Aresult
11
00
102 6 return $self->_request_from_env(@_);
103 }
104 else {
105 58 return $self->_request_from_http_request_args(@_);
106 }
107 }
108  
109 64 ▶sub _request_from_http_request {
Subroutine _request_from_http_request: called CC=1 SCAR=0.0
110 64 my $self = shift;
111 64 my ($http_request) = @_;
112 64 my $env = $self->_http_request_to_env($http_request);
113 64 return $self->_request_from_env($env);
114 }
115  
116 76 ▶sub _request_from_plack_request {
Subroutine _request_from_plack_request: called CC=1 SCAR=0.0
117 76 my $self = shift;
118 76 my ($req) = @_;
119  
120 76 return ($self->_app_from_request($req), $req->env);
121 }
122  
123 70 ▶sub _request_from_env {
Subroutine _request_from_env: called CC=1 SCAR=0.0
124 70 my $self = shift;
125 70 return $self->_request_from_plack_request(Plack::Request->new(@_));
126 }
127  
128 58 ▶sub _request_from_http_request_args {
Subroutine _request_from_http_request_args: called CC=1 SCAR=0.0
129 58 my $self = shift;
130 58 return $self->_request_from_http_request(HTTP::Request->new(@_));
131 }
132  
133 64 ▶sub _http_request_to_env {
Subroutine _http_request_to_env: called CC=4 SCAR=14.0
134 64 my $self = shift;
135 64 my ($req) = @_;
136  
137 64 my $scheme = $req->uri->scheme;
138 64 my $original_uri = $req->uri->clone;
139  
140 # hack around with this - psgi requires a host and port to exist, and
141 # for the scheme to be either http or https
142 64 ▶ if ($scheme ne 'http' && $scheme ne 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme ne "http" and $scheme ne "https") 32 32 64
Condition66%2/3
$scheme ne "http" and $scheme ne "https"
!ll&&!rl&&r
32032
MC/DC50%1/2
$scheme ne "http" and $scheme ne "https"
$scheme ne "http" $scheme ne "https"
Truth table66%2/3
A and B
A= $scheme ne "http"B= $scheme ne "https"
ABresult
0X0
100
111
143 32 ▶ if ($scheme =~ /-ssl$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme =~ /-ssl$/) { } 0 32 32
144 0 $req->uri->scheme('https');
145 }
146 else {
147 32 $req->uri->scheme('http');
148 }
149 32 $req->uri->host('Plack::Client');
150 32 $req->uri->port(-1);
151 }
152  
153 64 my $env = $req->to_psgi;
154  
155 64 $env->{'plack.client.original_uri'} = $original_uri;
156  
157 64 return $env;
158 }
159  
160 76 ▶sub _app_from_request {
Subroutine _app_from_request: called CC=3 SCAR=11.4
161 76 my $self = shift;
162 76 my ($req) = @_;
163  
164 76 ▶ my $uri = $req->env->{'plack.client.original_uri'} || $req->uri;
Condition33%1/3
$req->env->{'plack.client.original_uri'} || $req->uri
l!l&&r!l&&!r
7600
MC/DC0%0/2
$req->env->{'plack.client.original_uri'} || $req->uri
$req->env->{'plack.client.original_uri'} $req->uri
Truth table33%1/3
A || B
A= $req->env->{'plack.client.original_uri'}B= $req->uri
ABresult
1X1
011
000
165  
166 76 my $backend = $self->backend($uri);
167 76 my $app = $backend->($req);
168  
169 76 ▶ croak "Couldn't find app" unless $app;
Branch: 1 branch
BranchTrueFalseTotal
unless $app 0 76 76
170  
171 76 return $app;
172 }
173  
174 76 ▶sub _resolve_response {
Subroutine _resolve_response: called CC=3 SCAR=11.1
175 76 my $self = shift;
176 76 my ($psgi_res) = @_;
177  
178 76 ▶ if (ref($psgi_res) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $psgi_res eq "CODE") 62 14 76
179 62 my $body = [];
180 62 ▶ $psgi_res->(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
181 62 $psgi_res = shift;
182 return Plack::Util::inline_object(
183 94 ▶ write => sub { push @$body, $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
184 50 ▶ close => sub { push @$psgi_res, $body },
Subroutine __ANON__: called CC=1 SCAR=0.0
185 62 );
186 62 });
187 }
188  
189 76 ▶ if (ref($psgi_res) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $psgi_res ne "ARRAY") 0 76 76
190 0 require Data::Dumper;
191 0 croak "Unable to understand app response:\n"
192 . Data::Dumper::Dumper($psgi_res);
193 }
194  
195 76 return $psgi_res;
196 }
197  
198  
199 34 ▶sub get { shift->request('GET', @_) }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
200 6 ▶sub head { shift->request('HEAD', @_) }
Subroutine head: called CC=1 SCAR=0.0
Pod documented
201 6 ▶sub post { shift->request('POST', @_) }
Subroutine post: called CC=1 SCAR=0.0
Pod documented
202 6 ▶sub put { shift->request('PUT', @_) }
Subroutine put: called CC=1 SCAR=0.0
Pod documented
203 6 ▶sub delete { shift->request('DELETE', @_) }
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
204  
205  
206 5 1;
207  
208 __END__
↑ Summary Client/Backend/psgi_local.pm »