line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::WWW::Stub::Handler; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
21
|
use strict; |
|
3
|
|
|
|
|
14
|
|
|
3
|
|
|
|
|
107
|
|
4
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
92
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
15
|
use Carp qw(confess); |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
186
|
|
7
|
3
|
|
|
3
|
|
20
|
use Scalar::Util qw(blessed); |
|
3
|
|
|
|
|
22
|
|
|
3
|
|
|
|
|
1813
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
14
|
|
|
14
|
0
|
59
|
my ($class, %args) = @_; |
11
|
14
|
|
|
|
|
33
|
my $pattern = $args{pattern}; |
12
|
14
|
|
|
|
|
21
|
my $app = $args{app}; |
13
|
14
|
|
|
|
|
69
|
return bless { pattern => $pattern, app => $app }, $class; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub factory { |
17
|
14
|
|
|
14
|
0
|
33
|
my ($class, $uri_or_re, $app_or_res) = @_; |
18
|
14
|
50
|
33
|
|
|
64
|
if (blessed($app_or_res) && $app_or_res->isa('Test::WWW::Stub::Handler')) { |
19
|
0
|
|
|
|
|
0
|
return $app_or_res; |
20
|
|
|
|
|
|
|
} |
21
|
14
|
|
|
|
|
25
|
my $app; |
22
|
14
|
|
|
|
|
31
|
my $type = ref($app_or_res); |
23
|
14
|
100
|
|
|
|
57
|
if ($type eq 'CODE') { |
|
|
50
|
|
|
|
|
|
24
|
1
|
|
|
|
|
2
|
$app = $app_or_res; |
25
|
|
|
|
|
|
|
} elsif ($type eq 'ARRAY') { |
26
|
13
|
|
|
23
|
|
53
|
$app = sub { $app_or_res }; |
|
23
|
|
|
|
|
75
|
|
27
|
|
|
|
|
|
|
} else { |
28
|
0
|
|
|
|
|
0
|
confess 'Handler MUST be a PSGI app or an ARRAY'; |
29
|
|
|
|
|
|
|
} |
30
|
14
|
|
|
|
|
46
|
return $class->new(pattern => $uri_or_re, app => $app); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub is_static_pattern { |
34
|
37
|
|
|
37
|
0
|
66
|
my ($self) = @_; |
35
|
37
|
|
|
|
|
215
|
return ref($self->{pattern}) ne 'Regexp'; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub try_call { |
39
|
37
|
|
|
37
|
0
|
81
|
my ($self, $uri, $env, $req) = @_; |
40
|
37
|
|
|
|
|
89
|
my ($matched, $captures) = $self->_match($uri); |
41
|
37
|
100
|
|
|
|
113
|
return undef unless $matched; |
42
|
25
|
|
|
|
|
72
|
return $self->_call($env, $req, @$captures); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub _call { |
46
|
25
|
|
|
25
|
|
63
|
my ($self, $env, $req, @match) = @_; |
47
|
25
|
|
|
|
|
75
|
$env->{'test.www.stub.handler'} = [ $self->{pattern}, $self->{app} ]; |
48
|
25
|
|
|
|
|
74
|
return $self->{app}->($env, $req, @match); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub _match { |
52
|
37
|
|
|
37
|
|
78
|
my ($self, $uri) = @_; |
53
|
37
|
100
|
|
|
|
86
|
return $self->is_static_pattern ? $self->_match_static($uri) : $self->_match_regexp($uri); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub _match_static { |
57
|
30
|
|
|
30
|
|
61
|
my ($self, $uri) = @_; |
58
|
30
|
100
|
|
|
|
118
|
my $matched = $uri eq $self->{pattern} ? 1 : 0; |
59
|
30
|
|
|
|
|
232
|
return ($matched, []); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub _match_regexp { |
63
|
7
|
|
|
7
|
|
19
|
my ($self, $uri) = @_; |
64
|
7
|
|
|
|
|
11
|
my $pattern = $self->{pattern}; |
65
|
7
|
|
|
|
|
38
|
my @captures = $uri =~ m/$pattern/; |
66
|
7
|
100
|
|
|
|
67
|
my $matched = @captures > 0 ? 1 : 0; |
67
|
7
|
|
|
|
|
25
|
return ($matched, \@captures); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |