Debian Codegraph @

t/scope_problems/with_return_dies.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 use Scalar::Util qw/refaddr/;
14
15 hook before_template_render => sub {
16 my $path = request->path;
17 my $refadd = refaddr(app->with_return);
18 if ( $path =~ m!route_with_renderer_error! ) {
19 die $refadd;
20 }
21 };
22
23 get '/route_with_renderer_error' => sub {
24 ## This route first gets called, then template fires the above hook.
25 ## This hook errors, causing Dancer2::Core::App, to throw an error
26 ## which *also* fires the hook, crashing the server.
27 my $addr = refaddr(app->with_return);
28 template \$addr;
29 };
30
31 get '/normal_route' => sub {
32 ## This should issue normally
33 # my $addr = refaddr(app->with_return);
34 # template \$addr;
35 return refaddr(app->with_return);
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 my $res1 = $test->request(GET '/normal_route');
50 ok($res1->is_success, '/normal_route does not error');
51 my $refaddr1 = $res1->decoded_content;
52
53 ## This route should die
54 my $res2 = $test->request(GET '/route_with_renderer_error');
55 ok(! $res2->is_success, '/route_with_renderer_error errors errors');
56 my ($refaddr2) = $res2->decoded_content =~ /Hook error: (\d+)/;
57
58 ## The first route now errors
59 ## I can't seem to force with_return to fail in this test, even though I have
60 ## it failing in production.
61 ## So instead I'll check the refaddr of the with_return
62 ## If refaddr of with_return is the same between route2 and route3, then this
63 ## demonstrates that with_return has not been cleared between the two routes
64 ## And that /normal_route is now using the wrong with_return.
65 ## Possibly the old with_return hasn't been cleaned up? not sure.
66 my $res3 = $test->request(GET '/normal_route');
67 ok($res3->is_success, '/normal_route does not error');
68 my $refaddr3 = $res3->decoded_content;
69 isnt($refaddr1, $refaddr3, 'The 3rd request has a different with_return from the first run');
70 isnt($refaddr2, $refaddr3, 'The 3rd request has a different with_return from the second run');
71
72 done_testing();