aboutsummaryrefslogtreecommitdiff
path: root/t/basic.t
blob: 936f6c43e76919a658f47644e3410b21c69658ae (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
use strict;
use warnings FATAL => 'all';

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

BEGIN { plan tests => 7; }

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

# Read contents of header and footer

open my $h, '<', 't/htdocs/head.html'
	or die "Could not open < t/htdocs/head.html: $!";
open my $f, '<', 't/htdocs/foot.html'
	or die "Could not open < t/htdocs/foot.html: $!";
$head = do { local $/; <$h> };
$foot = do { local $/; <$f> };
close $h; close $f;

# Helper for replacing file contents

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

# Run tests

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

mkdir 't/htdocs/subdir' if ! -d 't/htdocs/subdir';
set 't/htdocs/subdir/.htaccess', <<CONF;
Inject head.html
CONF

@body = ("<title>Test</title>\n", "This is a test page.\n");
set '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");
set '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");
set '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");
set '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");
set 't/htdocs/test.html', @body;
ok GET_BODY('/test.html'), "${body[0]}$head${body[1]}$foot",
   '<!doctype> with leading newline';

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

set 't/htdocs/subdir/.htaccess', <<CONF;
Inject " "
CONF

set 't/htdocs/ ', 'head'; # needed in case " " is (incorrectly) accepted
ok GET_BODY('/subdir/test.html') eq "head${body[0]}" && 1 || 0, 0,
   'single-space argument should fail';

unlink 't/htdocs/.htaccess';
unlink 't/htdocs/test.html';
unlink 't/htdocs/subdir/.htaccess';
unlink 't/htdocs/subdir/test.html';
unlink 't/htdocs/ ';