changeset 304:6bee817c9e97

Tests: sub filter tests.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 08 Jul 2013 20:50:09 +0400
parents ad51e58c2d7a
children f4aab0e66ed0
files sub_filter.t sub_filter_perl.t
diffstat 2 files changed, 231 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/sub_filter.t
@@ -0,0 +1,127 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Tests for sub filter.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+use Socket qw/ CRLF /;
+
+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 rewrite sub/)->plan(14)
+	->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;
+
+        sub_filter_types *;
+        sub_filter foo bar;
+
+        location / {
+        }
+
+        location /once {
+            return 200 $arg_b;
+        }
+
+        location /many {
+            sub_filter_once off;
+            return 200 $arg_b;
+        }
+
+        location /complex {
+            sub_filter abac _replaced;
+            return 200 $arg_b;
+        }
+
+        location /complex2 {
+            sub_filter ababX _replaced;
+            return 200 $arg_b;
+        }
+
+        location /complex3 {
+            sub_filter aab _replaced;
+            return 200 $arg_b;
+        }
+    }
+}
+
+EOF
+
+$t->write_file('foo.html', 'foo');
+$t->write_file('foofoo.html', 'foofoo');
+$t->run();
+
+###############################################################################
+
+like(http_get('/foo.html'), qr/bar/, 'sub_filter');
+like(http_get('/foofoo.html'), qr/barfoo/, 'once default');
+
+like(http_get('/once?b=foofoo'), qr/barfoo/, 'once');
+like(http_get('/many?b=foofoo'), qr/barbar/, 'many');
+
+TODO: {
+local $TODO = 'not yet';
+
+like(http_get('/many?b=fo'), qr/fo/, 'incomplete');
+like(http_get('/many?b=foofo'), qr/barfo/, 'incomplete long');
+
+}
+
+like(http_get('/complex?b=abac'), qr/_replaced/, 'complex');
+like(http_get('/complex?b=abaabac'), qr/aba_replaced/, 'complex 1st char');
+
+TODO: {
+local $TODO = 'not yet';
+
+like(http_get('/complex?b=ababac'), qr/replaced/, 'complex 2nd char');
+
+}
+
+like(http_get('/complex2?b=ababX'), qr/_replaced/, 'complex2');
+
+TODO: {
+local $TODO = 'not yet';
+
+like(http_get('/complex2?b=abababX'), qr/ab_replaced/, 'complex2 long');
+
+}
+
+like(http_get('/complex3?b=aab'), qr/_replaced/, 'complex3 aab in aab');
+
+TODO: {
+local $TODO = 'not yet';
+
+like(http_get('/complex3?b=aaab'), qr/a_replaced/, 'complex3 aab in aaab');
+
+}
+
+like(http_get('/complex3?b=aaaab'), qr/aa_replaced/, 'complex3 aab in aaaab');
+
+###############################################################################
new file mode 100644
--- /dev/null
+++ b/sub_filter_perl.t
@@ -0,0 +1,104 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Tests for sub filter, extended tests using embedded perl.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+use Socket qw/ CRLF /;
+
+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 sub perl/)->plan(9)
+	->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;
+
+        sub_filter_types *;
+        sub_filter foobarbaz replaced;
+
+        location / {
+            perl 'sub {
+                my $r = shift;
+                $r->send_http_header("text/html");
+                return OK if $r->header_only;
+                $r->print("foo");
+                $r->flush();
+                $r->print("bar");
+                $r->flush();
+                $r->print("baz");
+                return OK;
+            }';
+        }
+
+        location /multi {
+            sub_filter aab _replaced;
+            perl 'sub {
+                my $r = shift;
+                $r->send_http_header("text/html");
+                return OK if $r->header_only;
+                $r->print($r->variable("arg_a"));
+                $r->print($r->variable("arg_b"));
+                return OK;
+            }';
+        }
+    }
+}
+
+EOF
+
+$t->run();
+
+###############################################################################
+
+like(http_get('/flush'), qr/^replaced$/m, 'flush');
+
+like(http_get('/multi?a=a&b=ab'), qr/^_replaced$/m, 'aab in a + ab');
+like(http_get('/multi?a=a&b=aaab'), qr/^aa_replaced$/m, 'aab in a + aaab');
+
+TODO: {
+local $TODO = 'not yet';
+
+like(http_get('/multi?a=a&b=aab'), qr/a_replaced/, 'aab in a + aab');
+like(http_get('/multi?a=a&b=aaaab'), qr/aaa_replaced/, 'aab in a + aaaab');
+
+}
+
+TODO: {
+local $TODO = 'not yet';
+
+like(http_get('/multi?a=aa&b=ab'), qr/a_replaced/, 'aab in aa + ab');
+like(http_get('/multi?a=aa&b=aab'), qr/aa_replaced/, 'aab in aa + aab');
+like(http_get('/multi?a=aa&b=aaab'), qr/aaa_replaced/, 'aab in aa + aaab');
+
+}
+
+like(http_get('/multi?a=aa&b=aaaab'), qr/aaaa_replaced/, 'aab in aa + aaaab');
+
+###############################################################################