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