2.01KiB; Perl | 2020-11-23 18:45:15+01 | Statements 32 | SLOC 65
1
#!perl -w
2
3
# Test if CGI::Buffer adds Content-Length and Etag headers, also simple
4
# check that optimise_content and gzips does something.
5
6
# TODO: check optimise_content and gzips do the *right* thing
7
# TODO: check ETags are correct
8
9
use strict;
10
use warnings;
11
12
use Test::Most tests => 9;
13
use Test::TempDir::Tiny;
14
# use Test::NoWarnings;	# HTML::Clean has them
15
eval 'use autodie qw(:all)';	# Test for open/close failures
16
17
BEGIN {
18
	use_ok('CGI::Buffer');
19
}
20
21
OUTPUT: {
22
	delete $ENV{'HTTP_ACCEPT_ENCODING'};
23
	delete $ENV{'SERVER_PROTOCOL'};
24
25
	my $input = << 'EOF';
26
	use CGI::Buffer;
27
28
	CGI::Buffer::set_options(optimise_content => 2);
29
30
	print "Content-type: text/html; charset=ISO=8859-1";
31
	print "\n\n";
32
33
	print "<HTML><BODY>\n";
34
	print "document.write(\"1\");\n";
35
	print "document.write(\"2\");\n";
36
	print "<script type=\"text/javascript\">\n";
37
	print "var i = 1;\n";
38
	print "document.write(\"foo\");\n";
39
	print "document.write(\"bar\");\n";
40
	print "var j = 1;\n";
41
	print "document.write(\"a\");\n";
42
	print "document.write(\"b\");\n";
43
	print "</script>\n";
44
	print "Hello World!\n";
45
	print "<script type=\"text/javascript\">\n";
46
	print "document.write(\"a\");\n";
47
	print "document.write(\"b\");\n";
48
	print "</script>\n";
49
	print "<script type=\"text/javascript\">\n";
50
	print "document.write(\"fred\");\n";
51
	print "var k = 1;\n";
52
	print "document.write(\"wilma\");\n";
53
	print "</script>\n";
54
	print "</body>\n";
55
EOF
56
57
	my $filename = tempdir() . 'js.t';
58
	open(my $tmp, '>', $filename);
59
	print $tmp $input;
60
61
	open(my $fout, '-|', "$^X -Iblib/lib $filename");
62
63
	my $keep = $_;
64
	undef $/;
65
	my $output = <$fout>;
66
	$/ = $keep;
67
68
	close $tmp;
69
70
	ok($output =~ /^Content-Length:\s+(\d+)+/m);
71 1
	my $length = $1;
72
73
	my ($headers, $body) = split /\r?\n\r?\n/, $output, 2;
74
	ok(defined($headers));
75
	ok(defined($body));
76
	is(length($body), $length, 'Check length of body');
77
78
	ok($output =~ /document\.write\("a"\+"b"\);/m);
79
	ok($output =~ /document\.write\("foo"\+"bar"\);/m);
80
	ok($output !~ /document\.write\("1"\+"2"\);/m);
81
	ok($output !~ /document\.write\("fred"\+"wilma"\);/m);
82
}