changeset 1:0dd7d109e56b

Gunzip: add more tests and improve docs.
author Maxim Dounin <mdounin@mdounin.ru>
date Sun, 20 Dec 2009 03:55:31 +0300
parents a75d4ad9c5d2
children 5fc44c8ba213
files README ngx_http_gunzip_filter_module.c t/gunzip-ssi.t t/gunzip.t
diffstat 4 files changed, 113 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/README
+++ b/README
@@ -1,10 +1,17 @@
 Gunzip module for nginx.
 
-This module allows ungzipping responses returned with Content-Encoding: gzip
+This module allows gunzipping responses returned with Content-Encoding: gzip
 for clients that doesn't support it.  It may be usefull if you prefer to store
 data compressed (to save space or disk/network IO) but do not want to penalize
 clients without gzip support.
 
+Note well: only responses with Content-Encoding set to gzip before this module
+are handled (e.g. using "add_header Content-Encoding gzip;" isn't enough as it
+happens after).  As of now only proxy and fastcgi are able to do so.
+
+This module was designed to work with nginx 0.8.* (though it should work with
+0.7.* too).
+
 Configuration directives:
 
     gunzip (on|off)
@@ -25,6 +32,7 @@ Usage:
 
     location /storage/ {
         gunzip on;
+        ...
     }
 
 To compile nginx with gunzip module, use "--add-module <path>" option to nginx
--- a/ngx_http_gunzip_filter_module.c
+++ b/ngx_http_gunzip_filter_module.c
@@ -126,6 +126,8 @@ ngx_http_gunzip_header_filter(ngx_http_r
     conf = ngx_http_get_module_loc_conf(r, ngx_http_gunzip_filter_module);
 
     /* TODO support multiple content-codings */
+    /* TODO always gunzip - due to configuration or module request */
+    /* TODO ignore content encoding? */
 
     if (!conf->enable
         || r->headers_out.content_encoding == NULL
new file mode 100644
--- /dev/null
+++ b/t/gunzip-ssi.t
@@ -0,0 +1,92 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Tests for gunzip filter module with subrequests.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+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('--with-http_gzip_static_module')->plan(4);
+
+$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 / {
+            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');
+
+###############################################################################
--- a/t/gunzip.t
+++ b/t/gunzip.t
@@ -10,7 +10,7 @@ use warnings;
 use strict;
 
 use Test::More;
-use Test::Nginx;
+use Test::Nginx qw/ :DEFAULT :gzip /;
 
 ###############################################################################
 
@@ -20,7 +20,7 @@ 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('--with-http_gzip_static_module')->plan(10);
+my $t = Test::Nginx->new()->has('--with-http_gzip_static_module')->plan(12);
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
@@ -83,13 +83,17 @@ IO::Compress::Gzip::gzip(\$in => \$out);
 
 pass('runs');
 
-my $t1 = http_get('/t1');
-unlike($t1, qr/Content-Encoding/, 'no content encoding');
-like($t1, qr/^(X\d\d\dXXXXXX){100}$/m, 'correct ungzipped response');
+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 ungzipped');
+like(http_get('/error'), qr/^(X\d\d\dXXXXXX){100}$/m, 'errors gunzipped');
 
 unlike(http_head('/t1'), qr/Content-Encoding/, 'head - no content encoding');