Mercurial > hg > nginx-tests
changeset 93:5276d85d5040
Tests: add basic gzip tests.
This also includes clearing Accept-Ranges header got from upstream (broken
since 0.7.44).
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Tue, 26 May 2009 20:18:05 +0400 |
parents | 390588796411 |
children | cbc17561ef4d |
files | gzip.t lib/Test/Nginx.pm ssi-include-big.t |
diffstat | 3 files changed, 149 insertions(+), 56 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /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'); + +} + +###############################################################################
--- a/lib/Test/Nginx.pm +++ b/lib/Test/Nginx.pm @@ -12,6 +12,10 @@ use strict; 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($;%) { ############################################################################### +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; ###############################################################################
--- a/ssi-include-big.t +++ b/ssi-include-big.t @@ -14,7 +14,7 @@ use Test::More; BEGIN { use FindBin; chdir($FindBin::Bin); } use lib 'lib'; -use Test::Nginx; +use Test::Nginx qw/ :DEFAULT :gzip /; ############################################################################### @@ -90,59 +90,4 @@ my $t4 = http_gzip_request('/test4.html' 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); - } -} - -###############################################################################