Tests: add basic gzip tests.
This also includes clearing Accept-Ranges header got from upstream (broken
since 0.7.44).
diff --git a/gzip.t b/gzip.t
new file mode 100644
index 0000000..57089f0
--- /dev/null
+++ b/gzip.t
@@ -0,0 +1,89 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Tests for nginx gzip 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;
+
+my $t = Test::Nginx->new()->plan(6);
+
+$t->write_file_expand('nginx.conf', <<'EOF');
+
+master_process off;
+daemon off;
+
+events {
+}
+
+http {
+ access_log off;
+ root %%TESTDIR%%;
+
+ client_body_temp_path %%TESTDIR%%/client_body_temp;
+ fastcgi_temp_path %%TESTDIR%%/fastcgi_temp;
+ proxy_temp_path %%TESTDIR%%/proxy_temp;
+
+ server {
+ listen 127.0.0.1:8080;
+ server_name localhost;
+ location / {
+ gzip on;
+ }
+ location /proxy/ {
+ gzip on;
+ proxy_pass http://127.0.0.1:8080/local/;
+ }
+ location /local/ {
+ gzip off;
+ alias %%TESTDIR%%/;
+ }
+ }
+}
+
+EOF
+
+$t->write_file('index.html', 'X' x 64);
+
+$t->run();
+
+###############################################################################
+
+my $r;
+
+$r = http_gzip_request('/');
+like($r, qr/^Content-Encoding: gzip/m, 'gzip');
+http_gzip_like($r, qr/^X{64}\Z/, 'gzip content correct');
+
+$r = http_gzip_request('/proxy/');
+like($r, qr/^Content-Encoding: gzip/m, 'gzip proxied');
+http_gzip_like($r, qr/^X{64}\Z/, 'gzip proxied content');
+
+# Accept-Ranges headers should be cleared
+
+unlike(http_gzip_request('/'), qr/Accept-Ranges/im, 'cleared accept-ranges');
+
+TODO: {
+local $TODO = 'broken since 0.7.44';
+
+unlike(http_gzip_request('/proxy/'), qr/Accept-Ranges/im,
+ 'cleared headers from proxy');
+
+}
+
+###############################################################################
diff --git a/lib/Test/Nginx.pm b/lib/Test/Nginx.pm
index 5986468..56cf900 100644
--- a/lib/Test/Nginx.pm
+++ b/lib/Test/Nginx.pm
@@ -12,6 +12,10 @@
use base qw/ Exporter /;
our @EXPORT = qw/ log_in log_out http http_get http_head /;
+our @EXPORT_OK = qw/ http_gzip_request http_gzip_like /;
+our %EXPORT_TAGS = (
+ gzip => [ qw/ http_gzip_request http_gzip_like / ]
+);
###############################################################################
@@ -275,6 +279,61 @@
###############################################################################
+sub http_gzip_request {
+ my ($url) = @_;
+ my $r = http(<<EOF);
+GET $url HTTP/1.1
+Host: localhost
+Connection: close
+Accept-Encoding: gzip
+
+EOF
+}
+
+sub http_content {
+ my ($text) = @_;
+
+ return undef if !defined $text;
+
+ if ($text !~ /(.*?)\x0d\x0a?\x0d\x0a?(.*)/ms) {
+ return undef;
+ }
+
+ my ($headers, $body) = ($1, $2);
+
+ if ($headers !~ /Transfer-Encoding: chunked/i) {
+ return $body;
+ }
+
+ my $content = '';
+ while ($body =~ /\G\x0d?\x0a?([0-9a-f]+)\x0d\x0a?/gcmsi) {
+ my $len = hex($1);
+ $content .= substr($body, pos($body), $len);
+ pos($body) += $len;
+ }
+
+ return $content;
+}
+
+sub http_gzip_like {
+ my ($text, $re, $name) = @_;
+
+ SKIP: {
+ eval { require IO::Uncompress::Gunzip; };
+ Test::More->builder->skip(
+ "IO::Uncompress::Gunzip not installed", 1) if $@;
+
+ my $in = http_content($text);
+ my $out;
+
+ IO::Uncompress::Gunzip::gunzip(\$in => \$out);
+
+ Test::More->builder->like($out, $re, $name);
+ }
+}
+
+###############################################################################
+
1;
###############################################################################
diff --git a/ssi-include-big.t b/ssi-include-big.t
index 7181c50..0467aed 100644
--- a/ssi-include-big.t
+++ b/ssi-include-big.t
@@ -14,7 +14,7 @@
BEGIN { use FindBin; chdir($FindBin::Bin); }
use lib 'lib';
-use Test::Nginx;
+use Test::Nginx qw/ :DEFAULT :gzip /;
###############################################################################
@@ -90,59 +90,4 @@
ok(defined $t4, 'big ssi main file');
http_gzip_like($t4, qr/^X{1025}\Z/, 'big ssi main file content');
-
-###############################################################################
-
-sub http_gzip_request {
- my ($url) = @_;
- my $r = http(<<EOF);
-GET $url HTTP/1.1
-Host: localhost
-Connection: close
-Accept-Encoding: gzip
-
-EOF
-}
-
-sub http_content {
- my ($text) = @_;
-
- return undef if !defined $text;
-
- if ($text !~ /(.*?)\x0d\x0a?\x0d\x0a?(.*)/ms) {
- return undef;
- }
-
- my ($headers, $body) = ($1, $2);
-
- if ($headers !~ /Transfer-Encoding: chunked/i) {
- return $body;
- }
-
- my $content = '';
- while ($body =~ /\G\x0d?\x0a?([0-9a-f]+)\x0d\x0a?/gcmsi) {
- my $len = hex($1);
- $content .= substr($body, pos($body), $len);
- pos($body) += $len;
- }
-
- return $content;
-}
-
-sub http_gzip_like {
- my ($text, $re, $name) = @_;
-
- SKIP: {
- eval { require IO::Uncompress::Gunzip; };
- skip "IO::Uncompress::Gunzip not installed", 1 if $@;
-
- my $in = http_content($text);
- my $out;
-
- IO::Uncompress::Gunzip::gunzip(\$in => \$out);
-
- like($out, $re, $name);
- }
-}
-
###############################################################################