changeset 467:43e05ac6c23c

Tests: the read_file function added in Test::Nginx.
author Sergey Kandaurov <pluknet@nginx.com>
date Mon, 22 Sep 2014 13:30:04 +0400
parents a64b4057189c
children 684278f71783
files access_log.t dav_chunked.t debug_connection.t lib/Test/Nginx.pm proxy_cache_revalidate.t syslog.t
diffstat 6 files changed, 33 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/access_log.t
+++ b/access_log.t
@@ -143,7 +143,7 @@ SKIP: {
 	eval { require IO::Uncompress::Gunzip; };
 	skip("IO::Uncompress::Gunzip not installed", 1) if $@;
 
-	my $gzipped = read_file($t, 'compressed.log');
+	my $gzipped = $t->read_file('compressed.log');
 	IO::Uncompress::Gunzip::gunzip(\$gzipped => \$log);
 	like($log, qr!^/compressed:200!s, 'compressed log - flush time');
 }
@@ -155,7 +155,7 @@ SKIP: {
 
 # verify that by default, 'combined' format is used, 'off' disables logging
 
-$log = read_file($t, 'combined.log');
+$log = $t->read_file('combined.log');
 like($log,
 	qr!^\Q127.0.0.1 - - [\E .*
 		\Q] "GET /combined HTTP/1.0" 200 2 "-" "-"\E$!x,
@@ -163,7 +163,7 @@ like($log,
 
 # verify that log filtering works
 
-$log = read_file($t, 'filtered.log');
+$log = $t->read_file('filtered.log');
 is($log, "/filtered/good:200\n/filtered/work:200\n", 'log filtering');
 
 
@@ -178,7 +178,7 @@ my $exp_complex = <<'EOF';
 /filtered/complex/either4:200
 EOF
 
-$log = read_file($t, 'complex.log');
+$log = $t->read_file('complex.log');
 is($log, $exp_complex, 'if with complex value');
 
 
@@ -187,7 +187,7 @@ is($log, $exp_complex, 'if with complex 
 TODO: {
 local $TODO = 'not yet' unless $t->has_version('1.7.5');
 
-$log = read_file($t, '/noreuse.log');
+$log = $t->read_file('/noreuse.log');
 is($log, "/filtered/noreuse1/good:200\n/filtered/noreuse2/good:200\n",
 	'log filtering with buffering');
 
@@ -196,34 +196,21 @@ is($log, "/filtered/noreuse1/good:200\n/
 
 # multiple logs in a same location
 
-$log = read_file($t, 'multi1.log');
+$log = $t->read_file('multi1.log');
 is($log, "/multi:200\n", 'multiple logs 1');
 
 # same content in the second log
 
-$log = read_file($t, 'multi2.log');
+$log = $t->read_file('multi2.log');
 is($log, "/multi:200\n", 'multiple logs 2');
 
 
 # test log destinations with variables
 
-$log = read_file($t, '0');
+$log = $t->read_file('0');
 is($log, "/varlog:200\n", 'varlog literal zero name');
 
-$log = read_file($t, 'filename');
+$log = $t->read_file('filename');
 is($log, "/varlog:200\n", 'varlog good name');
 
 ###############################################################################
-
-sub read_file {
-	my ($t, $file) = @_;
-	my $path = $t->testdir() . '/' . $file;
-
-	open my $fh, '<', $path or return "$!";
-	local $/;
-	my $content = <$fh>;
-	close $fh;
-	return $content;
-}
-
-###############################################################################
--- a/dav_chunked.t
+++ b/dav_chunked.t
@@ -70,7 +70,7 @@ 0
 EOF
 
 like($r, qr/201 Created.*(Content-Length|\x0d\0a0\x0d\x0a)/ms, 'put chunked');
-is(read_file($t->testdir() . '/file'), '1234567890', 'put content');
+is($t->read_file('file'), '1234567890', 'put content');
 
 $r = http(<<EOF);
 PUT /file HTTP/1.1
@@ -83,7 +83,7 @@ 0
 EOF
 
 like($r, qr/204 No Content/, 'put chunked empty');
-is(read_file($t->testdir() . '/file'), '', 'put empty content');
+is($t->read_file('file'), '', 'put empty content');
 
 my $body = ('a' . CRLF . '1234567890' . CRLF) x 1024 . '0' . CRLF . CRLF;
 
@@ -97,18 +97,6 @@ Transfer-Encoding: chunked
 EOF
 
 like($r, qr/204 No Content/, 'put chunked big');
-is(read_file($t->testdir() . '/file'), '1234567890' x 1024, 'put big content');
+is($t->read_file('file'), '1234567890' x 1024, 'put big content');
 
 ###############################################################################
-
-sub read_file {
-	my ($file) = @_;
-	open FILE, $file
-		or return "$!";
-	local $/;
-	my $content = <FILE>;
-	close FILE;
-	return $content;
-}
-
-###############################################################################
--- a/debug_connection.t
+++ b/debug_connection.t
@@ -56,27 +56,14 @@ EOF
 
 ###############################################################################
 
-my $d = $t->testdir();
-
 http_get('/');
-is(read_file("$d/debug1.log"), '', 'no debug_connection file 1');
-is(read_file("$d/debug2.log"), '', 'no debug_connection file 1');
+is($t->read_file('debug1.log'), '', 'no debug_connection file 1');
+is($t->read_file('debug2.log'), '', 'no debug_connection file 1');
 
 http_get('/debug');
-like(read_file("$d/debug1.log"), qr/\[debug\]/, 'debug_connection file 1');
-like(read_file("$d/debug2.log"), qr/\[debug\]/, 'debug_connection file 2');
-is(read_file("$d/debug1.log"), read_file("$d/debug2.log"),
+like($t->read_file('debug1.log'), qr/\[debug\]/, 'debug_connection file 1');
+like($t->read_file('debug2.log'), qr/\[debug\]/, 'debug_connection file 2');
+is($t->read_file('debug1.log'), $t->read_file('debug2.log'),
 	'debug_connection file1 file2 match');
 
 ###############################################################################
-
-sub read_file {
-	my ($file) = shift;
-	open my $fh, '<', $file or return "$!";
-	local $/;
-	my $content = <$fh>;
-	close $fh;
-	return $content;
-}
-
-###############################################################################
--- a/lib/Test/Nginx.pm
+++ b/lib/Test/Nginx.pm
@@ -313,6 +313,18 @@ sub stop_daemons() {
 	return $self;
 }
 
+sub read_file($) {
+	my ($self, $name) = @_;
+	local $/;
+
+	open F, '<', $self->{_testdir} . '/' . $name
+		or die "Can't open $name: $!";
+	my $content = <F>;
+	close F;
+
+	return $content;
+}
+
 sub write_file($$) {
 	my ($self, $name, $content) = @_;
 
--- a/proxy_cache_revalidate.t
+++ b/proxy_cache_revalidate.t
@@ -102,7 +102,7 @@ like(http_get('/t'), qr/X-Cache-Status: 
 like(http_get('/t'), qr/X-Cache-Status: HIT.*SEE/ms, 'cached again');
 
 select undef, undef, undef, 0.1;
-like(read_file($t->testdir() . '/access.log'), qr/ 304 /, 'not modified');
+like($t->read_file('access.log'), qr/ 304 /, 'not modified');
 
 # 2nd document is recreated with a new content
 
@@ -130,18 +130,3 @@ like(http_get('/etag/t2'), qr/X-Cache-St
 	'etag2 new response cached');
 
 ###############################################################################
-
-sub read_file {
-	my ($file) = @_;
-	my $log;
-
-        local $/;
-
-        open LOG, $file or die "Can't open $file: $!\n";
-        $log = <LOG>;
-        close LOG;
-
-	return $log;
-}
-
-###############################################################################
--- a/syslog.t
+++ b/syslog.t
@@ -208,8 +208,8 @@ http_get('/if/work?logme=yes');
 
 get_syslog('/a');
 
-like(read_file($t, 's_if.log'), qr/good:404.*work:404/s, 'syslog if success');
-unlike(read_file($t, 's_if.log'), qr/(if:|empty:|zero:)404/, 'syslog if fail');
+like($t->read_file('s_if.log'), qr/good:404.*work:404/s, 'syslog if success');
+unlike($t->read_file('s_if.log'), qr/(if:|empty:|zero:)404/, 'syslog if fail');
 
 ###############################################################################
 
@@ -222,7 +222,7 @@ sub levels {
 	my ($t, $file) = @_;
 	my %levels_hash;
 
-	map { $levels_hash{$_}++; } (read_file($t, $file) =~ /(\[\w+\])/g);
+	map { $levels_hash{$_}++; } ($t->read_file($file) =~ /(\[\w+\])/g);
 
 	return \%levels_hash;
 }
@@ -266,17 +266,6 @@ sub get_syslog {
 	return $data;
 }
 
-sub read_file {
-	my ($t, $file) = @_;
-	my $path = $t->testdir() . '/' . $file;
-
-	open my $fh, '<', $path or return "$!";
-	local $/;
-	my $content = <$fh>;
-	close $fh;
-	return $content;
-}
-
 sub parse_syslog_message {
 	my ($desc, $line) = @_;