| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Plack::Test; |
| 5 |
use HTTP::Cookies; |
| 6 |
use HTTP::Request::Common; |
| 7 |
|
| 8 |
{ |
| 9 |
package App; |
| 10 |
use Dancer2; |
| 11 |
|
| 12 |
setting( |
| 13 |
engines => { |
| 14 |
session => { |
| 15 |
Simple => { |
| 16 |
cookie_name => 'dancer.sid', |
| 17 |
cookie_path => '/foo', |
| 18 |
cookie_duration => '1 hour', |
| 19 |
cookie_same_site => 'Strict', |
| 20 |
is_http_only => 0, # will not show up in cookie |
| 21 |
}, |
| 22 |
}, |
| 23 |
} |
| 24 |
); |
| 25 |
|
| 26 |
setting( session => 'Simple' ); |
| 27 |
|
| 28 |
get '/has_session' => sub { |
| 29 |
return app->has_session; |
| 30 |
}; |
| 31 |
|
| 32 |
get '/foo/set_session/*' => sub { |
| 33 |
my ($name) = splat; |
| 34 |
session name => $name; |
| 35 |
}; |
| 36 |
|
| 37 |
get '/foo/read_session' => sub { |
| 38 |
my $name = session('name') || ''; |
| 39 |
"name='$name'"; |
| 40 |
}; |
| 41 |
|
| 42 |
get '/foo/destroy_session' => sub { |
| 43 |
my $name = session('name') || ''; |
| 44 |
app->destroy_session; |
| 45 |
return "destroyed='$name'"; |
| 46 |
}; |
| 47 |
} |
| 48 |
|
| 49 |
my $test = Plack::Test->create( App->to_app ); |
| 50 |
my $url = 'http://localhost'; |
| 51 |
|
| 52 |
my $jar = HTTP::Cookies->new; |
| 53 |
|
| 54 |
subtest 'Set session' => sub { |
| 55 |
my $res = $test->request( GET "$url/foo/set_session/larry" ); |
| 56 |
ok( $res->is_success, '/foo/set_session/larry' ); |
| 57 |
|
| 58 |
$jar->extract_cookies($res); |
| 59 |
ok( $jar->as_string, 'session cookie set' ); |
| 60 |
|
| 61 |
my ( $expires, $domain, $path, $opts ); |
| 62 |
my $cookie = $jar->scan( sub { |
| 63 |
( $expires, $domain, $path, $opts ) = @_[ 8, 4, 3, 10 ]; |
| 64 |
} ); |
| 65 |
|
| 66 |
my $httponly = $opts->{'HttpOnly'}; |
| 67 |
|
| 68 |
ok $expires - time > 3540, |
| 69 |
"cookie expiration is in future"; |
| 70 |
|
| 71 |
is $domain, 'localhost.local', "cookie domain set"; |
| 72 |
is $path, '/foo', "cookie path set"; |
| 73 |
is $httponly, undef, "cookie has not set HttpOnly"; |
| 74 |
is $opts->{SameSite}, 'Strict', "cookie has same site set to strict"; |
| 75 |
|
| 76 |
# read value back |
| 77 |
}; |
| 78 |
|
| 79 |
subtest 'Read session' => sub { |
| 80 |
my $req = GET "$url/foo/read_session"; |
| 81 |
$jar->add_cookie_header($req); |
| 82 |
|
| 83 |
my $res = $test->request($req); |
| 84 |
ok $res->is_success, "/foo/read_session"; |
| 85 |
like $res->content, qr/name='larry'/, "session value looks good"; |
| 86 |
}; |
| 87 |
|
| 88 |
done_testing; |