blib/lib/Catalyst/Plugin/ResponseFrom.pm

filter: Statementstmt 70.4% 43 / 61 Branchbran 50.0% 7 / 14 Conditioncond 58.3% 7 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 90.0% 9 / 10 Podpod 100% 3 / 3 totaltotal 67.2% 72 / 107 CC 12 SCAR 29.0
CC
12
Cov
66%
__ANON__
31.0
http_response_from
2.0
BEGIN
1.0
CRAP 18.2
↑ Summary
1 package Catalyst::Plugin::ResponseFrom;
2  
3 1 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTTP::Message::PSGI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.003';
11  
12 1 requires 'psgi_app', 'res', 'detach';
13  
14 ## Block of code gratuitously stolen from Web::Simple::Application
15 4 ▶my $_request_spec_to_http_request = sub {
Subroutine __ANON__: called CC=12 SCAR=34.4
16 4 my ($self, $method, $path, @rest) = @_;
17  
18 # if the first arg is a catalyst action, build a URL
19 4 ▶ if(Scalar::Util::blessed $method and $method->isa('Catalyst::Action')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($method) and $method->isa("Catalyst::Action")) 2 2 4
Condition100%3/3
builtin::blessed($method) and $method->isa("Catalyst::Action")
!ll&&!rl&&r
112
MC/DC100%2/2
builtin::blessed($method) and $method->isa("Catalyst::Action")
builtin::blessed($method) $method->isa("Catalyst::Action")
Truth table100%3/3
A and B
A= builtin::blessed($method)B= $method->isa("Catalyst::Action")
ABresult
0X0
100
111
20 2 $path = $self->uri_for($method, @_[2..$#_]);
21 2 $method = 'GET';
22 2 @rest = ();
23 }
24  
25 # if it's a reference, assume a request object
26 4 ▶ return $method if(Scalar::Util::blessed $method and $method->isa('HTTP::Request'));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($method) and $method->isa("HTTP::Request") 1 3 4
Condition66%2/3
builtin::blessed($method) and $method->isa("HTTP::Request")
!ll&&!rl&&r
301
MC/DC50%1/2
builtin::blessed($method) and $method->isa("HTTP::Request")
builtin::blessed($method) $method->isa("HTTP::Request")
Truth table66%2/3
A and B
A= builtin::blessed($method)B= $method->isa("HTTP::Request")
ABresult
0X0
100
111
27
28 3 ▶ if ($path =~ s/^(.*?)\@//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s/^(.*?)\@//) 0 3 3
29 0 my $basic = $1;
30 0 unshift @rest, 'Authorization:', 'Basic '.MIME::Base64::encode($basic);
31 }
32
33 3 my $request = HTTP::Request->new($method => $path);
34
35 3 my @params;
36
37 3 ▶ while (my ($header, $value) = splice(@rest, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@rest, 0, 2)) 0 3 3
38 0 ▶ unless ($header =~ s/:$//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header =~ s/:$//) 0 0 0
39 0 push @params, $header, $value;
40 }
41 0 $header =~ s/_/-/g;
42 0 ▶ if ($header eq 'Content') {
Branch: 1 branch
BranchTrueFalseTotal
if ($header eq 'Content') { } 0 0 0
43 0 $request->content($value);
44 } else {
45 0 $request->headers->push_header($header, $value);
46 }
47 }
48
49 3 ▶ if (($method eq 'POST' or $method eq 'PUT') and @params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "POST" || $method eq "PUT" and @params) 0 3 3
Condition33%2/6
$method eq "POST" || $method eq "PUT" and @params
!ll&&!rl&&r
300
$method eq "POST" or $method eq "PUT"
l!l&&r!l&&!r
003
MC/DC0%0/3
$method eq "POST" || $method eq "PUT" and @params
$method eq "POST" $method eq "PUT" @params
Truth table20%1/5
A or B and C
A= $method eq "POST"B= $method eq "PUT"C= @params
ABCresult
00X0
1X00
0100
1X11
0111
50 0 my $content = do {
51 0 my $url = URI->new('http:');
52 0 $url->query_form(@params);
53 0 $url->query;
54 };
55 0 $request->header('Content-Type' => 'application/x-www-form-urlencoded');
56 0 $request->header('Content-Length' => length($content));
57 0 $request->content($content);
58 }
59
60 3 return $request;
61 1 };
62  
63 4 ▶sub psgi_response_from {
Subroutine psgi_response_from: called CC=1 SCAR=0.0
Pod documented
64 4 my $self = shift;
65 4 my $http_request = $self->$_request_spec_to_http_request(@_);
66 4 my $psgi_env = HTTP::Message::PSGI::req_to_psgi($http_request);
67 4 return my $psgi_response = $self->psgi_app->($psgi_env);
68 }
69  
70 1 *response_from = \&http_response_from;
71 0 ▶sub http_response_from {
Subroutine http_response_from: not called CC=1 SCAR=6.9
Pod documented
72 0 my $self = shift;
73 0 my $psgi_response = $self->psgi_response_from(@_);
74 0 return my $http_response = HTTP::Message::PSGI::res_from_psgi($psgi_response);
75 }
76  
77 4 ▶sub redispatch_to {
Subroutine redispatch_to: called CC=1 SCAR=0.0
Pod documented
78 4 my $self = shift;
79 4 my $psgi_response = $self->psgi_response_from(@_);
80 4 $self->res->from_psgi_response($psgi_response);
81 4 $self->detach;
82 }
83  
84 1 1;
85  
86 =head1 NAME
87  
88 Catalyst::Plugin::ResponseFrom - Use the response of a public endpoint.
89  
90 =head1 SYNOPSIS
91  
92 package MyApp;
93 use Catalyst 'ResponseFrom';
94  
95 MyApp->setup;
96  
97 package MyApp::Controller::Example;
98  
99 use Moose;
100 use MooseX::MethodAttributes;
101 use HTTP::Request::Common;
102  
103 extends 'Catalyst::Controller';
104  
105 sub as_http_request :Local {
106 my ($self, $c) = @_;
107 $c->redispatch_to(GET $c->uri_for($self->action_for('target')));
108 # For simple GETs you can just use $c->uri_for style params like:
109 $c->redispatch_to($self->action_for('target'));
110 }
111  
112 sub as_spec :Local {
113 my ($self, $c) = @_;
114 $c->redispatch_to('GET' => $c->uri_for($self->action_for('target')));
115 }
116  
117 sub collect_response :Local {
118 my ($self, $c) = @_;
119 my $http_response = $c->http_response_from(GET => $c->uri_for($self->action_for('target')));
120 }
121  
122 sub target :Local {
123 my ($self, $c) = @_;
124 $c->response->content_type('text/plain');
125 $c->response->body("This is the target action");
126 }
127  
128 =head1 DESCRIPTION
129  
130 L<Catalyst> allows you to forward to a private named actions, but there is no
131 built in method to 'forward' to a public URL. You might want to do this rather
132 than (for example) issue a redirect.
133  
134 Additionally there is no 'subrequest' like feature (and L<Catalyst::Plugin::Subrequest>
135 uses internal hacks to function). There maye be cases, such as in testing, where
136 it would be great to be able to issue a public URL request and collect the response.
137  
138 This plugin is an attempt to give you these features in a clean manner that does not
139 rely on internal L<Catalyst> details that are subject to change. However you must be
140 using a more modern version of L<Catalyst> (the current requirement is 5.90060).
141  
142 =head1 METHODS
143  
144 This plugin adds the following methods to your L<Catalyst> application. All methods
145 share the same function signature (this approach and following documentation 'borrowed'
146 from L<Web::Simple>):
147  
148 my $psgi_response = $app->http_response_from(GET => '/' => %headers);
149 my $http_response = $app->http_response_from(POST => '/' => %headers_or_form);
150 $c->redispatch_to($http_request);
151  
152 Accept three style of arguments:
153  
154 =over4
155  
156 =item An L<HTTP::Request> object
157  
158 Runs this against the application as if running from a client such as a browser
159  
160 =item Parameters ($method, $path)
161  
162 A single domain specific language used to construct an L<HTTP::Request> object.
163  
164 If the HTTP method is POST or PUT, then a series of pairs can be passed after
165 this to create a form style message body. If you need to test an upload, then
166 create an L<HTTP::Request> object by hand or use the C<POST> subroutine
167 provided by L<HTTP::Request::Common>.
168
169 If you prefix the URL with 'user:pass@' this will be converted into
170 an Authorization header for HTTP basic auth:
171
172 my $res = $app->http_response_from(
173 GET => 'bob:secret@/protected/resource'
174 );
175
176 If pairs are passed where the key ends in :, it is instead treated as a
177 headers, so:
178
179 my $res = $app->http_response_from(
180 POST => '/',
181 'Accept:' => 'text/html',
182 some_form_key => 'value'
183 );
184
185 will do what you expect. You can also pass a special key of Content: to
186 set the request body:
187
188 my $res = $app->http_response_from(
189 POST => '/',
190 'Content-Type:' => 'text/json',
191 'Content:' => '{ "json": "here" }',
192 );
193  
194 =item a L<Catalyst::Action> instance + optional parameters
195  
196 If the arguments are identical to $c->uri_for, we create a request for that
197 action and assume the method is 'GET'.
198  
199 =back
200
201 =head2 psgi_response_from
202  
203 Given a request constructed as described above, return the L<PSGI> response.
204 This can be an Arrayref or Coderef.
205  
206 This is probably not that useful since you likely need to do additional work
207 to get data out of it, but was the result of refactoring, and I can imagine
208 a use case or two.
209  
210 =head2 http_response_from
211  
212 =head2 response_from
213  
214 Returns the L<HTTP::Response> object returned by the request.
215  
216 =head2 redispatch_to
217  
218 Uses the response giveing to the request and uses that to complete your
219 response. This also detaches so that calling this method effectively
220 ends processing. Basically use this when you decide you want the response
221 to be that of a totally different URL on your application.
222  
223 =head1 AUTHOR
224  
225 John Napiorkowski L<email:jjnapiork@cpan.org>
226
227 =head1 SEE ALSO
228
229 L<Catalyst>
230  
231 =head1 COPYRIGHT & LICENSE
232
233 Copyright 2015, John Napiorkowski L<email:jjnapiork@cpan.org>
234
235 This library is free software; you can redistribute it and/or modify it under
236 the same terms as Perl itself.
237
238 =cut
↑ Summary