Debian Codegraph @

t/issues/gh-944.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Plack::Test;
6 use HTTP::Request::Common;
7
8 {
9 package RouteContentTest; ## no critic
10 use Dancer2;
11 set serializer => 'JSON';
12
13 hook before => sub {
14 return if request->path eq '/content';
15 response->content({ foo => 'bar' });
16 response->halt;
17 };
18
19 get '/' => sub {1};
20
21 get '/content' => sub {
22 response->content({ foo => 'bar' });
23 return 'this is ignored';
24 };
25 }
26
27 my $test = Plack::Test->create( RouteContentTest->to_app );
28
29 subtest "response set in before hook" => sub {
30 my $res = $test->request( GET '/' );
31 ok( $res->is_success, 'Successful request' );
32 is( $res->content, '{"foo":"bar"}', 'Correct content' );
33 };
34
35 subtest "response content set in route" => sub {
36 my $res = $test->request( GET '/content' );
37 ok( $res->is_success, 'Successful request' );
38 isnt( $res->content, 'this is ignored', 'route return value ignored' );
39 is( $res->content, '{"foo":"bar"}', 'Correct content' );
40 };
41
42 done_testing();
43