| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::Fatal; |
| 4 |
use Test::More; |
| 5 |
|
| 6 |
BEGIN { |
| 7 |
# Freeze time at Tue, 15-Jun-2010 00:00:00 GMT |
| 8 |
*CORE::GLOBAL::time = sub { return 1276560000 } |
| 9 |
} |
| 10 |
|
| 11 |
use Dancer2::Core::Cookie; |
| 12 |
use Dancer2::Core::Request; |
| 13 |
|
| 14 |
if ( Dancer2::Core::Cookie::_USE_XS ) { |
| 15 |
|
| 16 |
subtest 'with HTTP::XSCookies' => \&all_tests; |
| 17 |
|
| 18 |
no warnings 'redefine'; |
| 19 |
*Dancer2::Core::Cookie::to_header = \&Dancer2::Core::Cookie::pp_to_header; |
| 20 |
} |
| 21 |
else { |
| 22 |
diag "If you want extra speed, install HTTP::XSCookies"; |
| 23 |
} |
| 24 |
|
| 25 |
subtest 'w/o HTTP::XSCookies' => \&all_tests; |
| 26 |
|
| 27 |
sub all_tests { |
| 28 |
my $cookie = Dancer2::Core::Cookie->new( name => "foo" ); |
| 29 |
|
| 30 |
subtest "Constructor" => sub { |
| 31 |
isa_ok $cookie => 'Dancer2::Core::Cookie'; |
| 32 |
can_ok $cookie => 'to_header'; |
| 33 |
}; |
| 34 |
|
| 35 |
subtest "Setting values" => sub { |
| 36 |
is $cookie->value("foo") => "foo", "Can set value"; |
| 37 |
is $cookie->value => "foo", "Set value stuck"; |
| 38 |
|
| 39 |
is $cookie . "bar", "foobar", "Stringifies to desired value"; |
| 40 |
|
| 41 |
ok $cookie->value( [qw(a b c)] ), "can set multiple values"; |
| 42 |
is $cookie->value => 'a', "get first value in scalar context"; |
| 43 |
is_deeply [ $cookie->value ] => [qw(a b c)], |
| 44 |
"get all values in list context";; |
| 45 |
is_deeply [ $cookie->values ] => [qw(a b c)], |
| 46 |
"values returns all values"; |
| 47 |
is scalar $cookie->values => 3, |
| 48 |
"values returns arrayref in scalar context"; |
| 49 |
|
| 50 |
ok $cookie->value( { x => 1, y => 2 } ), "can set values with a hashref"; |
| 51 |
like $cookie->value => qr/^[xy]$/; # hashes doesn't store order... |
| 52 |
is_deeply [ sort $cookie->value ] => [ sort ( 1, 2, 'x', 'y' ) ]; |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
subtest "accessors and defaults" => sub { |
| 57 |
is $cookie->name => 'foo', "name is as expected"; |
| 58 |
is $cookie->name("bar") => "bar", "can change name"; |
| 59 |
is $cookie->name => 'bar', "name change stuck"; |
| 60 |
|
| 61 |
ok !$cookie->domain, "no domain set by default"; |
| 62 |
is $cookie->domain("dancer.org") => "dancer.org", |
| 63 |
"setting domain returns new value"; |
| 64 |
is $cookie->domain => "dancer.org", |
| 65 |
"new domain valjue stuck"; |
| 66 |
is $cookie->domain("") => "", "can clear domain"; |
| 67 |
ok !$cookie->domain, "no domain set now"; |
| 68 |
|
| 69 |
is $cookie->path => '/', "by default, path is /"; |
| 70 |
ok $cookie->has_path, "has_path"; |
| 71 |
is $cookie->path("/foo") => "/foo", "setting path returns new value"; |
| 72 |
ok $cookie->has_path, "has_path"; |
| 73 |
is $cookie->path => "/foo", "new path stuck"; |
| 74 |
|
| 75 |
ok !$cookie->secure, "no cookie secure flag by default"; |
| 76 |
is $cookie->secure(1) => 1, "enabling \$cookie->secure returns new value"; |
| 77 |
is $cookie->secure => 1, "\$cookie->secure flag is enabled"; |
| 78 |
is $cookie->secure(0) => 0, "disabling \$cookie->secure returns new value"; |
| 79 |
ok !$cookie->secure, "\$cookie->secure flag is disabled"; |
| 80 |
|
| 81 |
ok $cookie->http_only, "http_only by default"; |
| 82 |
is $cookie->http_only(0) => 0, |
| 83 |
"disabling \$cookie->http_only returns new value"; |
| 84 |
ok !$cookie->http_only, |
| 85 |
"\$cookie->http_only is now disabled"; |
| 86 |
|
| 87 |
like exception { $cookie->same_site('foo') }, |
| 88 |
qr/Value "foo" did not pass type constraint "Enum\["Strict","Lax","None"\]/; |
| 89 |
}; |
| 90 |
|
| 91 |
my %times = ( |
| 92 |
"+2" => "Tue, 15-Jun-2010 00:00:02 GMT", |
| 93 |
"+2h" => "Tue, 15-Jun-2010 02:00:00 GMT", |
| 94 |
"-2h" => "Mon, 14-Jun-2010 22:00:00 GMT", |
| 95 |
"1 hour" => "Tue, 15-Jun-2010 01:00:00 GMT", |
| 96 |
"3 weeks 4 days 2 hours 99 min 0 secs" => "Sat, 10-Jul-2010 03:39:00 GMT", |
| 97 |
"2 months" => "Sat, 14-Aug-2010 00:00:00 GMT", |
| 98 |
"12 years" => "Sun, 12-Jun-2022 00:00:00 GMT", |
| 99 |
|
| 100 |
1288817656 => "Wed, 03-Nov-2010 20:54:16 GMT", |
| 101 |
1288731256 => "Tue, 02-Nov-2010 20:54:16 GMT", |
| 102 |
1288644856 => "Mon, 01-Nov-2010 20:54:16 GMT", |
| 103 |
1288558456 => "Sun, 31-Oct-2010 20:54:16 GMT", |
| 104 |
1288472056 => "Sat, 30-Oct-2010 20:54:16 GMT", |
| 105 |
1288385656 => "Fri, 29-Oct-2010 20:54:16 GMT", |
| 106 |
1288299256 => "Thu, 28-Oct-2010 20:54:16 GMT", |
| 107 |
1288212856 => "Wed, 27-Oct-2010 20:54:16 GMT", |
| 108 |
|
| 109 |
# Anything not understood is passed through |
| 110 |
"basset hounds got long ears" => "basset hounds got long ears", |
| 111 |
"+2 something" => "+2 something", |
| 112 |
); |
| 113 |
|
| 114 |
subtest "expiration strings" => sub { |
| 115 |
my $min = 60; |
| 116 |
my $hour = 60 * $min; |
| 117 |
my $day = 24 * $hour; |
| 118 |
my $week = 7 * $day; |
| 119 |
my $mon = 30 * $day; |
| 120 |
my $year = 365 * $day; |
| 121 |
|
| 122 |
ok !$cookie->expires; |
| 123 |
|
| 124 |
for my $exp ( keys %times ) { |
| 125 |
my $want = $times{$exp}; |
| 126 |
|
| 127 |
$cookie->expires($exp); |
| 128 |
is $cookie->expires => $want, "expiry $exp => $want";; |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
subtest "to header" => sub { |
| 133 |
|
| 134 |
my @cake = ( |
| 135 |
{ cookie => { |
| 136 |
name => 'bar', |
| 137 |
value => 'foo', |
| 138 |
expires => '+2h', |
| 139 |
secure => 1 |
| 140 |
}, |
| 141 |
expected => sprintf( |
| 142 |
"bar=foo; Expires=%s; HttpOnly; Path=/; Secure", |
| 143 |
$times{'+2h'}, |
| 144 |
), |
| 145 |
}, |
| 146 |
{ cookie => { |
| 147 |
name => 'bar', |
| 148 |
value => 'foo', |
| 149 |
domain => 'dancer.org', |
| 150 |
path => '/dance', |
| 151 |
http_only => 1 |
| 152 |
}, |
| 153 |
expected => "bar=foo; Domain=dancer.org; HttpOnly; Path=/dance", |
| 154 |
}, |
| 155 |
{ cookie => { |
| 156 |
name => 'bar', |
| 157 |
value => 'foo', |
| 158 |
}, |
| 159 |
expected => "bar=foo; HttpOnly; Path=/", |
| 160 |
}, |
| 161 |
{ cookie => { |
| 162 |
name => 'bar', |
| 163 |
value => 'foo', |
| 164 |
http_only => 0, |
| 165 |
}, |
| 166 |
expected => "bar=foo; Path=/", |
| 167 |
}, |
| 168 |
{ cookie => { |
| 169 |
name => 'bar', |
| 170 |
value => 'foo', |
| 171 |
http_only => '0', |
| 172 |
}, |
| 173 |
expected => "bar=foo; Path=/", |
| 174 |
}, |
| 175 |
{ cookie => { |
| 176 |
name => 'same-site', |
| 177 |
value => 'strict', |
| 178 |
same_site => 'Strict', |
| 179 |
}, |
| 180 |
expected => 'same-site=strict; HttpOnly; Path=/; SameSite=Strict', |
| 181 |
}, |
| 182 |
{ cookie => { |
| 183 |
name => 'same-site', |
| 184 |
value => 'lax', |
| 185 |
same_site => 'Lax', |
| 186 |
}, |
| 187 |
expected => 'same-site=lax; HttpOnly; Path=/; SameSite=Lax', |
| 188 |
}, |
| 189 |
); |
| 190 |
|
| 191 |
for my $cook (@cake) { |
| 192 |
my $c = Dancer2::Core::Cookie->new(%{$cook->{cookie}}); |
| 193 |
# name=value; sorted fields |
| 194 |
my @a = split /; /, $c->to_header; |
| 195 |
is join("; ", shift @a, sort @a), $cook->{expected}; |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
subtest 'multi-value' => sub { |
| 200 |
my $c = Dancer2::Core::Cookie->new( name => 'foo', value => [qw/bar baz/] ); |
| 201 |
|
| 202 |
is $c->to_header, 'foo=bar&baz; Path=/; HttpOnly'; |
| 203 |
|
| 204 |
my $r = Dancer2::Core::Request->new( env => { HTTP_COOKIE => 'foo=bar&baz' } ); |
| 205 |
|
| 206 |
is_deeply [ $r->cookies->{foo}->value ], [qw/bar baz/]; |
| 207 |
}; |
| 208 |
} |
| 209 |
|
| 210 |
done_testing; |