| 1 | package Eixo::Rest::ApiFakeServer; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use Eixo::Base::Clase; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | has( | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | listeners=>{}, | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | cgi=>undef, | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 2 | ▶ | sub initialize{ | ||||||||||||||||||
|
Subroutine
initialize: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 15 | 2 | my ($self, %args) = @_; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 2 | $self->{cgi} = $args{cgi}; | |||||||||||||||||||
| 18 | 2 | $self->{listeners} = $args{listeners}; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # we treat special routes, ie. with placeholders... | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 2 | my $routes = []; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 2 | foreach my $r (keys(%{$args{listeners}})){ | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 6 | ▶ | if($r =~ /\:/){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 1 | my $n = $r; | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 1 | ▶ | while($r =~ /\:(\w+)/g){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 1 | $n =~ s/\:$1/\[\^\/\]\+/; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 1 | $n .= '$'; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 1 | $n = qr/$n/; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | push @$routes, { | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | ▶ | tester=>sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 0 | $_[0] =~ /$n/; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | }, | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 1 | route=>$r | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | else{ | ||||||||||||||||||||
| 52 | unshift @$routes, { | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | ▶ | tester=>sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | $_[0] eq $r; | |||||||||||||||||||
| 57 | }, | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 5 | route=>$r | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 2 | $self->{routes} = $routes; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 2 | foreach(values(%{$self->{listeners}})){ | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 6 | ▶ | $_->{type} = "GET" unless($_->{type}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 2 | $self; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 2 | ▶ | sub start{ | ||||||||||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 76 | 2 | my ($self, $port) = @_; | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 2 | return Eixo::Rest::ApiFakeServerProcess::start_server( | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | $port, | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | $self | ||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | ▶ | sub process{ | ||||||||||||||||||
|
Subroutine
process: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 88 | 0 | my ($self, $cgi) = @_; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | $self->cgi($cgi); | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | ▶ | my $listener = $self->__getListener($cgi) || $self->__defaultListener; | ||||||||||||||||||
|
Condition0%0/3
$self->__getListener($cgi) || $self->__defaultListener
MC/DC0%0/2
$self->__getListener($cgi) || $self->__defaultListener
$self->__getListener($cgi)
$self->__defaultListener
Truth table0%0/3
A || B
A= $self->__getListener($cgi)B= $self->__defaultListener
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | $self->__send( | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | $listener | ||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | ▶ | sub __getListener{ | ||||||||||||||||||
|
Subroutine
__getListener: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 102 | 0 | my ($self, $cgi) = @_; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | foreach my $r (@{$self->{routes}}){ | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 0 | ▶ | if($r->{tester}->($cgi->path_info)){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | my $method = $self->{listeners}->{$r->{route}}; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 0 | ▶ | return $method if($method->{type} eq '*'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 0 | ▶ | return $method if(lc($method->{type}) eq lc($cgi->request_method)); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | ▶ | sub __defaultListener{ | ||||||||||||||||||
|
Subroutine
__defaultListener: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 119 | {} | ||||||||||||||||||||
| 120 | 0 | } | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 0 | ▶ | sub __send{ | ||||||||||||||||||
|
Subroutine
__send: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 123 | 0 | my ($self, $response) = @_; | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 0 | ▶ | ($response->{header}) ? $response->{header}->($self) : $self->__header; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 0 | ▶ | ($response->{body}) ? $response->{body}->($self) : $self->__body; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 0 | ▶ | sub __header{ | ||||||||||||||||||
|
Subroutine
__header: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | print "HTTP/1.0 200 OK\r\n"; | |||||||||||||||||||
| 134 | 0 | print $_[0]->cgi->header(-type => 'text/plain'); | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 0 | ▶ | sub __body{ | ||||||||||||||||||
|
Subroutine
__body: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | package Eixo::Rest::ApiFakeServerProcess; | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 144 | 2 | ▶ | use parent qw(HTTP::Server::Simple::CGI); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 2 | ▶ | sub start_server{ | ||||||||||||||||||
|
Subroutine
start_server: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 147 | 2 | my ($port, $api) = @_; | |||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 2 | my $server = __PACKAGE__->new; | |||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 2 | $server->{api} = $api; | |||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 2 | $server->port($port); | |||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 2 | $server->background(); | |||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 0 | ▶ | sub handle_request{ | ||||||||||||||||||
|
Subroutine
handle_request: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 160 | 0 | my ($self, $cgi) = @_; | |||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 0 | $self->{api}->process($cgi); | |||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 2 | 1; | |||||||||||||||||||