File Coverage

blib/lib/Test/Mojo/Session.pm
Criterion Covered Total %
statement 47 48 97.9
branch 2 4 50.0
condition 6 15 40.0
subroutine 12 12 100.0
pod 8 8 100.0
total 75 87 86.2


line stmt bran cond sub pod time code
1             package Test::Mojo::Session;
2              
3 1     1   627169 use Mojo::Base 'Test::Mojo';
  1         2  
  1         11  
4 1     1   6887 use Mojo::Util qw(b64_decode hmac_sha1_sum);
  1         3  
  1         50  
5 1     1   6 use Mojo::JSON 'decode_json';
  1         2  
  1         798  
6              
7             our $VERSION = '1.07';
8              
9             sub new {
10 1     1 1 651 my $self = shift->SUPER::new(@_);
11 1         15 return $self;
12             }
13              
14             # Compatibility hack for Mojolicious < 8.36
15             sub test {
16 9 50   9 1 23909 if (Test::Mojo->can('test')) {
17 9         33 return shift->SUPER::test(@_);
18             }
19 0         0 return shift->SUPER::_test(@_);
20             }
21              
22             sub session_has {
23 1     1 1 448 my ($self, $p, $desc) = @_;
24 1   33     14 $desc //= qq{session has value for JSON Pointer "$p"};
25 1         3 my $session = $self->_extract_session;
26 1         40 return $self->test('ok', !!Mojo::JSON::Pointer->new($session)->contains($p), $desc);
27             }
28              
29             sub session_hasnt {
30 1     1 1 445 my ($self, $p, $desc) = @_;
31 1   33     10 $desc //= qq{session has no value for JSON Pointer "$p"};
32 1         2 my $session = $self->_extract_session;
33 1         32 return $self->test('ok', !Mojo::JSON::Pointer->new($session)->contains($p), $desc);
34             }
35              
36             sub session_is {
37 2     2 1 878 my ($self, $p, $data, $desc) = @_;
38 2   66     14 $desc //= qq{session exact match for JSON Pointer "$p"};
39 2         5 my $session = $self->_extract_session;
40 2         59 return $self->test('is_deeply', Mojo::JSON::Pointer->new($session)->get($p), $data, $desc);
41             }
42              
43             sub session_like {
44 1     1 1 782 my ($self, $p, $regex, $desc) = @_;
45 1   33     5 $desc //= qq{session regular expression match for JSON Pointer "$p"};
46 1         3 my $session = $self->_extract_session;
47 1         43 return $self->test('like', Mojo::JSON::Pointer->new($session)->get($p), qr/$regex/, $desc);
48             }
49              
50             sub session_unlike {
51 1     1 1 434 my ($self, $p, $regex, $desc) = @_;
52 1   33     4 $desc //= qq{session negated regular expression match for JSON Pointer "$p"};
53 1         3 my $session = $self->_extract_session;
54 1         31 return $self->test('unlike', Mojo::JSON::Pointer->new($session)->get($p), qr/$regex/, $desc);
55             }
56              
57             sub session_ok {
58 1     1 1 837 my $self = shift;
59 1         5 my $session = $self->_extract_session;
60 1         35 return $self->test('ok', !!$session, 'session ok');
61             }
62              
63             sub _extract_session {
64 7     7   16 my $self = shift;
65              
66 7         28 my $app = $self->app;
67 7         149 my $sessions = $app->sessions;
68 7         37 my $c = $app->build_controller;
69 7         539 my $name = $sessions->cookie_name;
70 7 50       32 return unless my $cookie = (grep { $_->name eq $name } @{$self->ua->cookie_jar->all})[0];
  7         129  
  7         16  
71              
72 7         95 $c->req->cookies($cookie);
73 7         1248 $sessions->load($c);
74 7         5062 return $c->session;
75             }
76              
77             1;
78              
79             __END__