lib/Plack/Middleware/APISchema/RequestValidator.pm

filter: Statementstmt 100% 50 / 50 Branchbran 100% 6 / 6 Conditioncond 71.4% 5 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 13 / 13 Podpod 50.0% 1 / 2 totaltotal 92.6% 76 / 82 CC 7 SCAR 7.2
CC
7
Cov
97%
call
4.0
_resolve_status_code
3.0
router
2.0
CRAP 2.1
« lib/APISchema/Generator/Router/Simple.pm ↑ Summary blib/script/generate_markdown_document.pl »
1 package Plack::Middleware::APISchema::RequestValidator;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Plack::Util::Accessor qw(schema validator);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use APISchema::Generator::Router::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use APISchema::Validator;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use APISchema::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use constant DEFAULT_VALIDATOR_CLASS => 'Valiemon';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 13 ▶sub call {
Subroutine call: called CC=4 SCAR=13.9
Pod documented
16 13 my ($self, $env) = @_;
17 13 my $req = Plack::Request->new($env);
18  
19 13 my ($matched, $route) = $self->router->routematch($env);
20 13 ▶ $matched or return $self->app->($env);
Branch: 1 branch
BranchTrueFalseTotal
$matched or return $self->app->($env) 12 1 13
21  
22 12 ▶ my $validator = APISchema::Validator->for_request(
Condition50%1/2
$self->validator // "Valiemon"
l!l
012
MC/DC0%0/1
$self->validator // "Valiemon"
$self->validator
Truth table50%1/2
A
A= $self->validator
Aresult
11
00
23 validator_class => $self->validator // DEFAULT_VALIDATOR_CLASS,
24 );
25 my $result = $validator->validate($route->name => {
26 header => +{ map {
27 39 my $field = lc($_) =~ s/[-]/_/gr;
28 39 ( $field => $req->header($_) );
29 } $req->headers->header_field_names },
30 parameter => $env->{QUERY_STRING},
31 12 body => $req->content,
32 content_type => $req->content_type,
33 }, $self->schema);
34  
35 12 my $errors = $result->errors;
36 12 my $status_code = $self->_resolve_status_code($result);
37 return [
38 12 ▶ $status_code,
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$errors 7 5 12
39 [ 'Content-Type' => 'application/json' ],
40 [ encode_json_canonical($errors) ],
41 ] if scalar keys %$errors;
42  
43 5 $self->app->($env);
44 }
45  
46 14 ▶sub router {
Subroutine router: called CC=2 SCAR=7.0
Pod undocumented
47 14 my ($self) = @_;
48  
49 14 ▶ $self->{router} //= do {
Condition66%2/3
$self->{"router"} //= do { my $generator = "APISchema::Generator::Router::Simple"->new; $generator->generate_router($self->schema) }
l!l&&r!l&&!r
1130
MC/DC0%0/2
$self->{"router"} //= do { my $generator = "APISchema::Generator::Router::Simple"->new; $generator->generate_router($self->schema) }
$self->{"router"} do { my $generator = "APISchema::Generator::Router::Simple"->new; $generator->generate_router($self->schema) }
Truth table66%2/3
A //= B
A= $self->{"router"}B= do { my $generator = "APISchema::Generator::Router::Simple"->new; $generator->generate_router($self->schema) }
ABresult
1X1
011
000
50 3 my $generator = APISchema::Generator::Router::Simple->new;
51 3 $generator->generate_router($self->schema);
52 };
53 }
54  
55 12 ▶sub _resolve_status_code {
Subroutine _resolve_status_code: called CC=3 SCAR=11.0
56 12 my ($self, $validation_result) = @_;
57 12 ▶ my $error_message = $validation_result->errors->{body}->{message} // '';
Condition100%2/2
$validation_result->errors->{"body"}{"message"} // ""
l!l
57
MC/DC100%1/1
$validation_result->errors->{"body"}{"message"} // ""
$validation_result->errors->{"body"}{"message"}
Truth table100%2/2
A
A= $validation_result->errors->{"body"}{"message"}
Aresult
11
00
58 12 ▶ return $error_message =~ m/Wrong content-type/ ? HTTP_UNSUPPORTED_MEDIA_TYPE : HTTP_UNPROCESSABLE_ENTITY;
Branch: 1 branch
BranchTrueFalseTotal
$error_message =~ /Wrong content-type/ ? : 1 11 12
59 }
60  
61  
62 1 1;
« lib/APISchema/Generator/Router/Simple.pm ↑ Summary blib/script/generate_markdown_document.pl »