blib/lib/Test/Mojo/Plack.pm

filter: Statementstmt 82.6% 86 / 104 Branchbran 41.3% 19 / 46 Conditioncond 36.6% 11 / 30 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 88.8% 16 / 18 Podpod 100% 1 / 1 totaltotal 61.5% 133 / 216 CC 37 SCAR 33.2
CC
37
Cov
64%
_request_ok
52.7
__ANON__
24.1
new
14.3
CRAP 27.6
↑ Summary
1 package Test::Mojo::Plack;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Mojo::Base 'Test::Mojo';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Mojo::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Mojo::Transaction::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Mojo::Util qw(encode decode url_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use Class::Load qw(load_class is_class_loaded);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use List::MoreUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶sub new {
Subroutine new: called CC=7 SCAR=26.6
Pod documented
18 2 my ($class, $app_class) = @_;
19  
20 2 my $t = $class->SUPER::new();
21  
22 2 ▶ return $t unless $app_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $app_class 1 1 2
23  
24 1 $ENV{PLACK_ENV} = 1;
25  
26 1 ▶ if (ref $app_class eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $app_class eq 'CODE') { } 1 0 1
27 1 ▶ $t->{psgi_app} = sub { my $res = $app_class->(shift); sub { shift->($res); } };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
28 } else {
29 0 ▶ load_class($app_class) unless is_class_loaded($app_class);
Branch: 1 branch
BranchTrueFalseTotal
unless is_class_loaded($app_class) 0 0 0
30 0 $app_class->import;
31 0 ▶ if ($app_class->can("_finalized_psgi_app") ) { # Catalyst
Branch: 2 branches
BranchTrueFalseTotal
if ($app_class->can('_finalized_psgi_app')) { } 0 0 0
elsif ($app_class->can('dance')) { } 0 0 0
32 0 $t->{psgi_app} = $app_class->_finalized_psgi_app;
33 }
34 elsif ($app_class->can("dance") ) { # Dancer
35 0 ▶ $t->{psgi_app} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 0 my $request = Dancer::Request->new( env => shift );
37 0 my $res = Dancer->dance( $request );
38 0 ▶ sub { shift->($res); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 }
40 0 }
41 }
42 1 ▶ die "Unable to instantiate application as a PSGI application: '$app_class'" unless $t->{psgi_app};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"psgi_app"} 0 1 1
43  
44 1 return $t;
45 }
46  
47 2 ▶sub _request_ok {
Subroutine _request_ok: called CC=21 SCAR=39.7
48 2 my ($self, $tx, $url) = @_;
49  
50 # Let Mojo::Test handle it if no app has been instantiated
51 2 ▶ return $self->SUPER::_request_ok(@_[1..2]) unless $self->{psgi_app};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"psgi_app"} 1 1 2
52  
53 1 $url = Mojo::URL->new($url);
54  
55 1 ▶ my $env = {
Branch: 2 branches
BranchTrueFalseTotal
$tx->req->version ? : 1 0 1
$url->scheme && $url->scheme eq 'https' ? : 0 1 1
Condition44%4/9
$url->path || '/'
l!l
10
$url->query || ''
l!l
10
join('?', $url->path, $url->query) || '/'
l!l
10
$url->scheme and $url->scheme eq "https"
!ll&&!rl&&r
100
MC/DC0%0/1
$url->path || '/'
$url->path
MC/DC0%0/1
$url->query || ''
$url->query
MC/DC0%0/1
join('?', $url->path, $url->query) || '/'
join('?', $url->path, $url->query)
MC/DC0%0/2
$url->scheme and $url->scheme eq "https"
$url->scheme $url->scheme eq "https"
Truth table50%1/2
A
A= $url->path
Aresult
11
00
Truth table50%1/2
A
A= $url->query
Aresult
11
00
Truth table50%1/2
A
A= join('?', $url->path, $url->query)
Aresult
11
00
Truth table33%1/3
A and B
A= $url->schemeB= $url->scheme eq "https"
ABresult
0X0
100
111
56 PATH_INFO => url_unescape($url->path || '/'),
57 QUERY_STRING => $url->query || '',
58 SCRIPT_NAME => '',
59 SERVER_NAME => $url->host,
60 SERVER_PORT => $url->port,
61 SERVER_PROTOCOL => $tx->req->version ? ('HTTP/' . $tx->req->version ) : 'HTTP/1.1',
62 REMOTE_ADDR => '127.0.0.1',
63 REMOTE_HOST => 'localhost',
64 REMOTE_PORT => int( rand(64000) + 1000 ), # not in RFC 3875
65 REQUEST_URI => (join '?', $url->path, $url->query) || '/', # not in RFC 3875
66 REQUEST_METHOD => $tx->req->method,
67 'psgi.version' => [ 1, 1 ],
68 'psgi.url_scheme' => $url->scheme && $url->scheme eq 'https' ? 'https' : 'http',
69 'psgi.input' => IO::String->new($tx->req->body . "\r\n"),
70 'psgi.errors' => *STDERR,
71 'psgi.multithread' => 0,
72 'psgi.multiprocess' => 0,
73 'psgi.run_once' => 1,
74 'psgi.streaming' => 1,
75 'psgi.nonblocking' => 0,
76 'HTTP_CONTENT_LENGTH' => length($tx->req->body),
77 };
78  
79 1 ▶ for my $field ( @{ $tx->req->headers->names || [] }) {
Condition50%1/2
$tx->req->headers->names || []
l!l
10
MC/DC0%0/1
$tx->req->headers->names || []
$tx->req->headers->names
Truth table50%1/2
A
A= $tx->req->headers->names
Aresult
11
00
80 2 my $key = uc("HTTP_$field");
81 2 $key =~ tr/-/_/;
82 2 ▶ $key =~ s/^HTTP_// if $field =~ /^Content-(Length|Type)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /^Content-(Length|Type)$/u 0 2 2
83  
84 2 ▶ unless ( exists $env->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $env->{$key}) 2 0 2
85 2 $env->{$key} = $tx->req->headers->header($field);
86 }
87 }
88  
89 1 ▶ if ($env->{SCRIPT_NAME}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"SCRIPT_NAME"}) 0 1 1
90 0 $env->{PATH_INFO} =~ s/^\Q$env->{SCRIPT_NAME}\E/\//;
91 0 $env->{PATH_INFO} =~ s/^\/+/\//;
92 }
93  
94 1 ▶ if (!defined($env->{HTTP_HOST}) && $url->host) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $env->{"HTTP_HOST"} and $url->host) 0 1 1
Condition33%1/3
not defined $env->{"HTTP_HOST"} and $url->host
!ll&&!rl&&r
010
MC/DC0%0/2
not defined $env->{"HTTP_HOST"} and $url->host
not defined $env->{"HTTP_HOST"} $url->host
Truth table33%1/3
not(A) and B
A= not defined $env->{"HTTP_HOST"}B= $url->host
ABresult
0X0
100
111
95 0 $env->{HTTP_HOST} = $url->host;
96 0 ▶ $env->{HTTP_HOST} .= ':' . $url->port
Branch: 1 branch
BranchTrueFalseTotal
if $url->port 0 0 0
97 if $url->port;
98 }
99 1 ▶ $env->{HTTP_HOST} ||= 'localhost';
Condition50%1/2
$env->{"HTTP_HOST"} ||= "localhost"
l!l
01
MC/DC0%0/1
$env->{"HTTP_HOST"} ||= "localhost"
$env->{"HTTP_HOST"}
Truth table50%1/2
A
A= $env->{"HTTP_HOST"}
Aresult
11
00
100  
101 1 my $ret = $self->{psgi_app}->($env);
102 1 my $res = Mojo::Message::Response->new();
103  
104 1 ▶ $ret->(sub {
Subroutine __ANON__: called CC=11 SCAR=31.8
105 1 my ($code, $headers, $body) = @{+shift};
106 1 my $header_hash;
107 1 my $it = List::MoreUtils::natatime 2, @{$headers};
108 1 ▶ while (my($k, $v) = $it->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$it()) 1 1 2
109 1 $res->headers->append($k, $v);
110 }
111 1 $res->code($code);
112  
113 1 my $body_str = '';
114 1 ▶ if (defined $body && blessed($body)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $body and builtin::blessed($body)) { } 0 1 1
elsif (my $type = ref $body) { } 1 0 1
Condition33%1/3
defined $body and builtin::blessed($body)
!ll&&!rl&&r
010
MC/DC0%0/2
defined $body and builtin::blessed($body)
defined $body builtin::blessed($body)
Truth table33%1/3
A and B
A= defined $bodyB= builtin::blessed($body)
ABresult
0X0
100
111
115 0 ▶ if ($body->can('getline')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body->can("getline")) 0 0 0
116 0 ▶ while (my $line = $body->getline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $body->getline) 0 0 0
117 0 ▶ $body_str .= ($line || '');
Condition0%0/2
$line || ''
l!l
00
MC/DC0%0/1
$line || ''
$line
Truth table0%0/2
A
A= $line
Aresult
11
00
118 }
119 }
120 } elsif(my $type = ref $body) {
121 1 ▶ if ($type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 1 0 1
elsif ($type eq 'GLOB') { } 0 0 0
122 1 $body_str = join '', @{$body};
123 } elsif ($type eq 'GLOB') {
124 0 local $/;
125 0 $body_str = <$body>;
126 }
127 };
128  
129 1 ▶ $body_str //= $body;
Condition33%1/3
$body_str //= $body
l!l&&r!l&&!r
100
MC/DC0%0/2
$body_str //= $body
$body_str $body
Truth table33%1/3
A //= B
A= $body_strB= $body
ABresult
1X1
011
000
130  
131 1 $res->body($body_str);
132 1 });
133  
134 1 $self->tx(Mojo::Transaction::HTTP->new);
135 1 $self->tx->req->env($env);
136 1 $self->tx->res($res);
137  
138 1 my $err = $self->tx->error;
139 Test::More::diag $err->{message}
140 1 ▶ if !(my $ok = !$err->{message} || $err->{code}) && $err;
Branch: 1 branch
BranchTrueFalseTotal
if not my $ok = !$err->{"message"} || $err->{"code"} and $err 0 1 1
Condition33%2/6
not my $ok = !$err->{"message"} || $err->{"code"} and $err
!ll&&!rl&&r
100
!$err->{'message'} || $err->{'code'}
l!l&&r!l&&!r
100
MC/DC0%0/3
not my $ok = !$err->{"message"} || $err->{"code"} and $err
!$err->{'message'} $err->{'code'} $err
Truth table25%1/4
not(not(A) || B) and C
A= !$err->{'message'}B= $err->{'code'}C= $err
ABCresult
1XX0
01X0
0000
0011
141 1 my $desc = encode 'UTF-8', "@{[uc $tx->req->method]} $url";
142 1 ▶ return $self->can('_test') ? $self->_test('ok', $ok, $desc) : $self->test('ok', $ok, $desc);
Branch: 1 branch
BranchTrueFalseTotal
$self->can('_test') ? : 0 1 1
143 }
144  
145 =head1 NAME
146  
147 Test::Mojo::Plack - Test Plack-compatible applications with Test:Mojo
148  
149 =head1 VERSION
150  
151 Version 0.13
152  
153 =cut
154  
155 4 our $VERSION = '0.13';
156  
157 =head1 SYNOPSIS
158  
159 use Test::Mojo::Plack;
160 use Test::More;
161  
162 my $foo = Test::Mojo::Plack->new('My::Catalyst::App');
163 my $foo = Test::Mojo::Plack->new('My::Dancer::App');
164  
165 $foo->get_ok("/")
166 ->status_is(200)
167 ->content_type_is('text/html')
168 ->text_is('#footer a.author', 'mendoza@pvv.ntnu.no');
169  
170 done_testing;
171  
172 =head1 SUBROUTINES/METHODS
173  
174 =head2 new
175  
176 =head2 new($app)
177  
178 Returns a L<Test::Mojo::Plack> object that is a subclass of L<Test::Mojo>
179  
180 If $app is provided, it tries to set app a PSGI application by guessing the framework of it.
181  
182 =head1 METHODS
183  
184 L<Test::Mojo::Plack> inherits all methods from L<Test::Mojo> and overrides the following:
185  
186 =head2 _request_ok
187  
188 Hijacks the setup and sending of a request to send it to a pre-defined PSGI application.
189  
190 =head1 AUTHOR
191  
192 Nicolas Mendoza, C<< <mendoza at pvv.ntnu.no> >>
193  
194 =head1 BUGS
195  
196 =head1 SUPPORT
197  
198 You can find documentation for this module with the perldoc command.
199  
200 perldoc Test::Mojo::Plack
201  
202 =head1 SEE ALSO
203  
204 =over
205  
206 =item L<Test::Mojo>
207  
208 =item L<Catalyst::Test>
209  
210 =item L<Plack::Test>
211  
212 =item L<Test::Mojo::Role::PSGI> - Newer alternative approach
213  
214 =back
215  
216 =head1 REPOSITORY
217  
218 L<https://github.com/nicomen/test-mojo-plack>
219  
220 =head1 ACKNOWLEDGEMENTS
221  
222 Heavily inspired by L<Plack::Test> and L<Catalyst::Test> and of course L<Test::Mojo>
223  
224 =head1 LICENSE AND COPYRIGHT
225  
226 Copyright 2014 Nicolas Mendoza.
227  
228 This program is free software; you can redistribute it and/or modify it
229 under the terms of either: the GNU General Public License as published
230 by the Free Software Foundation; or the Artistic License.
231  
232 See http://dev.perl.org/licenses/ for more information.
233  
234  
235 =cut
236  
237 4 1; # End of Test::Mojo::Plack
238  
239  
↑ Summary