changeset 399:c6ab68c41c8c

Tests: charset filter tests.
author Maxim Dounin <mdounin@mdounin.ru>
date Fri, 16 May 2014 19:25:47 +0400
parents 077ffeac825c
children 98d9b06b087b
files charset.t charset_gzip_static.t
diffstat 2 files changed, 271 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/charset.t
@@ -0,0 +1,102 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+# (C) Nginx, Inc.
+
+# Tests for charset filter.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+my $t = Test::Nginx->new()->has(qw/http charset proxy/)->plan(7)
+	->write_file_expand('nginx.conf', <<'EOF')->run();
+
+%%TEST_GLOBALS%%
+
+daemon off;
+
+events {
+}
+
+http {
+    %%TEST_GLOBALS_HTTP%%
+
+    types {
+        text/html html;
+        text/foo  foo;
+    }
+
+    charset_map B A {
+        58 59; # X -> Y
+    }
+
+    server {
+        listen       127.0.0.1:8080;
+        server_name  localhost;
+
+        location / {
+            charset utf-8;
+        }
+
+        location /t3.foo {
+            charset utf-8;
+            charset_types text/foo;
+        }
+
+        location /t4.any {
+            charset utf-8;
+            charset_types *;
+        }
+
+        location /t5.html {
+            charset $arg_c;
+        }
+
+        location /t.html {
+            charset A;
+            source_charset B;
+        }
+
+        location /proxy/ {
+            charset B;
+            override_charset on;
+            proxy_pass http://127.0.0.1:8080/;
+        }
+    }
+}
+
+EOF
+
+$t->write_file('t1.html', '');
+$t->write_file('t2.foo', '');
+$t->write_file('t3.foo', '');
+$t->write_file('t4.any', '');
+$t->write_file('t5.html', '');
+$t->write_file('t.html', 'X' x 99);
+
+###############################################################################
+
+like(http_get('/t1.html'), qr!text/html; charset=utf-8!, 'charset indicated');
+like(http_get('/t2.foo'), qr!text/foo\x0d!, 'wrong type');
+like(http_get('/t3.foo'), qr!text/foo; charset=utf-8!, 'charset_types');
+like(http_get('/t4.any'), qr!text/plain; charset=utf-8!, 'charset_types any');
+like(http_get('/t5.html?c=utf-8'), qr!text/html; charset=utf-8!, 'variables');
+
+like(http_get('/t.html'), qr!Y{99}!, 'recode');
+like(http_get('/proxy/t.html'), qr!X{99}!, 'override charset');
+
+###############################################################################
new file mode 100644
--- /dev/null
+++ b/charset_gzip_static.t
@@ -0,0 +1,169 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+# (C) Nginx, Inc.
+
+# Tests for charset filter.
+
+###############################################################################
+
+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()->has(qw/http charset gzip_static/)->plan(13)
+	->write_file_expand('nginx.conf', <<'EOF')->run();
+
+%%TEST_GLOBALS%%
+
+daemon off;
+
+events {
+}
+
+http {
+    %%TEST_GLOBALS_HTTP%%
+
+    types {
+        text/html html;
+    }
+
+    charset_map B A {
+        58 59; # X -> Y
+    }
+
+    server {
+        listen       127.0.0.1:8080;
+        server_name  localhost;
+
+        location /t1 {
+            charset utf-8;
+            gzip_static on;
+        }
+
+        location /t2 {
+            gzip_static on;
+            charset A;
+            source_charset B;
+        }
+
+        location /t {
+            gzip_static on;
+        }
+
+        location /p/ {
+            charset utf-8;
+            proxy_pass http://127.0.0.1:8080/;
+            proxy_http_version 1.1;
+        }
+
+        location /p.ab/ {
+            charset A;
+            source_charset B;
+            proxy_pass http://127.0.0.1:8080/;
+            proxy_http_version 1.1;
+        }
+
+        location /p.aa/ {
+            charset A;
+            source_charset A;
+            proxy_pass http://127.0.0.1:8080/;
+            proxy_http_version 1.1;
+        }
+    }
+}
+
+EOF
+
+$t->write_file('t1.html', '');
+$t->write_file('t1.html.gz', '');
+
+my $in = 'X' x 99;
+my $out;
+
+eval {
+	require IO::Compress::Gzip;   
+	IO::Compress::Gzip::gzip(\$in => \$out);
+};
+
+$t->write_file('t2.html', $in);
+$t->write_file('t2.html.gz', $out);
+
+$t->write_file('t.html', '');
+$t->write_file('t.html.gz', '');
+
+###############################################################################
+
+# charset filter currently ignores responses with Content-Encoding set
+# (except ones with r->ignore_content_encoding used by gzip_static)
+# as it can't convert such content; there are two problems though:
+#
+# - it make sense to indicate charset
+#   if conversion isn't needed
+#
+# - gzip_static may need conversion, too
+#
+# proper solution seems to be to always allow charset indication, but
+# don't try to do anything if recoding is needed
+
+like(http_get('/t1.html'), qr!text/html; charset=!, 'plain');
+like(http_gzip_request('/t1.html'), qr!text/html; charset=.*gzip!ms, 'gzip');
+
+like(http_get('/t2.html'), qr!text/html; charset=A.*Y{99}!ms, 'recode plain');
+
+TODO: {
+local $TODO = 'not yet';
+
+like(http_gzip_request('/t2.html'), qr!text/html\x0d.*gzip!ms, 'recode gzip');
+http_gzip_like(http_gzip_request('/t2.html'), qr!X{99}!, 'recode content');
+
+}
+
+like(http_get('/t.html'), qr!text/html\x0d!, 'nocharset plain');
+like(http_gzip_request('/t.html'), qr!text/html\x0d.*gzip!ms, 'nocharset gzip');
+
+like(http_get('/p/t.html'), qr!text/html; charset=!, 'proxy plain');
+
+TODO: {
+local $TODO = 'not yet';
+
+like(http_gzip_request('/p/t.html'), qr!text/html; charset=.*gzip!ms,
+	'proxy gzip');
+
+}
+
+
+like(http_get('/p.ab/t.html'), qr!text/html; charset=A!ms,
+	'proxy recode plain');
+
+TODO: {
+local $TODO = 'not yet';
+
+like(http_gzip_request('/p.ab/t.html'), qr!text/html\x0d; charset=.*gzip!ms,
+	'proxy recode gzip');
+
+}
+
+like(http_get('/p.aa/gz.html'), qr!text/html; charset=A!ms,
+	'proxy nullrecode plain');
+
+TODO: {
+local $TODO = 'not yet';
+
+like(http_gzip_request('/p.aa/gz.html'), qr!text/html\x0d; charset=.*gzip!ms,
+	'proxy nullrecode gzip');
+
+}
+
+###############################################################################