[PATCH] Tests: additional rewrite and set tests

Maxim Dounin mdounin at mdounin.ru
Tue May 19 19:13:46 UTC 2026


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1779217965 -10800
#      Tue May 19 22:12:45 2026 +0300
# Node ID ed704b735aa4530b758d3af2741046b0a888d07e
# Parent  2d2b3edb6935fa91060b3e2e804eca7927b93e81
Tests: additional rewrite and set tests.

diff --git a/rewrite.t b/rewrite.t
--- a/rewrite.t
+++ b/rewrite.t
@@ -21,7 +21,7 @@ use Test::Nginx;
 select STDERR; $| = 1;
 select STDOUT; $| = 1;
 
-my $t = Test::Nginx->new()->has(qw/http rewrite proxy/)->plan(23)
+my $t = Test::Nginx->new()->has(qw/http rewrite proxy/)->plan(24)
 	->write_file_expand('nginx.conf', <<'EOF');
 
 %%TEST_GLOBALS%%
@@ -126,7 +126,13 @@ http {
             return 200 "uri:$uri args:$args";
         }
 
-        location /capturedup {
+        location /capture_dup {
+            rewrite ^(.*) $1?c=$1;
+            return 200 "uri:$uri args:$args";
+        }
+
+        location /capture_another {
+            rewrite ^(.*) $1?c=d;
             rewrite ^(.*) $1?c=$1;
             return 200 "uri:$uri args:$args";
         }
@@ -233,9 +239,22 @@ like(http_get('/capture/%25?a=b'),
 	qr!^uri:/capture/% args:c=d&a=b$!ms,
 	'escape with added args');
 
-like(http_get('/capturedup/%25?a=b'),
-	qr!^uri:/capturedup/% args:c=/capturedup/%25&a=b$!ms,
-	'escape with added args');
+like(http_get('/capture_dup/%25?a=b'),
+	qr!^uri:/capture_dup/% args:c=/capture_dup/%25&a=b$!ms,
+	'escape with added args and duplicate captures');
+
+TODO: {
+local $TODO = 'not yet'
+	unless $t->has_version('1.31.1');
+todo_skip 'might coredump', 1
+	unless $t->has_version('1.31.1')
+	or $ENV{TEST_NGINX_UNSAFE};
+
+like(http_get('/capture_another/%25?a=b'),
+	qr!^uri:/capture_another/% args:c=/capture_another/%25&c=d&a=b$!ms,
+	'escape with added args and another rewrite');
+
+}
 
 # break
 
diff --git a/rewrite_set.t b/rewrite_set.t
--- a/rewrite_set.t
+++ b/rewrite_set.t
@@ -22,7 +22,7 @@ use Test::Nginx;
 select STDERR; $| = 1;
 select STDOUT; $| = 1;
 
-my $t = Test::Nginx->new()->has(qw/http rewrite ssi/)->plan(4);
+my $t = Test::Nginx->new()->has(qw/http rewrite ssi/)->plan(10);
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
@@ -40,18 +40,57 @@ http {
         listen       127.0.0.1:8080;
         server_name  localhost;
 
-        ssi on;
+        location /string {
+            set $temp "set_string";
+            return 200 "X${temp}X";
+        }
+
+        location /variable/ {
+            set $temp "set_$uri";
+            return 200 "X${temp}X";
+        }
+
+        location ~ ^(/capture/.*) {
+            set $temp "set_$1";
+            return 200 "X${temp}X";
+        }
+
+        location /if/ {
+            if ($uri ~ "(.*)") {
+                set $temp "set_$1";
+            }
+            return 200 "X${temp}X";
+        }
+
+        location /rewrite/ {
+            rewrite (.*) $1;
+            if ($uri ~ "(.*)") {
+                set $temp "set_$1";
+            }
+            return 200 "X${temp}X";
+        }
+
+        location /args/ {
+            rewrite (.*) $1?args;
+            if ($uri ~ "(.*)") {
+                set $temp "set_$1";
+            }
+            return 200 "X${temp}X";
+        }
 
         location /t1 {
             set $http_foo "set_foo";
+            ssi on;
             return 200 'X<!--#echo var="http_foo" -->X';
         }
 
         location /t2 {
+            ssi on;
             return 200 'X<!--#echo var="http_bar" -->X';
         }
 
         location /t3 {
+            ssi on;
             return 200 'X<!--#echo var="http_baz" -->X';
         }
 
@@ -60,8 +99,8 @@ http {
             return 200 "X${http_connection}X\n";
         }
 
-        # set in other context
         location /other {
+            # set in other context
             set $http_bar "set_bar";
         }
     }
@@ -73,15 +112,48 @@ EOF
 
 ###############################################################################
 
-# prefixed variables
+# basic set operations
+
+like(http_get('/string'), qr/Xset_stringX/, 'set string');
+like(http_get('/variable/%20x'), qr!Xset_/variable/ xX!, 'set variable');
+like(http_get('/capture/%20x'), qr!Xset_/capture/%20xX!, 'set capture');
+like(http_get('/if/%20x'), qr!Xset_/if/%20xX!, 'set capture after if');
+
+TODO: {
+local $TODO = 'not yet'
+	unless $t->has_version('1.31.1');
+
+# set after a rewrite, used to loss quoting
+# due to e->quote being reset
+
+like(http_get('/rewrite/%20x'), qr!Xset_/rewrite/%20xX!,
+	'set capture after rewrite');
+
+}
+
+TODO: {
+local $TODO = 'not yet'
+	unless $t->has_version('1.31.1');
+todo_skip 'might coredump', 1
+	unless $t->has_version('1.31.1')
+	or $ENV{TEST_NGINX_UNSAFE};
+
+# set after a rewrite with arguments,
+# used to incorrectly allocate buffer due to e->is_args set
+# but not propagated to length calculations
+
+like(http_get('/args/%20x'), qr!Xset_/args/%20xX!,
+	'set capture after rewrite with arguments');
+
+}
+
+# non-indexed access of prefixed variables
 
 like(http_get_extra('/t1.html', 'Foo: http_foo'), qr/Xset_fooX/,
 	'set in this context');
 like(http_get_extra('/t2.html', 'Bar: http_bar'), qr/Xhttp_barX/,
 	'set in other context');
-
 like(http_get_extra('/t3.html', 'Baz: http_baz'), qr/Xhttp_bazX/, 'not set');
-
 like(http_get('/t4.html'), qr/XbarX/, 'set get in return');
 
 ###############################################################################



More information about the nginx-devel mailing list