Debian Codegraph @

t/auto_page.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 AutoPageTest;
10 use Dancer2;
11
12 set auto_page => 1;
13 set views => 't/views';
14 set layout => 'main';
15 set charset => 'UTF-8';
16 }
17
18
19 my @engines = ('tiny');
20 eval {require Template; Template->import(); push @engines, 'template_toolkit';};
21
22 for my $tt_engine ( @engines ) {
23 # Change template engine and run tests
24 AutoPageTest::set( template => $tt_engine );
25 subtest "autopage with template $tt_engine" => \&run_tests;
26 }
27
28 sub run_tests {
29 my $test = Plack::Test->create( AutoPageTest->to_app );
30
31 {
32 my $r = $test->request( GET '/auto_page' );
33
34 is( $r->code, 200, 'Autopage found the page' );
35 # ö is U+00F6 or c3 b6 when encoded as bytes
36 like(
37 $r->content,
38 qr/---\nHey! This is Auto Page w\x{c3}\x{b6}rking/,
39 '...with proper content',
40 );
41
42 is(
43 $r->headers->content_type,
44 'text/html',
45 'auto page has correct content type header',
46 );
47
48 is(
49 $r->headers->content_type_charset,
50 'UTF-8',
51 'auto page has correct charset in content type header',
52 );
53
54 is(
55 $r->headers->content_length,
56 98, # auto_page.tt+layouts/main.tt processed. ö has two bytes in UTF-8
57 'auto page has correct content length header',
58 );
59 }
60
61 {
62 my $r = $test->request( GET '/folder/page' );
63
64 is( $r->code, 200, 'Autopage found the page under a folder' );
65 like(
66 $r->content,
67 qr/---\nPage under folder/,
68 '...with proper content',
69 );
70 }
71
72 {
73 my $r = $test->request( GET '/non_existent_page' );
74 is( $r->code, 404, 'Autopage doesn\'t try to render nonexistent pages' );
75 }
76
77 {
78 my $r = $test->request( GET '/layouts/main');
79 is( $r->code, 404, 'Layouts are not served' );
80 }
81
82 {
83 my $r = $test->request( GET '/file.txt' );
84 is( $r->code, 200, 'found file on public with autopage' );
85 is(
86 $r->content,
87 "this is a public file\n",
88 '[GET /file.txt] Correct content',
89 );
90
91 like(
92 $r->headers->content_type,
93 qr{text/plain},
94 'public served file has correct content type header',
95 );
96 }
97 }
98
99 done_testing;