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