File Coverage

blib/lib/Plack/App/FakeApache/Request.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Plack::App::FakeApache::Request;
2              
3 1     1   1278 use Moose;
  0            
  0            
4              
5             use APR::Pool;
6             use APR::Table;
7              
8             use HTTP::Status qw(:is);
9              
10             use Plack::Request;
11             use Plack::Response;
12             use Plack::App::File;
13              
14             use Plack::App::FakeApache::Connection;
15             use Plack::App::FakeApache::Log;
16             use Plack::App::FakeApache::Server;
17             use Cwd qw(cwd);
18             use URI;
19              
20             my $NS = "plack.app.fakeapache";
21              
22             # Plack related attributes:
23             has env => (
24             is => 'ro',
25             isa => 'HashRef[Any]',
26             required => 1,
27             );
28              
29             has plack_request => (
30             is => 'ro',
31             isa => 'Plack::Request',
32             lazy_build => 1,
33             handles => {
34             method => 'method',
35             unparsed_uri => 'request_uri',
36             user => 'user',
37             },
38             );
39              
40             has plack_response => (
41             is => 'ro',
42             isa => 'Plack::Response',
43             lazy_build => 1,
44             handles => {
45             set_content_length => 'content_length',
46             content_type => 'content_type',
47             content_encoding => 'content_encoding',
48             status => 'status',
49             },
50             );
51              
52             has log => (
53             is => 'rw',
54             default => sub { Plack::App::FakeApache::Log->new() },
55             handles => [ qw(log_error log_reason warn) ],
56             );
57              
58             has server => (
59             is => 'rw',
60             default => sub { Plack::App::FakeApache::Server->new() },
61             );
62              
63             # Apache related attributes
64             has _apr_pool => (
65             is => 'ro',
66             isa => 'APR::Pool',
67             lazy_build => 1,
68             );
69              
70             has headers_in => (
71             is => 'ro',
72             isa => 'APR::Table',
73             lazy_build => 1,
74             );
75              
76             has headers_out => (
77             is => 'ro',
78             isa => 'APR::Table',
79             lazy_build => 1,
80             );
81              
82             has err_headers_out => (
83             is => 'ro',
84             isa => 'APR::Table',
85             lazy_build => 1,
86             );
87              
88             has _subprocess_env => (
89             is => 'ro',
90             isa => 'APR::Table',
91             lazy_build => 1,
92             );
93              
94             has dir_config => (
95             isa => 'HashRef[Any]',
96             traits => ['Hash'],
97             default => sub { {} },
98             handles => {
99             dir_config => 'accessor'
100             }
101             );
102              
103             has location => (
104             is => 'rw',
105             isa => "Str",
106             default => '/',
107             );
108              
109             has filename => (
110             is => 'rw',
111             isa => 'Str|Undef',
112             lazy_build => 1,
113             );
114              
115             has root => (
116             is => 'rw',
117             isa => 'Str',
118             default => cwd(),
119             );
120              
121             has is_initial_req => (
122             is => 'ro',
123             isa => 'Bool',
124             default => 1,
125             );
126              
127             has auth_type => (
128             is => 'ro',
129             isa => 'Str',
130             );
131              
132             has auth_name => (
133             is => 'ro',
134             isa => 'Str',
135             );
136              
137             # builders
138             sub _build_plack_request { return Plack::Request->new( shift->env ) }
139             sub _build_plack_response { return Plack::Response->new( 200, {}, [] ) }
140             sub _build__apr_pool { return APR::Pool->new() }
141             sub _build_headers_out { return APR::Table::make( shift->_apr_pool, 64 ) }
142             sub _build_err_headers_out{ return APR::Table::make( shift->_apr_pool, 64 ) }
143              
144             sub _build__subprocess_env {
145             my $self = shift;
146             my $env = $self->env;
147             my $table = APR::Table::make( $self->_apr_pool, 64 );
148              
149             $table->add( $_ => $env->{$_} ) for grep { /^[_A-Z]+$/ } keys %$env;
150              
151             return $table;
152             }
153              
154             sub _build_headers_in {
155             my $self = shift;
156             my $table = APR::Table::make( $self->_apr_pool, 64 );
157              
158             $self->plack_request->headers->scan( sub {
159             $table->add( @_ );
160             } );
161              
162             return $table;
163             }
164              
165             sub _build_filename {
166             my $self = shift;
167              
168             my $paf = Plack::App::File->new(
169             root => $self->root
170             );
171             my ($file, $path) = $paf->locate_file( $self->env );
172              
173             return undef if ref $file; # some sort of error
174             return $file;
175             }
176              
177             # Plack methods
178             sub finalize {
179             my $self = shift;
180             my $response = $self->plack_response;
181              
182             $self->headers_out->do( sub { $response->header( @_ ); 1 } ) if is_success( $self->status() );
183             $self->err_headers_out->do( sub { $response->header( @_ ); 1 } );
184              
185             return $response->finalize;
186             };
187              
188             # Apache methods
189              
190             sub args {
191             my $self = shift;
192             return $self->plack_request->env->{QUERY_STRING};
193             }
194              
195             sub hostname {
196             my $self = shift;
197              
198             return $self->env->{SERVER_NAME};
199             }
200              
201             sub subprocess_env {
202             my $self = shift;
203              
204             if (@_ == 1) {
205             return $self->_subprocess_env->get( @_ );
206             }
207              
208             if (@_ == 2) {
209             return $self->_subprocess_env->set( @_ );
210             }
211              
212             if (defined wantarray) {
213             return $self->_subprocess_env;
214             }
215              
216             $self->_subprocess_env->do( sub { $ENV{ $_[0] } = $_[1]; 1 } );
217             return;
218             }
219              
220             sub document_root {
221             my $self = shift;
222             return $self->root;
223             }
224              
225             sub pnotes {
226             my $self = shift;
227             my $key = shift;
228             my $old = $self->env->{$NS.'.pnotes'}->{$key};
229              
230             if (@_) {
231             $self->env->{$NS.'.pnotes'}->{$key} = shift;
232             }
233              
234             return $old;
235             }
236              
237             sub notes {
238             my $self = shift;
239             my $key = shift;
240             my $old = $self->env->{$NS.'.notes'}->{$key};
241              
242             if (@_) {
243             $self->env->{$NS.'.notes'}->{$key} = "".shift;
244             }
245              
246             return $old;
247             }
248              
249             # this is strictly mocking Apache::Connection, and only partially
250             sub connection {
251             my $self = shift;
252              
253             return Plack::App::FakeApache::Connection->new(
254             remote_ip => $self->plack_request->address,
255             log => $self->log,
256             );
257             }
258              
259             sub read {
260             my $self = shift;
261             my ($buffer, $length, $offset) = @_; # ... but use $_[0] for buffer
262              
263             my $request = $self->plack_request;
264              
265             # Is this needed? Intrudes on a Plack::Request private methodf...
266             unless ($request->env->{'psgix.input.buffered'}) {
267             $request->_parse_request_body;
268              
269             # Sets psgix.input.buffered and rewinds.
270             }
271              
272             my $fh = $request->input
273             or return 0;
274              
275             return $fh->read($_[0], $length, $offset);
276             }
277              
278             sub print {
279             my $self = shift;
280              
281             my $length = 0;
282             for (@_) {
283             $self->_add_content($_);
284             $length += length;
285             }
286              
287             return $length;
288             }
289              
290             sub write {
291             my ($self, $buffer, $length, $offset) = @_;
292              
293             if (defined $length && $length == -1) {
294             $self->_add_content($buffer);
295             return length $buffer;
296             }
297              
298             my $output = substr $buffer, $offset // 0, $length // length $buffer;
299              
300             $self->_add_content($output);
301            
302             return length $output;
303             }
304              
305             sub _add_content {
306             my $self = shift;
307              
308             push @{ $self->plack_response->body }, @_;
309             }
310              
311             sub rflush {
312             1;
313             }
314              
315             sub uri
316             {
317             my $self = shift;
318             return $self->plack_request->uri->path;
319             }
320              
321             sub construct_url
322             {
323             my $self = shift;
324             my $path = shift;
325             my $uri = URI->new($self->plack_request->uri);
326             $uri->path($path) if $path;
327             return $uri->as_string;
328             }
329              
330             no Moose;
331             __PACKAGE__->meta->make_immutable;
332              
333             1;