Debian Codegraph @

t/scope_problems/session_is_cleared.t

raw
1 #!/usr/bin/env perl
2
3 ## This test will first cause a (legitimate) error in a
4 ## before_template_render hook
5 ## Then it will fetch an unrelated route that should return normally.
6 ## However, this route is now using the wrong with_return block.
7 ## This is because the first route, errors in rendering the *error* page.
8 ## This cause the block to die, and with_return is never unset.
9 ## This test uses two template files
10
11 package MyTestApp;
12 use Dancer2;
13
14 hook before_template_render => sub {
15 my $path = request->path;
16 if ( $path =~ m!route_with_renderer_error! ) {
17 die session->id;
18 }
19 };
20
21 get '/route_with_renderer_error' => sub {
22 ## This route first gets called, then template fires the above hook.
23 ## This hook errors, causing Dancer2::Core::App, to throw an error
24 ## which *also* fires the hook, crashing the server.
25 session->write('bob' => "I SHOULD NOT BE IN THE NEXT SESSION");
26 my $tt = session->id;
27 template \"$tt";
28 };
29
30 get '/normal_route' => sub {
31 ## This should issue normally
32 if ( !session('bob') ) {
33 session('bob' => "test" . rand());
34 }
35 return session('bob');
36 };
37
38 package main;
39 use strict;
40 use warnings;
41 use Test::More;
42 use Plack::Test;
43 use HTTP::Request::Common qw/GET/;
44 use Dancer2;
45
46 my $test = Plack::Test->create(Dancer2->psgi_app);
47
48 ## This route works fine
49 ## Just a sanity check
50 my $res1 = $test->request(GET '/normal_route');
51 ok($res1->is_success, '/normal_route does not error');
52
53 ## This route should die and cause a broken state
54 my $res2 = $test->request(GET '/route_with_renderer_error');
55 ok(! $res2->is_success, '/route_with_renderer_error errors errors');
56
57 ## This route will now have the same session as the previous route.
58 ## Despite not having any cookies...
59 my $res3 = $test->request(GET '/normal_route');
60 ok($res3->is_success, '/normal_route does not error');
61 my $session_value = $res3->decoded_content;
62 isnt($session_value, "I SHOULD NOT BE IN THE NEXT SESSION",
63 '3rd route does not have session value from second route');
64
65 done_testing();