Skip to content

Commit

Permalink
Make session creation lazier
Browse files Browse the repository at this point in the history
Don't create a session just because someone attempted to read data from
one.  I.e. just because someone checks C< session("logged_in") >> don't
create a new, empty session.

Addresses GH Issue #155
  • Loading branch information
xdg committed Mar 4, 2013
1 parent 2ded636 commit e34b070
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lib/Dancer2/Core/App.pm
Expand Up @@ -150,6 +150,12 @@ sub engine {
sub session {
my ($self, $key, $value) = @_;

# shortcut reads if no session exists, so we don't
# instantiate sessions for no reason
if ( @_ == 2 ) {
return unless $self->context->has_session;
}

my $session = $self->context->session;
croak "No session available, a session engine needs to be set"
if !defined $session;
Expand Down
7 changes: 3 additions & 4 deletions t/session_lifecycle.t
Expand Up @@ -51,21 +51,20 @@ foreach my $engine (@engines) {
ok !$cookie, "no cookie set"
or diag explain $cookie;

# empty session created if session read attempted
# no empty session created if session read attempted
$res = $ua->get("http://127.0.0.1:$port/read_session");
ok $res->is_success, "/read_session";
$cookie = extract_cookie($res);
ok $cookie, "session cookie set"
ok !$cookie, "no cookie set"
or diag explain $cookie;
my $sid1 = $cookie->{"dancer.session"};
like $res->content, qr/name=''/, "empty session";

# set value into session
$res = $ua->get("http://127.0.0.1:$port/set_session/larry");
ok $res->is_success, "/set_session/larry";
$cookie = extract_cookie($res);
ok $cookie, "session cookie set"
or diag explain $cookie;
my $sid1 = $cookie->{"dancer.session"};

# read value back
$res = $ua->get("http://127.0.0.1:$port/read_session");
Expand Down

1 comment on commit e34b070

@akarelas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done. The privacy activists won't be all over us now.

Please sign in to comment.