line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::JSON::RPC::Autodoc::Request; |
2
|
4
|
|
|
4
|
|
12220
|
use strict; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
88
|
|
3
|
4
|
|
|
4
|
|
11
|
use warnings; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
77
|
|
4
|
4
|
|
|
4
|
|
341
|
use parent qw/HTTP::Request/; |
|
4
|
|
|
|
|
212
|
|
|
4
|
|
|
|
|
23
|
|
5
|
4
|
|
|
4
|
|
46699
|
use Clone qw/clone/; |
|
4
|
|
|
|
|
7086
|
|
|
4
|
|
|
|
|
192
|
|
6
|
4
|
|
|
4
|
|
1092
|
use JSON qw/to_json/; |
|
4
|
|
|
|
|
15264
|
|
|
4
|
|
|
|
|
20
|
|
7
|
4
|
|
|
4
|
|
391
|
use Test::Builder; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
81
|
|
8
|
4
|
|
|
4
|
|
1163
|
use Try::Tiny; |
|
4
|
|
|
|
|
2741
|
|
|
4
|
|
|
|
|
166
|
|
9
|
4
|
|
|
4
|
|
1373
|
use Test::JSON::RPC::Autodoc::Response; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
87
|
|
10
|
4
|
|
|
4
|
|
1410
|
use Test::JSON::RPC::Autodoc::Validator; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
108
|
|
11
|
4
|
|
|
4
|
|
1490
|
use Plack::Test::MockHTTP; |
|
4
|
|
|
|
|
1317
|
|
|
4
|
|
|
|
|
2194
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
5
|
|
|
5
|
1
|
22
|
my ($class, %opt) = @_; |
15
|
5
|
|
|
|
|
25
|
my $self = $class->SUPER::new(); |
16
|
5
|
|
100
|
|
|
65
|
$self->uri($opt{path} || '/'); |
17
|
5
|
|
|
|
|
9371
|
$self->{method} = 'POST'; |
18
|
5
|
|
|
|
|
8
|
$self->{app} = $opt{app}; |
19
|
5
|
|
50
|
|
|
26
|
$self->{id} = $opt{id} || 1; |
20
|
5
|
|
100
|
|
|
20
|
$self->{label} = $opt{label} || undef; |
21
|
5
|
|
|
|
|
16
|
return $self; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub json_rpc_method { |
25
|
3
|
|
|
3
|
0
|
4
|
my ($self, $name) = @_; |
26
|
3
|
50
|
|
|
|
10
|
return $self->{json_rpc_method} unless $name; |
27
|
0
|
|
|
|
|
0
|
$self->{json_rpc_method} = $name; |
28
|
0
|
|
|
|
|
0
|
return $name; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub main_content { |
32
|
3
|
|
|
3
|
0
|
95
|
my ($self, $content) = @_; |
33
|
3
|
50
|
|
|
|
10
|
return $self->{main_content} unless $content; |
34
|
0
|
|
|
|
|
0
|
$self->{main_content} = $content; |
35
|
0
|
|
|
|
|
0
|
return $content; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub params { |
39
|
3
|
|
|
3
|
0
|
302
|
my ($self, %params) = @_; |
40
|
3
|
|
|
|
|
54
|
$self->{rule} = clone \%params; |
41
|
3
|
|
|
|
|
6
|
for my $p (%params) { |
42
|
8
|
100
|
|
|
|
18
|
next unless ref $p eq 'HASH'; |
43
|
4
|
|
|
|
|
7
|
for my $key (keys %$p) { |
44
|
12
|
100
|
|
|
|
19
|
if ( $key eq 'required' ) { |
45
|
3
|
|
|
|
|
5
|
$p->{optional} = !$p->{required}; |
46
|
3
|
|
|
|
|
6
|
delete $p->{$key}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
3
|
|
|
|
|
42
|
my $validator = Data::Validator->new(%params)->with('NoThrow'); |
51
|
3
|
|
|
|
|
8074
|
$self->{validator} = $validator; |
52
|
3
|
|
|
|
|
10
|
return $validator; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub validator { |
56
|
21
|
|
|
21
|
0
|
80
|
my $self = shift; |
57
|
21
|
100
|
|
|
|
86
|
return $self->{validator} if $self->{validator}; |
58
|
3
|
|
|
|
|
15
|
return Data::Validator->new->with('NoThrow'); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub post_ok { |
62
|
5
|
|
|
5
|
0
|
980
|
my ($self, $method, $params, $headers) = @_; |
63
|
5
|
|
100
|
|
|
13
|
$params ||= {}; |
64
|
5
|
|
|
|
|
7
|
my $args = $self->validator->validate(%$params); |
65
|
5
|
|
|
|
|
595
|
my $ok = 1; |
66
|
5
|
50
|
|
|
|
81
|
$ok = 0 if $self->validator->has_errors; |
67
|
5
|
|
|
|
|
425
|
$self->validator->clear_errors(); |
68
|
|
|
|
|
|
|
|
69
|
5
|
|
|
|
|
460
|
my $json = $self->_make_request($method, $params, $headers); |
70
|
|
|
|
|
|
|
|
71
|
5
|
|
|
|
|
21
|
my $mock = Plack::Test::MockHTTP->new($self->{app}); |
72
|
5
|
|
|
|
|
25
|
my $res = $mock->request($self); |
73
|
5
|
50
|
|
|
|
4457
|
$ok = 0 if $res->code != 200; |
74
|
5
|
|
|
|
|
67
|
my $Test = Test::Builder->new(); |
75
|
5
|
|
|
|
|
26
|
$Test->ok($ok); |
76
|
|
|
|
|
|
|
|
77
|
5
|
|
|
|
|
898
|
$self->{response} = $res; |
78
|
5
|
|
|
|
|
9
|
$self->{main_content} = $json; |
79
|
5
|
|
|
|
|
4
|
$self->{json_rpc_method} = $method; |
80
|
5
|
|
|
|
|
18
|
return $res; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub post_only { |
84
|
1
|
|
|
1
|
0
|
5
|
my ($self, $method, $params, $headers) = @_; |
85
|
1
|
|
|
|
|
2
|
$self->_make_request($method, $params, $headers); |
86
|
1
|
|
|
|
|
7
|
my $mock = Plack::Test::MockHTTP->new($self->{app}); |
87
|
1
|
|
|
|
|
6
|
my $res = $mock->request($self); |
88
|
1
|
|
|
|
|
1083
|
return $res; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub post_not_ok { |
92
|
2
|
|
|
2
|
0
|
198
|
my ($self, $method, $params, $headers) = @_; |
93
|
2
|
|
50
|
|
|
4
|
$params ||= {}; |
94
|
2
|
|
|
|
|
3
|
my $args = $self->validator->validate(%$params); |
95
|
|
|
|
|
|
|
|
96
|
2
|
50
|
|
|
|
152
|
my $ok = 1 if $self->validator->has_errors; |
97
|
2
|
|
|
|
|
3
|
$self->validator->clear_errors(); |
98
|
|
|
|
|
|
|
|
99
|
2
|
|
|
|
|
6
|
$self->_make_request($method, $params, $headers); |
100
|
|
|
|
|
|
|
|
101
|
2
|
|
|
|
|
10
|
my $mock = Plack::Test::MockHTTP->new($self->{app}); |
102
|
2
|
|
|
|
|
11
|
my $res = $mock->request($self); |
103
|
2
|
50
|
|
|
|
578
|
$ok = 1 if $res->code == 200; |
104
|
2
|
|
|
|
|
20
|
my $Test = Test::Builder->new(); |
105
|
2
|
|
|
|
|
9
|
$Test->ok($ok); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub _make_request { |
109
|
8
|
|
|
8
|
|
72
|
my ($self, $method, $params, $headers) = @_; |
110
|
|
|
|
|
|
|
my $json = to_json( |
111
|
|
|
|
|
|
|
{ |
112
|
|
|
|
|
|
|
jsonrpc => '2.0', |
113
|
|
|
|
|
|
|
id => $self->{id}, |
114
|
8
|
|
|
|
|
41
|
method => $method, |
115
|
|
|
|
|
|
|
params => $params, |
116
|
|
|
|
|
|
|
}, { pretty => 1, utf8 => 1 } |
117
|
|
|
|
|
|
|
); |
118
|
8
|
|
|
|
|
308
|
$self->header('Content-Type' => 'application/json'); |
119
|
8
|
|
|
|
|
352
|
$self->header('Content-Length' => length $json); |
120
|
8
|
100
|
66
|
|
|
210
|
if($headers && ref $headers eq 'ARRAY') { |
121
|
1
|
|
|
|
|
2
|
for my $header (@$headers) { |
122
|
1
|
|
|
|
|
2
|
$self->header(@$header); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
8
|
|
|
|
|
52
|
$self->content($json); |
126
|
8
|
|
|
|
|
95
|
return $json; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
13
|
|
|
13
|
1
|
7978
|
sub method { shift->{method} } |
130
|
17
|
|
|
17
|
0
|
62
|
sub rule { shift->{rule} } |
131
|
4
|
|
|
4
|
0
|
11
|
sub label { shift->{label} } |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub response { |
134
|
13
|
|
|
13
|
0
|
64084
|
my $self = shift; |
135
|
13
|
100
|
|
|
|
42
|
return $self->{response} if $self->{response}; |
136
|
2
|
|
|
|
|
25
|
return; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
1; |