Dancer2-Session-DBIC
view release on metacpan or search on metacpan
get '/id' => sub {
return session->id;
};
get '/getfoo' => sub {
return session('foo');
};
get '/putfoo' => sub {
session foo => 'bar';
return session('foo');
};
get '/getcamel' => sub {
return session('camel');
};
get '/putcamel' => sub {
session camel => 'ã©ã¯ã';
return session('camel');
};
get '/change_session_id' => sub {
if ( app->can('change_session_id') ) {
# Dancer2 > 0.200003
app->change_session_id;
}
else {
return "unsupported";
}
};
get '/destroy' => sub {
if (app->can('destroy_session')) {
app->destroy_session;
}
# legacy
else {
context->destroy_session;
}
return "Session destroyed";
};
get '/sessionid' => sub {
return session->id;
};
}
my $app = Dancer2->runner->psgi_app;
is( ref $app, 'CODE', 'Got app' );
test_psgi $app, sub {
my $cb = shift;
my $res = $cb->( GET '/sessionid' );
my $newid = $res->decoded_content;
# extract the cookie
my $cookie = $res->header('Set-Cookie');
$cookie =~ s/^(.*?);.*$/$1/s;
ok ($cookie, "Got the cookie: $cookie");
my @headers = (Cookie => $cookie);
my $session_id = $cb->( GET '/id', @headers)->decoded_content;
like(
$session_id,
qr/^[0-9a-z_-]+$/i,
'Retrieve session id',
);
is(
$cb->( GET '/getfoo', @headers )->decoded_content,
'',
'Retrieve pristine foo key',
);
cmp_ok $schema->resultset($rs_expected)->count, '==', 1,
"One session in the database";
ok $schema->resultset($rs_expected)
->find( { $pk_expected => $session_id } ),
"Database record found with correct session id";
is(
$cb->( GET '/putfoo', @headers )->decoded_content,
'bar',
'Set foo key to bar',
);
is(
$cb->( GET '/getfoo', @headers )->decoded_content,
'bar',
'Retrieve foo key which is "bar" now',
);
is(
$cb->( GET '/getcamel', @headers )->decoded_content,
'',
'Retrieve pristine camel key',
);
is(
$cb->( GET '/putcamel', @headers )->decoded_content,
'ã©ã¯ã',
'Set camel key to ã©ã¯ã',
);
is(
$cb->( GET '/getcamel', @headers )->decoded_content,
'ã©ã¯ã',
'Retrieve camel key which is "ã©ã¯ã" now',
);
like(
$cb->( GET '/sessionid', @headers )->decoded_content,
qr/\w/,
"Found session id",
);
my $oldid = $cb->( GET '/sessionid', @headers )->decoded_content;
is($oldid, $newid, "Same id, session holds");
$res = $cb->( GET '/change_session_id', @headers );
$newid = $res->decoded_content;
SKIP: {
skip "This Dancer2 version does not support change_session_id", 5
unless $newid ne 'unsupported';
note "testing change_session_id";
isnt( $oldid, $newid,
"after change_session_id we have new ID in response" );
$cookie = $res->header('Set-Cookie');
$cookie =~ s/^(.*?);.*$/$1/s;
ok( $cookie, "Got the cookie: $cookie" );
@headers = ( Cookie => $cookie );
( $newid = $cookie ) =~ s/.+=//;
isnt( $oldid, $newid,
"after change_session_id session cookie value changed" );
cmp_ok $schema->resultset($rs_expected)->count, '==', 1,
"One session in the database";
ok $schema->resultset($rs_expected)
->find( { $pk_expected => $newid } ),
"Database record found with correct session id";
}
is(
$cb->( GET '/destroy', @headers)->decoded_content,
'Session destroyed',
'Session destroyed without crashing',
);
is(
$cb->( GET '/getfoo', @headers )->decoded_content,
'',
'Retrieve pristine foo key after destroying',
);
$newid = $cb->( GET '/sessionid', @headers )->decoded_content;
ok($newid ne $oldid, "New and old ids differ");
};
return $dbic_session;
}
done_testing;
( run in 2.019 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )