line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::JSON::RPC::Autodoc::Request; |
2
|
4
|
|
|
4
|
|
13800
|
use strict; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
94
|
|
3
|
4
|
|
|
4
|
|
12
|
use warnings; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
104
|
|
4
|
4
|
|
|
4
|
|
382
|
use parent qw/HTTP::Request/; |
|
4
|
|
|
|
|
221
|
|
|
4
|
|
|
|
|
30
|
|
5
|
4
|
|
|
4
|
|
48060
|
use Clone qw/clone/; |
|
4
|
|
|
|
|
7120
|
|
|
4
|
|
|
|
|
198
|
|
6
|
4
|
|
|
4
|
|
1054
|
use JSON qw/to_json/; |
|
4
|
|
|
|
|
16698
|
|
|
4
|
|
|
|
|
20
|
|
7
|
4
|
|
|
4
|
|
488
|
use Test::Builder; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
75
|
|
8
|
4
|
|
|
4
|
|
1157
|
use Try::Tiny; |
|
4
|
|
|
|
|
2665
|
|
|
4
|
|
|
|
|
222
|
|
9
|
4
|
|
|
4
|
|
1455
|
use Test::JSON::RPC::Autodoc::Response; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
102
|
|
10
|
4
|
|
|
4
|
|
1361
|
use Test::JSON::RPC::Autodoc::Validator; |
|
4
|
|
|
|
|
12
|
|
|
4
|
|
|
|
|
127
|
|
11
|
4
|
|
|
4
|
|
1869
|
use Plack::Test::MockHTTP; |
|
4
|
|
|
|
|
1612
|
|
|
4
|
|
|
|
|
2684
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
5
|
|
|
5
|
1
|
27
|
my ($class, %opt) = @_; |
15
|
5
|
|
|
|
|
35
|
my $self = $class->SUPER::new(); |
16
|
5
|
|
100
|
|
|
71
|
$self->uri($opt{path} || '/'); |
17
|
5
|
|
|
|
|
9146
|
$self->{method} = 'POST'; |
18
|
5
|
|
|
|
|
9
|
$self->{app} = $opt{app}; |
19
|
5
|
|
50
|
|
|
23
|
$self->{id} = $opt{id} || 1; |
20
|
5
|
|
100
|
|
|
20
|
$self->{label} = $opt{label} || undef; |
21
|
5
|
|
|
|
|
15
|
return $self; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub json_rpc_method { |
25
|
3
|
|
|
3
|
0
|
3
|
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
|
101
|
my ($self, $content) = @_; |
33
|
3
|
50
|
|
|
|
11
|
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
|
298
|
my ($self, %params) = @_; |
40
|
3
|
|
|
|
|
57
|
$self->{rule} = clone \%params; |
41
|
3
|
|
|
|
|
9
|
for my $p (%params) { |
42
|
8
|
100
|
|
|
|
16
|
next unless ref $p eq 'HASH'; |
43
|
4
|
|
|
|
|
8
|
for my $key (keys %$p) { |
44
|
12
|
100
|
|
|
|
22
|
if ( $key eq 'required' ) { |
45
|
3
|
|
|
|
|
6
|
$p->{optional} = !$p->{required}; |
46
|
3
|
|
|
|
|
4
|
delete $p->{$key}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
3
|
|
|
|
|
50
|
my $validator = Data::Validator->new(%params)->with('NoThrow'); |
51
|
3
|
|
|
|
|
8239
|
$self->{validator} = $validator; |
52
|
3
|
|
|
|
|
47
|
return $validator; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub validator { |
56
|
21
|
|
|
21
|
0
|
85
|
my $self = shift; |
57
|
21
|
100
|
|
|
|
95
|
return $self->{validator} if $self->{validator}; |
58
|
3
|
|
|
|
|
13
|
return Data::Validator->new->with('NoThrow'); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub post_ok { |
62
|
5
|
|
|
5
|
0
|
1003
|
my ($self, $method, $params, $headers) = @_; |
63
|
5
|
|
100
|
|
|
13
|
$params ||= {}; |
64
|
5
|
|
|
|
|
9
|
my $args = $self->validator->validate(%$params); |
65
|
5
|
|
|
|
|
601
|
my $ok = 1; |
66
|
5
|
50
|
|
|
|
90
|
$ok = 0 if $self->validator->has_errors; |
67
|
5
|
|
|
|
|
443
|
$self->validator->clear_errors(); |
68
|
|
|
|
|
|
|
|
69
|
5
|
|
|
|
|
437
|
my $json = $self->_make_request($method, $params, $headers); |
70
|
|
|
|
|
|
|
|
71
|
5
|
|
|
|
|
23
|
my $mock = Plack::Test::MockHTTP->new($self->{app}); |
72
|
5
|
|
|
|
|
24
|
my $res = $mock->request($self); |
73
|
5
|
50
|
|
|
|
4433
|
$ok = 0 if $res->code != 200; |
74
|
5
|
|
|
|
|
57
|
my $Test = Test::Builder->new(); |
75
|
5
|
|
|
|
|
24
|
$Test->ok($ok); |
76
|
|
|
|
|
|
|
|
77
|
5
|
|
|
|
|
955
|
$self->{response} = $res; |
78
|
5
|
|
|
|
|
9
|
$self->{main_content} = $json; |
79
|
5
|
|
|
|
|
6
|
$self->{json_rpc_method} = $method; |
80
|
5
|
|
|
|
|
17
|
return $res; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub post_only { |
84
|
1
|
|
|
1
|
0
|
7
|
my ($self, $method, $params, $headers) = @_; |
85
|
1
|
|
|
|
|
3
|
$self->_make_request($method, $params, $headers); |
86
|
1
|
|
|
|
|
8
|
my $mock = Plack::Test::MockHTTP->new($self->{app}); |
87
|
1
|
|
|
|
|
6
|
my $res = $mock->request($self); |
88
|
1
|
|
|
|
|
1088
|
return $res; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub post_not_ok { |
92
|
2
|
|
|
2
|
0
|
230
|
my ($self, $method, $params, $headers) = @_; |
93
|
2
|
|
50
|
|
|
6
|
$params ||= {}; |
94
|
2
|
|
|
|
|
3
|
my $args = $self->validator->validate(%$params); |
95
|
|
|
|
|
|
|
|
96
|
2
|
50
|
|
|
|
143
|
my $ok = 1 if $self->validator->has_errors; |
97
|
2
|
|
|
|
|
4
|
$self->validator->clear_errors(); |
98
|
|
|
|
|
|
|
|
99
|
2
|
|
|
|
|
5
|
$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
|
|
|
|
606
|
$ok = 1 if $res->code == 200; |
104
|
2
|
|
|
|
|
20
|
my $Test = Test::Builder->new(); |
105
|
2
|
|
|
|
|
10
|
$Test->ok($ok); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub _make_request { |
109
|
8
|
|
|
8
|
|
71
|
my ($self, $method, $params, $headers) = @_; |
110
|
|
|
|
|
|
|
my $json = to_json( |
111
|
|
|
|
|
|
|
{ |
112
|
|
|
|
|
|
|
jsonrpc => '2.0', |
113
|
|
|
|
|
|
|
id => $self->{id}, |
114
|
8
|
|
|
|
|
39
|
method => $method, |
115
|
|
|
|
|
|
|
params => $params, |
116
|
|
|
|
|
|
|
}, { pretty => 1, utf8 => 1 } |
117
|
|
|
|
|
|
|
); |
118
|
8
|
|
|
|
|
264
|
$self->header('Content-Type' => 'application/json'); |
119
|
8
|
|
|
|
|
337
|
$self->header('Content-Length' => length $json); |
120
|
8
|
100
|
66
|
|
|
212
|
if($headers && ref $headers eq 'ARRAY') { |
121
|
1
|
|
|
|
|
3
|
for my $header (@$headers) { |
122
|
1
|
|
|
|
|
3
|
$self->header(@$header); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
8
|
|
|
|
|
54
|
$self->content($json); |
126
|
8
|
|
|
|
|
98
|
return $json; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
13
|
|
|
13
|
1
|
8136
|
sub method { shift->{method} } |
130
|
17
|
|
|
17
|
0
|
73
|
sub rule { shift->{rule} } |
131
|
4
|
|
|
4
|
0
|
10
|
sub label { shift->{label} } |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub response { |
134
|
13
|
|
|
13
|
0
|
83212
|
my $self = shift; |
135
|
13
|
100
|
|
|
|
55
|
return $self->{response} if $self->{response}; |
136
|
2
|
|
|
|
|
20
|
return; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
1; |