line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Fake; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
2952
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
92
|
|
4
|
|
|
|
|
|
|
{ our $VERSION = '0.004'; } |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
490
|
use Mojo::Base 'Mojolicious'; |
|
2
|
|
|
|
|
191538
|
|
|
2
|
|
|
|
|
15
|
|
7
|
2
|
|
|
2
|
|
352705
|
use Log::Any qw< $log >; |
|
2
|
|
|
|
|
17172
|
|
|
2
|
|
|
|
|
10
|
|
8
|
2
|
|
|
2
|
|
5123
|
use YAML::XS qw< LoadFile >; |
|
2
|
|
|
|
|
5384
|
|
|
2
|
|
|
|
|
142
|
|
9
|
2
|
|
|
2
|
|
1020
|
use Try::Tiny; |
|
2
|
|
|
|
|
2722
|
|
|
2
|
|
|
|
|
124
|
|
10
|
2
|
|
|
2
|
|
15
|
use Scalar::Util qw< blessed >; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
93
|
|
11
|
2
|
|
|
2
|
|
1142
|
use Template::Perlish; |
|
2
|
|
|
|
|
11180
|
|
|
2
|
|
|
|
|
11
|
|
12
|
2
|
|
|
2
|
|
100
|
use 5.010; |
|
2
|
|
|
|
|
7
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub load_config { |
15
|
1
|
|
|
1
|
1
|
3
|
my $config; |
16
|
|
|
|
|
|
|
try { |
17
|
1
|
|
50
|
1
|
|
58
|
my $config_file = $ENV{WEBSERVICE_FAKE} // 'webservice-fake.yml'; |
18
|
1
|
|
|
|
|
5
|
$config = LoadFile($config_file); |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
|
|
392
|
my $custom = delete $config->{custom}; |
21
|
1
|
50
|
33
|
|
|
6
|
if ($custom && !blessed($custom)) { |
22
|
0
|
0
|
|
|
|
0
|
$custom = {class => $custom} unless ref $custom; |
23
|
0
|
|
|
|
|
0
|
local @INC = @INC; |
24
|
0
|
|
0
|
|
|
0
|
unshift @INC, @{$custom->{include} // []}; |
|
0
|
|
|
|
|
0
|
|
25
|
0
|
|
|
|
|
0
|
(my $path = "$custom->{class}.pm") =~ s{::}{/}gmxs; |
26
|
0
|
|
|
|
|
0
|
require $path; |
27
|
|
|
|
|
|
|
} ## end if ($custom && !blessed...) |
28
|
1
|
50
|
|
|
|
4
|
$config->{custom} = $custom->new($config) |
29
|
|
|
|
|
|
|
if defined $custom; |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
50
|
|
|
9
|
$config->{defaults}{template_start} //= '[%'; |
32
|
1
|
|
50
|
|
|
21
|
$config->{defaults}{template_stop} //= '%]'; |
33
|
1
|
|
50
|
|
|
7
|
$config->{defaults}{code} //= 200; |
34
|
1
|
|
50
|
|
|
5
|
$config->{v} //= {}; |
35
|
|
|
|
|
|
|
} ## end try |
36
|
|
|
|
|
|
|
catch { |
37
|
0
|
|
|
0
|
|
0
|
my $msg = $_; |
38
|
0
|
0
|
|
|
|
0
|
if (ref $_) { |
39
|
0
|
|
|
|
|
0
|
require Data::Dumper; |
40
|
0
|
|
|
|
|
0
|
local $Data::Dumper::Indent = 1; |
41
|
0
|
|
|
|
|
0
|
$msg = Data::Dumper::Dumper($_); |
42
|
|
|
|
|
|
|
} |
43
|
0
|
|
|
|
|
0
|
$log->error($msg); |
44
|
0
|
|
|
|
|
0
|
die $_; |
45
|
1
|
|
|
|
|
8
|
}; |
46
|
1
|
|
|
|
|
29
|
return $config; |
47
|
|
|
|
|
|
|
} ## end sub load_config |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub startup { |
50
|
1
|
|
|
1
|
1
|
13860
|
my $self = shift; |
51
|
|
|
|
|
|
|
|
52
|
1
|
|
|
|
|
6
|
my $config = $self->load_config; |
53
|
1
|
|
|
0
|
|
8
|
$self->helper(config => sub { $config }); |
|
0
|
|
|
|
|
0
|
|
54
|
1
|
|
50
|
|
|
30
|
$self->secrets($config->{secrets} // ['Fake off!']); |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
8
|
my $r = $self->routes; |
57
|
1
|
|
|
|
|
4
|
for my $spec (@{$config->{routes}}) { |
|
1
|
|
|
|
|
3
|
|
58
|
8
|
|
|
|
|
365
|
my $route = $r->route($spec->{path}); |
59
|
|
|
|
|
|
|
my @methods = |
60
|
1
|
|
|
|
|
5
|
exists($spec->{methods}) ? @{$spec->{methods}} |
61
|
|
|
|
|
|
|
: exists($spec->{method}) ? $spec->{method} |
62
|
8
|
100
|
|
|
|
1809
|
: (); |
|
|
100
|
|
|
|
|
|
63
|
8
|
100
|
|
|
|
27
|
$route->via(map { uc($_) } @methods) if @methods; |
|
7
|
|
|
|
|
33
|
|
64
|
8
|
|
|
|
|
91
|
$route->to(cb => $self->callback($spec, $config)); |
65
|
|
|
|
|
|
|
} ## end for my $spec (@{$config...}) |
66
|
|
|
|
|
|
|
|
67
|
1
|
|
|
|
|
40
|
return $self; |
68
|
|
|
|
|
|
|
} ## end sub startup |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub callback { |
71
|
8
|
|
|
8
|
1
|
20
|
my ($self, $spec, $config) = @_; |
72
|
8
|
|
|
|
|
15
|
my $defaults = $config->{defaults}; |
73
|
|
|
|
|
|
|
|
74
|
8
|
|
|
|
|
20
|
my $body_expander = $self->body_expander($spec, $config); |
75
|
8
|
|
|
|
|
25
|
my $headers_expander = $self->headers_expander($spec, $config); |
76
|
8
|
|
|
|
|
23
|
my $body_wrapper = $self->body_wrapper($spec, $config); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
return sub { |
79
|
12
|
|
|
12
|
|
117623
|
my $c = shift; |
80
|
|
|
|
|
|
|
|
81
|
12
|
|
|
|
|
44
|
my $variables = { |
82
|
|
|
|
|
|
|
body_params => $c->req->body_params->to_hash, |
83
|
|
|
|
|
|
|
controller => $c, |
84
|
|
|
|
|
|
|
headers => $c->req->headers->to_hash, |
85
|
|
|
|
|
|
|
params => $c->req->params->to_hash, |
86
|
|
|
|
|
|
|
query_params => $c->req->query_params->to_hash, |
87
|
|
|
|
|
|
|
stash => scalar($c->stash()), |
88
|
|
|
|
|
|
|
}; |
89
|
12
|
|
|
|
|
3426
|
$log->debug($c->req->to_string()); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# body, with exception handling for empty one, and wrapping |
92
|
12
|
|
|
|
|
5935
|
my $body = $body_expander->($variables); |
93
|
12
|
100
|
|
|
|
7394
|
if (!length $body) { |
94
|
2
|
50
|
|
|
|
9
|
if ($spec->{on_empty}) { |
95
|
0
|
|
|
|
|
0
|
my $r = $c->match()->root(); |
96
|
0
|
|
|
|
|
0
|
my $match = Mojolicious::Routes::Match->new(root => $r); |
97
|
0
|
|
|
|
|
0
|
$match->match($c => $spec->{on_empty}); |
98
|
0
|
|
|
|
|
0
|
my $frame = $match->stack()->[0]; |
99
|
0
|
|
|
|
|
0
|
$c->stash($_ => $frame->{$_}) for keys %$frame; |
100
|
0
|
|
|
|
|
0
|
return $frame->{cb}->($c); |
101
|
|
|
|
|
|
|
} ## end if ($spec->{on_empty}) |
102
|
|
|
|
|
|
|
return $c->render_not_found() |
103
|
2
|
50
|
|
|
|
8
|
if $spec->{not_found_on_empty}; |
104
|
|
|
|
|
|
|
} ## end if (!length $body) |
105
|
12
|
100
|
|
|
|
166
|
$body = $body_wrapper->({%$variables, content => $body}) |
106
|
|
|
|
|
|
|
if $body_wrapper; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# headers |
109
|
12
|
|
|
|
|
2041
|
my $headers = $headers_expander->($variables); |
110
|
|
|
|
|
|
|
|
111
|
12
|
|
|
|
|
4364
|
my $response = $c->res; |
112
|
12
|
|
|
|
|
172
|
$response->body($body); |
113
|
12
|
|
|
|
|
495
|
my $rhs = $response->headers(); |
114
|
12
|
|
|
|
|
123
|
$rhs->header($_, @{$headers->{$_}}) for keys %$headers; |
|
38
|
|
|
|
|
594
|
|
115
|
12
|
|
|
|
|
256
|
$response->fix_headers(); |
116
|
|
|
|
|
|
|
|
117
|
12
|
|
66
|
|
|
1560
|
$c->rendered($spec->{code} // $defaults->{code}); |
118
|
8
|
|
|
|
|
4241
|
}; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
} ## end sub callback |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub headers_expander { |
123
|
8
|
|
|
8
|
1
|
17
|
my ($self, $spec, $config) = @_; |
124
|
8
|
|
|
|
|
16
|
my $defaults = $config->{defaults}; |
125
|
|
|
|
|
|
|
|
126
|
8
|
|
33
|
|
|
28
|
my $start = $spec->{template_start} // $defaults->{template_start}; |
127
|
8
|
|
33
|
|
|
25
|
my $stop = $spec->{template_stop} // $defaults->{template_stop}; |
128
|
|
|
|
|
|
|
|
129
|
8
|
|
|
|
|
11
|
my %hef; |
130
|
8
|
|
|
|
|
13
|
for my $hs ( |
131
|
8
|
|
50
|
|
|
21
|
@{$defaults->{headers} // []}, # take them |
132
|
8
|
|
100
|
|
|
27
|
@{$spec->{headers} // []}, # all |
133
|
|
|
|
|
|
|
) |
134
|
|
|
|
|
|
|
{ |
135
|
12
|
|
|
|
|
35
|
for my $name (keys %$hs) { |
136
|
20
|
|
|
|
|
41
|
my $template = $hs->{$name}; |
137
|
|
|
|
|
|
|
my $expander = Template::Perlish->new( |
138
|
|
|
|
|
|
|
start => $start, |
139
|
|
|
|
|
|
|
stop => $stop, |
140
|
|
|
|
|
|
|
variables => { |
141
|
|
|
|
|
|
|
spec => $spec, |
142
|
|
|
|
|
|
|
config => $config, |
143
|
|
|
|
|
|
|
v => $config->{v}, |
144
|
|
|
|
|
|
|
} |
145
|
20
|
|
|
|
|
104
|
)->compile_as_sub($template); |
146
|
20
|
|
50
|
|
|
17492
|
push @{$hef{$name} //= []}, $expander; |
|
20
|
|
|
|
|
142
|
|
147
|
|
|
|
|
|
|
} ## end for my $name (keys %$hs) |
148
|
|
|
|
|
|
|
} ## end for my $hs (@{$defaults...}) |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# Ensure there will be a Content-Type |
151
|
|
|
|
|
|
|
$hef{'Content-Type'} //= |
152
|
8
|
|
100
|
9
|
|
46
|
[sub { return 'application/json' }]; |
|
9
|
|
|
|
|
34
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
return sub { |
155
|
12
|
|
|
12
|
|
35
|
my ($variables) = @_; |
156
|
|
|
|
|
|
|
return { |
157
|
|
|
|
|
|
|
map { |
158
|
12
|
|
|
|
|
46
|
$_ => [map { $_->($variables) } @{$hef{$_}}]; |
|
38
|
|
|
|
|
5529
|
|
|
38
|
|
|
|
|
655
|
|
|
38
|
|
|
|
|
92
|
|
159
|
|
|
|
|
|
|
} keys %hef |
160
|
|
|
|
|
|
|
}; |
161
|
8
|
|
|
|
|
31
|
}; |
162
|
|
|
|
|
|
|
} ## end sub headers_expander |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub body_expander { |
165
|
8
|
|
|
8
|
1
|
14
|
my ($self, $spec, $config) = @_; |
166
|
8
|
|
|
|
|
14
|
my $defaults = $config->{defaults}; |
167
|
|
|
|
|
|
|
|
168
|
8
|
|
50
|
|
|
17
|
my $body = $spec->{body} // '[%%]'; |
169
|
8
|
|
33
|
|
|
33
|
my $start = $spec->{template_start} // $defaults->{template_start}; |
170
|
8
|
|
33
|
|
|
24
|
my $stop = $spec->{template_stop} // $defaults->{template_stop}; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
my $be = Template::Perlish->new( |
173
|
|
|
|
|
|
|
start => $start, |
174
|
|
|
|
|
|
|
stop => $stop, |
175
|
|
|
|
|
|
|
variables => { |
176
|
|
|
|
|
|
|
spec => $spec, |
177
|
|
|
|
|
|
|
config => $config, |
178
|
|
|
|
|
|
|
v => $config->{v}, |
179
|
|
|
|
|
|
|
} |
180
|
8
|
|
|
|
|
44
|
)->compile_as_sub($body); |
181
|
|
|
|
|
|
|
|
182
|
8
|
|
50
|
|
|
8265
|
my $trim = $spec->{trim} //= ''; |
183
|
|
|
|
|
|
|
return sub { |
184
|
0
|
|
|
0
|
|
0
|
(my $body = $be->(@_)) =~ s{^\s+|\s+$}{}gmxs; |
185
|
0
|
|
|
|
|
0
|
return $body; |
186
|
|
|
|
|
|
|
} |
187
|
8
|
50
|
|
|
|
23
|
if $trim eq 'lines'; |
188
|
|
|
|
|
|
|
return sub { |
189
|
0
|
|
|
0
|
|
0
|
(my $body = $be->(@_)) =~ s{\A\s+|\s+\z}{}gmxs; |
190
|
0
|
|
|
|
|
0
|
return $body; |
191
|
|
|
|
|
|
|
} |
192
|
8
|
50
|
|
|
|
19
|
if $trim eq 'ends'; |
193
|
8
|
|
|
|
|
18
|
return $be; |
194
|
|
|
|
|
|
|
} ## end sub body_expander |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub body_wrapper { |
197
|
8
|
|
|
8
|
1
|
21
|
my ($self, $spec, $config) = @_; |
198
|
8
|
|
|
|
|
13
|
my $defaults = $config->{defaults}; |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
my $wrapper = |
201
|
|
|
|
|
|
|
exists($spec->{body_wrapper}) ? $spec->{body_wrapper} |
202
|
|
|
|
|
|
|
: exists($defaults->{body_wrapper}) ? $defaults->{body_wrapper} |
203
|
8
|
50
|
|
|
|
24
|
: undef; |
|
|
100
|
|
|
|
|
|
204
|
8
|
100
|
|
|
|
23
|
return unless defined $wrapper; |
205
|
|
|
|
|
|
|
|
206
|
4
|
|
33
|
|
|
19
|
my $start = $spec->{template_start} // $defaults->{template_start}; |
207
|
4
|
|
33
|
|
|
15
|
my $stop = $spec->{template_stop} // $defaults->{template_stop}; |
208
|
|
|
|
|
|
|
return Template::Perlish->new( |
209
|
|
|
|
|
|
|
start => $start, |
210
|
|
|
|
|
|
|
stop => $stop, |
211
|
|
|
|
|
|
|
variables => { |
212
|
|
|
|
|
|
|
spec => $spec, |
213
|
|
|
|
|
|
|
config => $config, |
214
|
|
|
|
|
|
|
v => $config->{v}, |
215
|
|
|
|
|
|
|
} |
216
|
4
|
|
|
|
|
21
|
)->compile_as_sub($wrapper); |
217
|
|
|
|
|
|
|
} ## end sub body_wrapper |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
1; |
220
|
|
|
|
|
|
|
__END__ |