line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::RestAPI; |
2
|
9
|
|
|
9
|
|
16174
|
use Moo; |
|
9
|
|
|
|
|
156369
|
|
|
9
|
|
|
|
|
70
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.1.6'; |
5
|
|
|
|
|
|
|
|
6
|
9
|
|
|
9
|
|
20886
|
use Types::Standard qw(ArrayRef InstanceOf Int Str); |
|
9
|
|
|
|
|
726588
|
|
|
9
|
|
|
|
|
124
|
|
7
|
9
|
|
|
9
|
|
16462
|
use Test::RestAPI::Endpoint qw(convert_path_to_filename); |
|
9
|
|
|
|
|
36
|
|
|
9
|
|
|
|
|
781
|
|
8
|
9
|
|
|
9
|
|
4867
|
use Test::RestAPI::MojoGenerator; |
|
9
|
|
|
|
|
29
|
|
|
9
|
|
|
|
|
351
|
|
9
|
9
|
|
|
9
|
|
5468
|
use Port::Selector; |
|
9
|
|
|
|
|
54264
|
|
|
9
|
|
|
|
|
293
|
|
10
|
9
|
|
|
9
|
|
56
|
use Path::Tiny; |
|
9
|
|
|
|
|
15
|
|
|
9
|
|
|
|
|
534
|
|
11
|
9
|
|
|
9
|
|
541
|
use Mojo::JSON qw(decode_json); |
|
9
|
|
|
|
|
2358
|
|
|
9
|
|
|
|
|
533
|
|
12
|
9
|
|
|
9
|
|
577
|
use Mojo::UserAgent; |
|
9
|
|
|
|
|
115851
|
|
|
9
|
|
|
|
|
135
|
|
13
|
|
|
|
|
|
|
|
14
|
9
|
|
|
9
|
|
331
|
use constant WINDOWS => ($^O eq 'MSWin32'); |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
921
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
BEGIN { |
17
|
9
|
|
|
9
|
|
9138
|
if (WINDOWS) { |
18
|
|
|
|
|
|
|
## no critic (ProhibitStringyEval) |
19
|
|
|
|
|
|
|
eval q{ |
20
|
|
|
|
|
|
|
use Win32::Process qw(NORMAL_PRIORITY_CLASS); |
21
|
|
|
|
|
|
|
}; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
die $@ if $@; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 NAME |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Test::RestAPI - Real mock of REST API |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 SYNOPSIS |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $api = Test::RestAPI->new( |
34
|
|
|
|
|
|
|
endpoints => [ |
35
|
|
|
|
|
|
|
Test::RestAPI::Endpoint->new( |
36
|
|
|
|
|
|
|
path => '/a', |
37
|
|
|
|
|
|
|
method => 'any', |
38
|
|
|
|
|
|
|
) |
39
|
|
|
|
|
|
|
], |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$api->start(); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
HTTP::Tiny->new->get($api->uri.'/test'); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 DESCRIPTION |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
In many (test) case you need mock some REST API. One way is mock your REST-API class abstraction or HTTP client. |
50
|
|
|
|
|
|
|
This module provides other way - start generated L server and provides pseudo-real your defined API. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 METHODS |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head2 new(%attribute) |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head3 %attribute |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head4 endpoints |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
I of instances L |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
default is I> (root) 200 OK - hello: |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Test::RestAPI::Endpoint->new( |
65
|
|
|
|
|
|
|
path => '/', |
66
|
|
|
|
|
|
|
method => 'any', |
67
|
|
|
|
|
|
|
render => {text => 'Hello'}, |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=cut |
71
|
|
|
|
|
|
|
has 'endpoints' => ( |
72
|
|
|
|
|
|
|
is => 'ro', |
73
|
|
|
|
|
|
|
isa => ArrayRef [ InstanceOf ['Test::RestAPI::Endpoint'] ], |
74
|
|
|
|
|
|
|
default => sub { |
75
|
|
|
|
|
|
|
return [ |
76
|
|
|
|
|
|
|
Test::RestAPI::Endpoint->new( |
77
|
|
|
|
|
|
|
path => '/', |
78
|
|
|
|
|
|
|
method => 'any', |
79
|
|
|
|
|
|
|
render => {text => 'Hello'}, |
80
|
|
|
|
|
|
|
) |
81
|
|
|
|
|
|
|
]; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head4 mojo_app_generator |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
This attribute is used for generating mojo application. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
default is L |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |
92
|
|
|
|
|
|
|
has 'mojo_app_generator' => ( |
93
|
|
|
|
|
|
|
is => 'ro', |
94
|
|
|
|
|
|
|
isa => InstanceOf ['Test::RestAPI::MojoGenerator'], |
95
|
|
|
|
|
|
|
default => sub { |
96
|
|
|
|
|
|
|
return Test::RestAPI::MojoGenerator->new(); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
has 'pid' => ( |
101
|
|
|
|
|
|
|
is => 'rw', |
102
|
|
|
|
|
|
|
isa => Int, |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
has 'uri' => ( |
106
|
|
|
|
|
|
|
is => 'rw', |
107
|
|
|
|
|
|
|
isa => Str, |
108
|
|
|
|
|
|
|
); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
has 'mojo_home' => ( |
111
|
|
|
|
|
|
|
is => 'ro', |
112
|
|
|
|
|
|
|
default => sub { |
113
|
|
|
|
|
|
|
my $mojo_home = Path::Tiny->tempdir(); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
path($mojo_home, 'log')->mkpath(); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
return $mojo_home; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head3 start |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Start REST API (L) application on some random unused port |
124
|
|
|
|
|
|
|
and wait to initialize. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
For start new process is used C on non-windows machines and L for windows machines. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
For generating L application is used L in C attribute - is possible set own generator. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |
131
|
|
|
|
|
|
|
sub start { |
132
|
8
|
|
|
8
|
1
|
472
|
my ($self) = @_; |
133
|
|
|
|
|
|
|
|
134
|
8
|
|
|
|
|
124
|
my $app_path = $self->mojo_app_generator->create_app($self->endpoints); |
135
|
|
|
|
|
|
|
|
136
|
8
|
|
|
|
|
60
|
$self->pid($self->_start($app_path)); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub _start { |
140
|
8
|
|
|
8
|
|
24
|
my ($self, $app_path) = @_; |
141
|
|
|
|
|
|
|
|
142
|
8
|
|
|
|
|
36
|
$self->_create_uri(); |
143
|
|
|
|
|
|
|
|
144
|
8
|
|
|
|
|
8138
|
my $pid; |
145
|
8
|
|
|
|
|
46
|
if (WINDOWS) { |
146
|
|
|
|
|
|
|
$pid = $self->_start_win($app_path); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
else { |
149
|
8
|
|
|
|
|
64
|
$pid = $self->_start_fork($app_path); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
4
|
|
|
|
|
138
|
$self->_wait_to_start(); |
153
|
|
|
|
|
|
|
|
154
|
4
|
|
|
|
|
36
|
return $pid; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub _create_uri { |
158
|
8
|
|
|
8
|
|
16
|
my ($self) = @_; |
159
|
|
|
|
|
|
|
|
160
|
8
|
|
|
|
|
152
|
my $port = Port::Selector->new->port(); |
161
|
|
|
|
|
|
|
|
162
|
8
|
|
|
|
|
9718
|
$self->uri("http://localhost:$port"); |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub _start_win { |
166
|
0
|
|
|
0
|
|
0
|
my ($self, $app_path) = @_; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
#This trick is copied from IPC::System::Simple |
169
|
|
|
|
|
|
|
#If is check in this sub to non-Win32 system, |
170
|
|
|
|
|
|
|
#perl don't check NORMAL_PRIORITY_CLASS constant in compilation phase. |
171
|
0
|
|
|
|
|
0
|
if (!WINDOWS) { |
172
|
0
|
|
|
|
|
0
|
die '_start_win ca be called only anna Windows'; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
else { |
175
|
|
|
|
|
|
|
my $args = 'perl '.$app_path->canonpath().' '.join ' ', $self->_mojo_args(); |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Win32::Process::Create( |
178
|
|
|
|
|
|
|
my $proc, |
179
|
|
|
|
|
|
|
$^X, |
180
|
|
|
|
|
|
|
$args, |
181
|
|
|
|
|
|
|
0, |
182
|
|
|
|
|
|
|
NORMAL_PRIORITY_CLASS, |
183
|
|
|
|
|
|
|
"." |
184
|
|
|
|
|
|
|
) || die "Process $args start fail $^E"; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
return $proc->GetProcessID(); |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub _start_fork { |
191
|
8
|
|
|
8
|
|
28
|
my ($self, $app_path) = @_; |
192
|
|
|
|
|
|
|
|
193
|
8
|
|
|
|
|
66
|
my @args = ($^X, $app_path->stringify, $self->_mojo_args()); |
194
|
|
|
|
|
|
|
|
195
|
8
|
|
|
|
|
17691
|
my $pid = fork; |
196
|
|
|
|
|
|
|
|
197
|
8
|
100
|
|
|
|
595
|
if ($pid) { |
|
|
50
|
|
|
|
|
|
198
|
4
|
|
|
|
|
321
|
return $pid |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
elsif ($pid == 0) { |
201
|
4
|
|
|
|
|
52
|
exec {$args[0]} @args; |
|
4
|
|
|
|
|
0
|
|
202
|
0
|
|
|
|
|
0
|
exit 1; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
else { |
205
|
0
|
|
|
|
|
0
|
die "Fork problem: $!"; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
sub _mojo_args { |
210
|
8
|
|
|
8
|
|
64
|
my ($self) = @_; |
211
|
|
|
|
|
|
|
|
212
|
8
|
|
|
|
|
296
|
return ('daemon', '-l', $self->uri, '-m', 'production', '--home', $self->mojo_home->canonpath()); |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub _wait_to_start { |
216
|
4
|
|
|
4
|
|
32
|
my ($self) = @_; |
217
|
|
|
|
|
|
|
|
218
|
4
|
|
|
|
|
35
|
while (1) { |
219
|
9
|
100
|
|
|
|
5001996
|
if (Mojo::UserAgent->new->get($self->uri.'/app_mojo_healtcheck')->res->body() eq 'OK') { |
220
|
4
|
|
|
|
|
50492
|
return 1; |
221
|
|
|
|
|
|
|
} |
222
|
5
|
|
|
|
|
35031
|
sleep 1; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head2 count_of_requests($path) |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
return count of request to C<$path> endpoint |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=cut |
231
|
|
|
|
|
|
|
sub count_of_requests { |
232
|
2
|
|
|
2
|
1
|
34694
|
my ($self, $path) = @_; |
233
|
|
|
|
|
|
|
|
234
|
2
|
50
|
|
|
|
13
|
$path = '/' if !defined $path; |
235
|
|
|
|
|
|
|
|
236
|
2
|
|
|
|
|
53
|
my $fh = path($self->mojo_home, convert_path_to_filename($path))->filehandle(); |
237
|
|
|
|
|
|
|
|
238
|
2
|
|
|
|
|
743
|
my $lines = 0; |
239
|
2
|
|
|
|
|
49
|
while (<$fh>) { |
240
|
3
|
|
|
|
|
15
|
$lines++; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
|
243
|
2
|
|
|
|
|
53
|
return $lines; |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=head2 list_of_requests_body($path) |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
return list (ArrayRef) of requests body to C<$path> endpoint |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=cut |
251
|
|
|
|
|
|
|
sub list_of_requests_body { |
252
|
1
|
|
|
1
|
1
|
30027
|
my ($self, $path) = @_; |
253
|
|
|
|
|
|
|
|
254
|
1
|
50
|
|
|
|
9
|
$path = '/' if !defined $path; |
255
|
|
|
|
|
|
|
|
256
|
1
|
|
|
|
|
17
|
my $fh = path($self->mojo_home, convert_path_to_filename($path))->filehandle(); |
257
|
|
|
|
|
|
|
|
258
|
1
|
|
|
|
|
312
|
my @lines; |
259
|
1
|
|
|
|
|
35
|
while (my $line = <$fh>) { |
260
|
2
|
|
|
|
|
107
|
chomp $line; |
261
|
|
|
|
|
|
|
|
262
|
2
|
|
|
|
|
30
|
push @lines, decode_json($line); |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
|
265
|
1
|
|
|
|
|
67
|
return \@lines; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
sub DESTROY { |
270
|
4
|
|
|
4
|
|
27370
|
my ($self) = @_; |
271
|
|
|
|
|
|
|
|
272
|
4
|
50
|
|
|
|
46
|
if ($^O eq 'MSWin32') { |
273
|
0
|
|
|
|
|
0
|
Win32::Process::KillProcess($self->pid, 0); |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
else { |
276
|
4
|
|
|
|
|
189
|
kill 'SIGTERM', $self->pid; |
277
|
|
|
|
|
|
|
} |
278
|
|
|
|
|
|
|
} |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head1 LICENSE |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
Copyright (C) Avast Software. |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
285
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=head1 AUTHOR |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
Jan Seidl Eseidl@avast.comE |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=cut |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
1; |