File Coverage

blib/lib/Kelp/Test/CookieJar.pm
Criterion Covered Total %
statement 30 37 81.0
branch 3 8 37.5
condition 3 5 60.0
subroutine 6 7 85.7
pod 0 5 0.0
total 42 62 67.7


line stmt bran cond sub pod time code
1             package Kelp::Test::CookieJar;
2              
3 29     29   206 use Kelp::Base;
  29         55  
  29         230  
4 29     29   18310 use URI::Escape;
  29         53333  
  29         23727  
5              
6             # Stripped-down HTTP::Cookies interface for testing purposes and proper url escaping
7              
8             attr cookies => sub { {} };
9              
10             sub set_cookie
11             {
12 2     2 0 15 my ($self, undef, $name, $value) = @_;
13              
14 2         6 $self->cookies->{$name} = $value;
15 2         4 return 1;
16             }
17              
18             sub get_cookies
19             {
20 1     1 0 4 my ($self, undef, @names) = @_;
21              
22 1         3 my %ret;
23              
24 1 50       4 if (@names) {
25 1 50       3 return $self->cookies->{$names[0]}
26             unless wantarray;
27              
28 1         2 return map { $self->cookies->{$_} } @names;
  2         5  
29             }
30             else {
31 0         0 return $self->cookies;
32             }
33             }
34              
35             sub clear
36             {
37 0     0 0 0 my ($self, undef, undef, $name) = @_;
38              
39 0 0       0 if ($name) {
40 0         0 delete $self->cookies->{$name};
41             }
42             else {
43 0         0 %{$self->cookies} = ();
  0         0  
44             }
45              
46 0         0 return $self;
47             }
48              
49             sub add_cookie_header
50             {
51 251     251 0 675 my ($self, $request) = @_;
52              
53 251         426 my %c = %{$self->cookies};
  251         845  
54 251         889 my @vals = map { uri_escape($_) . '=' . uri_escape($c{$_}) } keys %c;
  5         61  
55 251         1965 $request->header(Cookie => join '; ', @vals);
56              
57 251         23076 return $request;
58             }
59              
60             sub extract_cookies
61             {
62 251     251 0 640 my ($self, $response) = @_;
63              
64 251   100     918 my @headers = split ', ', $response->header('Set-Cookie') // '';
65 251         16711 foreach my $header (@headers) {
66 1         4 my $cookie = (split /; /, $header)[0];
67 1         5 my ($name, $value) = split '=', $cookie;
68              
69 1 50 33     6 next unless defined $name && defined $value;
70 1         4 $self->set_cookie(undef, uri_unescape($name), uri_unescape($value));
71             }
72              
73 251         666 return $response;
74             }
75              
76             1;
77