blib/lib/Dancer/Cookies.pm

filter: Statementstmt 100% 46 / 46 Branchbran 83.3% 10 / 12 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 11 / 11 Podpod 66.6% 4 / 6 totaltotal 92.9% 79 / 85 SCAR 23.0
CC
10
Cov
95%
parse_cookie_from_env
8.1
cookie
3.0
BEGIN
1.0
CRAP 10.0
« blib/lib/Dancer/SharedData.pm ↑ Summary blib/lib/Dancer/Factory/Hook.pm »
1 package Dancer::Cookies;
2 205 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: a singleton storage for all cookies
4 205 $Dancer::Cookies::VERSION = '1.3520';
5 205 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 205 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 205 ▶use Dancer::Cookie;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 205 ▶use Dancer::SharedData;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 205 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # all cookies defined by the application are store in that singleton
14 # this is a hashref the represent all key/value pairs to store as cookies
15 205 my $COOKIES = {};
16 407 ▶sub cookies {$COOKIES}
Subroutine cookies: called CC=1 SCAR=0.0
Pod documented
17  
18 sub init {
19 23 ▶ $COOKIES = parse_cookie_from_env();
Subroutine init: called CC=1 SCAR=0.0
Pod documented
20 }
21  
22 sub cookie {
23 101 ▶ my $class = shift;
Subroutine cookie: called CC=3 SCAR=11.0
Pod documented
24 101 my $name = shift;
25 101 my $value = shift;
26 101 ▶ defined $value && set_cookie( $class, $name, $value, @_ );
Branch: 1 branch
BranchTrueFalseTotal
defined $value and set_cookie($class, $name, $value, @_) 2 99 101
27 101 ▶ cookies->{$name} ? cookies->{$name}->value : undef;
Branch: 1 branch
BranchTrueFalseTotal
cookies()->{$name} ? : 66 35 101
28 }
29  
30 sub parse_cookie_from_env {
31 23 ▶ my $request = Dancer::SharedData->request;
Subroutine parse_cookie_from_env: called CC=8 SCAR=20.9
Pod documented
32 23 ▶ my $env = (defined $request) ? $request->env : {};
Branch: 1 branch
BranchTrueFalseTotal
defined $request ? : 23 0 23
33 23 ▶ my $env_str = $env->{COOKIE} || $env->{HTTP_COOKIE};
Condition100%3/3
$env->{'COOKIE'} || $env->{'HTTP_COOKIE'}
l!l&&r!l&&!r
1625
MC/DC100%2/2
$env->{'COOKIE'} || $env->{'HTTP_COOKIE'}
$env->{'COOKIE'} $env->{'HTTP_COOKIE'}
Truth table100%3/3
A || B
A= $env->{'COOKIE'}B= $env->{'HTTP_COOKIE'}
ABresult
1X1
011
000
34 23 ▶ return {} unless defined $env_str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $env_str 5 18 23
35  
36 18 my $cookies = {};
37 18 foreach my $cookie ( split( /[,;]\s?/, $env_str ) ) {
38 # here, we don't want more than the 2 first elements
39 # a cookie string can contains something like:
40 # cookie_name="foo=bar"
41 # we want `cookie_name' as the value and `foo=bar' as the value
42 21 my( $name, $value ) = split /\s*=\s*/, $cookie, 2;
43  
44 # catch weird entries like 'cookie1=foo;;cookie2=bar'
45 21 ▶ next unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 21 21
46  
47 21 my @values;
48 21 ▶ if ( defined $value && $value ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value ne "") 19 2 21
Condition66%2/3
defined $value and $value ne ""
!ll&&!rl&&r
2019
MC/DC50%1/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table66%2/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
49 19 @values = map { uri_unescape($_) } split( /[&;]/, $value );
50 }
51  
52 21 $cookies->{$name} =
53 Dancer::Cookie->new( name => $name, value => \@values );
54 }
55  
56 18 return $cookies;
57 }
58  
59 # set_cookie name => value,
60 # expires => time() + 3600, domain => '.foo.com'
61 # http_only => 0 # defaults to 1
62 sub set_cookie {
63 10 ▶ my ( $class, $name, $value, %options ) = @_;
Subroutine set_cookie: called CC=1 SCAR=0.0
Pod undocumented
64 10 my $cookie = Dancer::Cookie->new(
65 name => $name,
66 value => $value,
67 %options
68 );
69 10 Dancer::Cookies->set_cookie_object($name => $cookie);
70 }
71  
72 sub set_cookie_object {
73 44 ▶ my ($class, $name, $cookie) = @_;
Subroutine set_cookie_object: called CC=1 SCAR=0.0
Pod undocumented
74 44 Dancer::SharedData->response->add_cookie($name, $cookie);
75 44 Dancer::Cookies->cookies->{$name} = $cookie;
76 }
77  
78 205 1;
79  
80 __END__
« blib/lib/Dancer/SharedData.pm ↑ Summary blib/lib/Dancer/Factory/Hook.pm »