changeset 231:bc1861122d0c

Tests: gunzip filter tests import.
author Maxim Dounin <mdounin@mdounin.ru>
date Fri, 07 Sep 2012 20:30:32 +0400
parents 9d7805b05f0f
children 4c43b3a0117c
files gunzip.t gunzip_memcached.t gunzip_perl.t gunzip_ssi.t gunzip_static.t
diffstat 5 files changed, 477 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/gunzip.t
@@ -0,0 +1,113 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Tests for gunzip filter module.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx qw/ :DEFAULT :gzip /;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+eval { require IO::Compress::Gzip; };
+Test::More::plan(skip_all => "IO::Compress::Gzip not found") if $@;
+
+my $t = Test::Nginx->new()->has(qw/http gunzip proxy gzip_static/)->plan(13);
+
+$t->write_file_expand('nginx.conf', <<'EOF');
+
+%%TEST_GLOBALS%%
+
+daemon         off;
+
+events {
+}
+
+http {
+    %%TEST_GLOBALS_HTTP%%
+
+    server {
+        listen       127.0.0.1:8080;
+        server_name  localhost;
+        location / {
+            gunzip on;
+            gzip_vary on;
+            proxy_pass http://127.0.0.1:8081/;
+            proxy_set_header Accept-Encoding gzip;
+        }
+        location /error {
+            error_page 500 /t1;
+            return 500;
+        }
+    }
+
+    server {
+        listen       127.0.0.1:8081;
+        server_name  localhost;
+
+        location / {
+            default_type text/plain;
+            gzip_static on;
+            gzip_http_version 1.0;
+            gzip_types text/plain;
+        }
+    }
+}
+
+EOF
+
+my $in = join('', map { sprintf "X%03dXXXXXX", $_ } (0 .. 99));
+my $out;
+
+IO::Compress::Gzip::gzip(\$in => \$out);
+
+$t->write_file('t1.gz', $out);
+$t->write_file('t2.gz', $out . $out);
+$t->write_file('t3', 'not compressed');
+
+my $emptyin = '';
+my $emptyout;
+IO::Compress::Gzip::gzip(\$emptyin => \$emptyout);
+
+$t->write_file('empty.gz', $emptyout);
+
+$t->run();
+
+###############################################################################
+
+pass('runs');
+
+my $r = http_get('/t1');
+unlike($r, qr/Content-Encoding/, 'no content encoding');
+like($r, qr/^(X\d\d\dXXXXXX){100}$/m, 'correct gunzipped response');
+
+$r = http_gzip_request('/t1');
+like($r, qr/Content-Encoding: gzip/, 'gzip still works - encoding');
+like($r, qr/\Q$out\E/, 'gzip still works - content');
+
+like(http_get('/t2'), qr/^(X\d\d\dXXXXXX){200}$/m, 'multiple gzip members');
+
+like(http_get('/error'), qr/^(X\d\d\dXXXXXX){100}$/m, 'errors gunzipped');
+
+unlike(http_head('/t1'), qr/Content-Encoding/, 'head - no content encoding');
+
+like(http_get('/t1'), qr/Vary/, 'get vary');
+like(http_head('/t1'), qr/Vary/, 'head vary');
+unlike(http_get('/t3'), qr/Vary/, 'no vary on non-gzipped get');
+unlike(http_head('/t3'), qr/Vary/, 'no vary on non-gzipped head');
+
+like(http_get('/empty'), qr/ 200 /, 'gunzip empty');
+
+###############################################################################
new file mode 100644
--- /dev/null
+++ b/gunzip_memcached.t
@@ -0,0 +1,99 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Tests for gunzip filter module with memcached.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx qw/ :DEFAULT :gzip /;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+eval { require Cache::Memcached; };
+plan(skip_all => 'Cache::Memcached not installed') if $@;
+
+eval { require IO::Compress::Gzip; };
+plan(skip_all => "IO::Compress::Gzip not found") if $@;
+
+my $t = Test::Nginx->new()->has(qw/http gunzip memcached/)
+	->has_daemon('memcached')
+	->write_file_expand('nginx.conf', <<'EOF');
+
+%%TEST_GLOBALS%%
+
+daemon         off;
+
+events {
+}
+
+http {
+    %%TEST_GLOBALS_HTTP%%
+
+    server {
+        listen       127.0.0.1:8080;
+        server_name  localhost;
+
+        gunzip on;
+
+        location / {
+            set $memcached_key $uri;
+            memcached_pass 127.0.0.1:8081;
+            memcached_gzip_flag 2;
+        }
+    }
+}
+
+EOF
+
+my $memhelp = `memcached -h`;
+my @memopts = ();
+
+if ($memhelp =~ /repcached/) {
+	# repcached patch adds additional listen socket
+	push @memopts, '-X', '8082';
+}
+if ($memhelp =~ /-U/) {
+	# UDP port is on by default in memcached 1.2.7+
+	push @memopts, '-U', '0';
+}
+
+$t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081', @memopts);
+
+eval {
+	open OLDERR, ">&", \*STDERR; close STDERR;
+	$t->run();
+	open STDERR, ">&", \*OLDERR;
+};
+plan(skip_all => 'no memcached_gzip_flag') if $@;
+
+$t->plan(2);
+
+$t->waitforsocket('127.0.0.1:8081')
+	or die "Can't start memcached";
+
+# Put compressed value into memcached.  This requires compress_threshold to be
+# set and compressed value to be at least 20% less than original one.
+
+my $memd = Cache::Memcached->new(servers => [ '127.0.0.1:8081' ],
+	compress_threshold => 1);
+$memd->set('/', 'TEST' x 10)
+        or die "can't put value into memcached: $!";
+
+###############################################################################
+
+http_gzip_like(http_gzip_request('/'), qr/TEST/, 'memcached response gzipped');
+like(http_get('/'), qr/TEST/, 'memcached response gunzipped');
+
+###############################################################################
new file mode 100644
--- /dev/null
+++ b/gunzip_perl.t
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Tests for gunzip filter module with perl module.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx qw/ :DEFAULT :gzip /;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+eval { require IO::Compress::Gzip; };
+Test::More::plan(skip_all => "IO::Compress::Gzip not found") if $@;
+
+my $t = Test::Nginx->new()->has(qw/http gunzip perl/)->plan(2)
+	->write_file_expand('nginx.conf', <<'EOF');
+
+%%TEST_GLOBALS%%
+
+daemon         off;
+
+events {
+}
+
+http {
+    %%TEST_GLOBALS_HTTP%%
+
+    server {
+        listen       127.0.0.1:8080;
+        server_name  localhost;
+
+        gunzip on;
+
+        location / {
+            perl 'sub {
+                my $r = shift;
+                $r->header_out("Content-Encoding", "gzip");
+                $r->send_http_header("text/plain");
+                return OK if $r->header_only;
+                use IO::Compress::Gzip;
+                my $in = "TEST";
+                my $out;
+                IO::Compress::Gzip::gzip(\\$in => \\$out);
+                $r->print($out);
+                return OK;
+            }';
+        }
+    }
+}
+
+EOF
+
+$t->run();
+
+###############################################################################
+
+http_gzip_like(http_gzip_request('/'), qr/TEST/, 'perl response gzipped');
+like(http_get('/'), qr/TEST/, 'perl response gunzipped');
+
+###############################################################################
new file mode 100644
--- /dev/null
+++ b/gunzip_ssi.t
@@ -0,0 +1,93 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Tests for gunzip filter module with subrequests.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx qw/ :DEFAULT :gzip /;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+eval { require IO::Compress::Gzip; };
+Test::More::plan(skip_all => "IO::Compress::Gzip not found") if $@;
+
+my $t = Test::Nginx->new()->has(qw/http gunzip ssi proxy gzip_static/)
+	->plan(4);
+
+$t->write_file_expand('nginx.conf', <<'EOF');
+
+%%TEST_GLOBALS%%
+
+daemon         off;
+
+events {
+}
+
+http {
+    %%TEST_GLOBALS_HTTP%%
+
+    server {
+        listen       127.0.0.1:8080;
+        server_name  localhost;
+
+        location / {
+            gunzip on;
+            gzip_vary on;
+            proxy_pass http://127.0.0.1:8081/;
+            proxy_set_header Accept-Encoding gzip;
+        }
+
+        location /t.html {
+            ssi on;
+        }
+    }
+
+    server {
+        listen       127.0.0.1:8081;
+        server_name  localhost;
+
+        location / {
+            default_type text/plain;
+            gzip_static on;
+            gzip_http_version 1.0;
+            gzip_types text/plain;
+        }
+    }
+}
+
+EOF
+
+my $in = join('', map { sprintf "X%03dXXXXXX", $_ } (0 .. 99));
+my $out;
+
+IO::Compress::Gzip::gzip(\$in => \$out);
+
+$t->write_file('t1.gz', $out);
+$t->write_file('t.html', 'xxx <!--#include virtual="/t1" --> xxx');
+
+$t->run();
+
+###############################################################################
+
+my $r = http_get('/t.html');
+unlike($r, qr/Content-Encoding/, 'no content encoding');
+like($r, qr/^xxx (X\d\d\dXXXXXX){100} xxx$/m, 'correct gunzipped response');
+
+$r = http_gzip_request('/t.html');
+unlike($r, qr/Content-Encoding/, 'gzip - no content encoding');
+like($r, qr/(X\d\d\dXXXXXX){100}/m, 'gzip - correct gunzipped response');
+
+###############################################################################
new file mode 100644
--- /dev/null
+++ b/gunzip_static.t
@@ -0,0 +1,100 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Tests for gunzip filter module with gzip_static always.  It is basically
+# the copy of gunzip.t with minor modifications.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx qw/ :DEFAULT :gzip /;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+eval { require IO::Compress::Gzip; };
+Test::More::plan(skip_all => "IO::Compress::Gzip not found") if $@;
+
+my $t = Test::Nginx->new()->has(qw/http gunzip proxy gzip_static/);
+
+$t->write_file_expand('nginx.conf', <<'EOF');
+
+%%TEST_GLOBALS%%
+
+daemon         off;
+
+events {
+}
+
+http {
+    %%TEST_GLOBALS_HTTP%%
+
+    server {
+        listen       127.0.0.1:8080;
+        server_name  localhost;
+        location / {
+            gunzip on;
+            gzip_vary on;
+            gzip_static always;
+        }
+        location /error {
+            error_page 500 /t1;
+            return 500;
+        }
+    }
+}
+
+EOF
+
+my $in = join('', map { sprintf "X%03dXXXXXX", $_ } (0 .. 99));
+my $out;
+
+IO::Compress::Gzip::gzip(\$in => \$out);
+
+$t->write_file('t1.gz', $out);
+$t->write_file('t2.gz', $out . $out);
+$t->write_file('t3', 'not compressed');
+
+eval {
+	open OLDERR, ">&", \*STDERR; close STDERR;
+	$t->run();
+	open STDERR, ">&", \*OLDERR;
+};
+plan(skip_all => 'no gzip_static always') if $@;
+
+$t->plan(12);
+
+###############################################################################
+
+pass('runs');
+
+my $r = http_get('/t1');
+unlike($r, qr/Content-Encoding/, 'no content encoding');
+like($r, qr/^(X\d\d\dXXXXXX){100}$/m, 'correct gunzipped response');
+
+$r = http_gzip_request('/t1');
+like($r, qr/Content-Encoding: gzip/, 'gzip still works - encoding');
+like($r, qr/\Q$out\E/, 'gzip still works - content');
+
+like(http_get('/t2'), qr/^(X\d\d\dXXXXXX){200}$/m, 'multiple gzip members');
+
+like(http_get('/error'), qr/^(X\d\d\dXXXXXX){100}$/m, 'errors gunzipped');
+
+unlike(http_head('/t1'), qr/Content-Encoding/, 'head - no content encoding');
+
+like(http_get('/t1'), qr/Vary/, 'get vary');
+like(http_head('/t1'), qr/Vary/, 'head vary');
+unlike(http_get('/t3'), qr/Vary/, 'no vary on non-gzipped get');
+unlike(http_head('/t3'), qr/Vary/, 'no vary on non-gzipped head');
+
+###############################################################################