|
Hello, |
Replies: 2 comments 1 reply
|
The CSRF token is stored in your active session, |
From the Mojolicious test suite, t/mojolicious/validation_lite_app.t: use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::Mojo;
use Test::More;
use Mojolicious::Lite;
any '/forgery' => sub {
my $c = shift;
my $v = $c->validation;
return $c->render unless $v->has_data;
$v->csrf_protect->required('foo');
};
my $t = Test::Mojo->new;
subtest 'Correct CSRF token' => sub {
# See also: https://docs.mojolicious.org/Mojolicious/Guides/Testing#Cookies-and-session-management
my $token = $t->ua->get('/forgery')->res->dom->at('[name=csrf_token]')->val;
$t->post_ok('/forgery' => form => {csrf_token => $token, foo => 'bar'})
->status_is(200)
->content_unlike(qr/Wrong or missing CSRF token!/)
->element_exists('[value=bar]')
->element_exists_not('.field-with-error')
->element_count_is('[name=csrf_token]', 2)
->element_count_is('form', 2)
->element_exists('form > input[name=csrf_token] + input[type=submit]');
isnt $t->tx->res->dom->find('[name=csrf_token]')->[0]->val, $t->tx->res->dom->find('[name=csrf_token]')->[1]->val,
'different masked tokens';
};
done_testing;
__DATA__
@@ forgery.html.ep
%= form_for forgery => begin
%= 'Wrong or missing CSRF token!' if validation->has_error('csrf_token')
%= csrf_field
%= text_field 'foo'
%= end
%= csrf_button_to Root => '/'$ prove /tmp/csrf.pl
/tmp/csrf.pl .. ok
All tests successful.
Files=1, Tests=1, 0 wallclock secs ( 0.01 usr 0.00 sys + 0.10 cusr 0.01 csys = 0.12 CPU)
Result: PASS |
From the Mojolicious test suite, t/mojolicious/validation_lite_app.t: