#!/usr/bin/env perl
plugin
'WithCSRFProtection'
;
post
'/example_with_condition'
=> (
with_csrf_protection
=> 1 );
app->routes->post(
'/example_with_shortcut'
)
->with_csrf_protection->to(
'#example_with_shortcut'
);
get
'/token'
=>
sub
{
my
(
$c
) =
@_
;
$c
->render(
text
=>
$c
->csrf_token );
};
my
$t
= Test::Mojo->new;
my
$token
=
$t
->get_ok(
'/token'
)->status_is(200)->tx->res->text;
for
my
$path
(
qw( example_with_condition example_with_shortcut )
) {
$t
->post_ok(
"/$path"
)->status_is(403)
->content_like(
qr/Failed CSRF check/
);
$t
->post_ok(
"/$path?csrf_token=wrong"
)->status_is(403);
$t
->post_ok(
"/$path?csrf_token=$token"
)->status_is(200);
$t
->post_ok(
"/$path"
=>
form
=> {
csrf_token
=>
'wrong'
} )
->status_is(403);
$t
->post_ok(
"/$path"
=>
form
=> {
csrf_token
=>
$token
} )
->status_is(200);
$t
->post_ok(
"/$path"
=> {
'X-CSRF-Token'
=>
'wrong'
} )->status_is(403);
$t
->post_ok(
"/$path"
=> {
'X-CSRF-Token'
=>
$token
} )->status_is(200);
}