line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::HTTP::MockServer::Once::REST; |
2
|
1
|
|
|
1
|
|
1001
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
3
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
4
|
1
|
|
|
1
|
|
544
|
use JSON::XS; |
|
1
|
|
|
|
|
2637
|
|
|
1
|
|
|
|
|
493
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub new { |
7
|
1
|
|
|
1
|
1
|
2607
|
my ($class) = shift; |
8
|
1
|
|
33
|
|
|
5
|
$class = ref $class || $class; |
9
|
1
|
|
|
|
|
5
|
my %dispatch = @_; |
10
|
1
|
|
|
|
|
3
|
return bless { d => \%dispatch }, $class; |
11
|
|
|
|
|
|
|
} |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub _wrapped_rest_call { |
14
|
0
|
|
|
0
|
|
|
my $self = shift; |
15
|
0
|
|
|
|
|
|
my $wrapped = shift; |
16
|
|
|
|
|
|
|
return sub { |
17
|
|
|
|
|
|
|
# this is the coderef invoked by MockServer |
18
|
0
|
|
|
0
|
|
|
my ($req, $res) = @_; |
19
|
0
|
|
|
|
|
|
my $input_content = $req->content; |
20
|
0
|
|
|
|
|
|
my $input_data; |
21
|
0
|
|
|
|
|
|
my $input_ct = $req->header("Content-type"); |
22
|
0
|
0
|
0
|
|
|
|
if ($input_content && $input_ct && $input_ct eq 'application/json') { |
|
|
|
0
|
|
|
|
|
23
|
0
|
|
|
|
|
|
eval { |
24
|
0
|
|
|
|
|
|
$input_data = decode_json $req->content; |
25
|
|
|
|
|
|
|
}; |
26
|
0
|
0
|
|
|
|
|
if ($@) { |
27
|
0
|
|
|
|
|
|
$res->code(400); |
28
|
0
|
|
|
|
|
|
$res->message('Malformed Request'); |
29
|
0
|
|
|
|
|
|
$res->content($@); |
30
|
0
|
|
|
|
|
|
return; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
} |
33
|
0
|
|
|
|
|
|
my $selected_key; |
34
|
|
|
|
|
|
|
my @captures; |
35
|
0
|
|
|
|
|
|
my $s = join " ", $req->method(), $req->uri()->canonical()->path(); |
36
|
0
|
|
|
|
|
|
foreach my $key (keys %{$self->{d}}) { |
|
0
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
my $rx = $self->{d}{$key}; |
38
|
0
|
0
|
|
|
|
|
if (@captures = ($s =~ $rx)) { |
39
|
0
|
|
|
|
|
|
$selected_key = $key; |
40
|
0
|
|
|
|
|
|
last; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
0
|
0
|
|
|
|
|
if (!$selected_key) { |
44
|
0
|
|
|
|
|
|
$res->code(404); |
45
|
0
|
|
|
|
|
|
$res->message('Page not found'); |
46
|
0
|
|
|
|
|
|
$res->content("$s not found"); |
47
|
0
|
|
|
|
|
|
return; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
my $return_data = $wrapped->( |
51
|
|
|
|
|
|
|
$selected_key, $req, $res, \@captures, $input_data |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
|
|
|
|
if ($return_data) { |
55
|
0
|
|
|
|
|
|
my $a = $req->header('Accept'); |
56
|
0
|
0
|
0
|
|
|
|
if ($a && $a eq 'application/json') { |
57
|
0
|
|
|
|
|
|
eval { |
58
|
0
|
|
|
|
|
|
my $out = encode_json $return_data; |
59
|
0
|
|
|
|
|
|
$res->header('Content-type', 'application/json'); |
60
|
0
|
|
|
|
|
|
$res->content($out); |
61
|
|
|
|
|
|
|
}; |
62
|
0
|
0
|
|
|
|
|
if ($@) { |
63
|
0
|
|
|
|
|
|
$res->code(500); |
64
|
0
|
|
|
|
|
|
$res->message('Internal Server Error'); |
65
|
0
|
|
|
|
|
|
$res->content($@); |
66
|
0
|
|
|
|
|
|
return; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} else { |
69
|
0
|
|
|
|
|
|
$res->code(400); |
70
|
0
|
|
|
|
|
|
$res->message('Malformed Request'); |
71
|
0
|
|
|
|
|
|
$res->content("Only support Accept: application/json"); |
72
|
0
|
|
|
|
|
|
return; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
0
|
|
|
|
|
|
}; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub wrap_object { |
79
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
80
|
0
|
|
|
|
|
|
my $object = shift; |
81
|
|
|
|
|
|
|
return $self->_wrapped_rest_call( |
82
|
|
|
|
|
|
|
sub { |
83
|
0
|
|
|
0
|
|
|
my $key = shift; |
84
|
0
|
|
|
|
|
|
return $object->$key(@_); |
85
|
|
|
|
|
|
|
} |
86
|
0
|
|
|
|
|
|
); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub wrap_hash { |
90
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
91
|
0
|
|
|
|
|
|
my $hash = shift; |
92
|
|
|
|
|
|
|
return $self->_wrapped_rest_call( |
93
|
|
|
|
|
|
|
sub { |
94
|
0
|
|
|
0
|
|
|
my $key = shift; |
95
|
0
|
|
|
|
|
|
return $hash->{$key}->(@_) |
96
|
|
|
|
|
|
|
} |
97
|
0
|
|
|
|
|
|
); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
__END__ |