aboutsummaryrefslogtreecommitdiff
path: root/t/basic.t
blob: 8e5a37bd146d435c9c17efb4d3619c5c4c364fe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use strict;
use warnings FATAL => 'all';

use Apache::Test;
use Apache::TestUtil;
use Apache::TestRequest qw/GET_BODY/;

BEGIN { plan tests => 9; }

# Prepare environment

my $head; # expected page header
my $foot; # expected page footer
my @body; # sections of page body

# Helper for replacing file contents

sub overwrite {
	my $file = shift;
	open my $fh, '>', $file or die "Could not open > $file: $!";
	print $fh join('', @_);
	close $fh;
}

# Run tests

mkdir 't/htdocs/subdir' if ! -d 't/htdocs/subdir';

$head = <<HEAD;
This is a test header.
HEAD
overwrite 't/htdocs/head.html', $head;

$foot = <<HEAD;
This is a test footer.
HEAD
overwrite 't/htdocs/foot.html', $foot;

overwrite 't/htdocs/.htaccess', <<CONF;
Inject head.html foot.html
CONF

@body = ("<title>Test</title>\n", "This is a test page.\n");
overwrite 't/htdocs/test.html', @body;
ok GET_BODY('/test.html'), "${body[0]}$head${body[1]}$foot",
   '<head>-less head';

@body = ("<head>...</head>\n", "This is a test page.\n");
overwrite 't/htdocs/test.html', @body;
ok GET_BODY('/test.html'), "${body[0]}$head${body[1]}$foot",
   '<head>-ful head';

@body = ("<html>\n", "This is a test page.\n", "</html>\n");
overwrite 't/htdocs/test.html', @body;
ok GET_BODY('/test.html'), "${body[0]}$head${body[1]}$foot${body[2]}",
   '<html>-wrapped document';

@body = ("<!doctype html>\n", "This is a test page.\n");
overwrite 't/htdocs/test.html', @body;
ok GET_BODY('/test.html'), "${body[0]}$head${body[1]}$foot",
   '<!doctype>';

@body = ("\n<!doctype html>\n", "This is a test page.\n");
overwrite 't/htdocs/test.html', @body;
ok GET_BODY('/test.html'), "${body[0]}$head${body[1]}$foot",
   '<!doctype> with leading newline';

@body = ("<!-- quirks mode -->\n<!doctype html><!-- test -->\n", "This is a test page.\n");
overwrite 't/htdocs/test.html', @body;
ok GET_BODY('/test.html'), "${body[0]}$head${body[1]}$foot",
   'comments in <head>';

@body = ("<body>\n", "This is a test page.\n");
overwrite 't/htdocs/test.html', @body;
ok GET_BODY('/test.html'), "${body[0]}$head${body[1]}$foot",
   'document with <body>';

overwrite 't/htdocs/subdir/.htaccess', <<CONF;
Inject head.html
CONF

@body = ("This is a test page.\n");
overwrite 't/htdocs/subdir/test.html', @body;
ok GET_BODY('/subdir/test.html'), "$head${body[0]}",
   'different injection in subdirectory';

overwrite 't/htdocs/.htaccess', <<CONF;
Inject - head.html
CONF

@body = ("This is a test page.\n");
overwrite 't/htdocs/test.html', @body;
ok GET_BODY('/test.html'), "${body[0]}$head",
   'only footer';

unlink 't/htdocs/.htaccess';
unlink 't/htdocs/head.html';
unlink 't/htdocs/foot.html';
unlink 't/htdocs/test.html';
unlink 't/htdocs/subdir/.htaccess';
unlink 't/htdocs/subdir/test.html';
rmdir 't/htdocs/subdir';