[PATCH] tests: Conditional application of limit_conn directive

turchanov at farpost.com turchanov at farpost.com
Tue Sep 22 06:53:46 UTC 2020


# HG changeset patch
# User Sergei Turchanov <turchanov at farpost.com>
# Date 1600754712 -36000
#      Tue Sep 22 16:05:12 2020 +1000
# Node ID ed28bf1ce143fda23a16128d00099079ea18a66a
# Parent  e682d5ad3861af0a2117892a1aefc17b1303a1fa
Tests: test conditional application of limit_conn directive

diff -r e682d5ad3861 -r ed28bf1ce143 limit_conn_if.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/limit_conn_if.t	Tue Sep 22 16:05:12 2020 +1000
@@ -0,0 +1,114 @@
+#!/usr/bin/perl
+
+# (C) Sergei Turchanov
+
+# limit_req based tests for conditional application of limit_conn 
directive.
+
+###############################################################################
+
+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 proxy limit_conn limit_req/);
+
+$t->write_file_expand('nginx.conf', <<'EOF')->plan(11);
+
+%%TEST_GLOBALS%%
+
+daemon off;
+
+events {
+}
+
+http {
+    %%TEST_GLOBALS_HTTP%%
+
+    limit_req_zone   $binary_remote_addr  zone=req:1m rate=30r/m;
+
+    limit_conn_zone  $binary_remote_addr  zone=zone:1m;
+    limit_conn_zone  $binary_remote_addr  zone=zone2:1m;
+    limit_conn_zone  $binary_remote_addr  zone=custom:1m;
+
+    server {
+        listen       127.0.0.1:8081;
+        server_name  localhost;
+
+        location /w {
+            limit_req  zone=req burst=10;
+        }
+    }
+
+    map $arg_unlim $apply_limit {
+        1       "";
+        2       0;
+        3       "foo-bar";
+        default 1;
+    }
+
+    server {
+        listen       127.0.0.1:8080;
+        server_name  localhost;
+
+        location / {
+            proxy_pass http://127.0.0.1:8081;
+            limit_conn zone 1;
+        }
+
+        location /1 {
+            limit_conn zone 1;
+        }
+
+        location /cond {
+            limit_conn zone 1 if=$apply_limit;
+        }
+
+        location /zone {
+            limit_conn zone2 1;
+        }
+    }
+}
+
+EOF
+
+$t->run();
+
+###############################################################################
+
+# charge limit_req
+
+http_get('/w');
+
+# same and other zones in different locations
+
+my $s = http_get('/w', start => 1);
+like(http_get('/'), qr/^HTTP\/1.. 503 /, 'rejected');
+like(http_get('/1'), qr/^HTTP\/1.. 503 /, 'rejected different 
location');
+like(http_get('/cond'), qr/^HTTP\/1.. 503 /,
+    'rejected due to condition (if=1)');
+like(http_get('/cond?unlim=3'), qr/^HTTP\/1.. 503 /,
+    'rejected due to condition (if=somerandomstring)');
+unlike(http_get('/cond?unlim=1'), qr/^HTTP\/1.. 503 /,
+    'passed due to condition (if="")');
+like(http_get('/cond'), qr/^HTTP\/1.. 503 /, 'rejected again (if=1)');
+unlike(http_get('/cond?unlim=2'), qr/^HTTP\/1.. 503 /,
+    'passed due to condition (if=0)');
+like(http_get('/cond'), qr/^HTTP\/1.. 503 /, 'still rejected (if=1)');
+unlike(http_get('/zone'), qr/^HTTP\/1.. 503 /, 'passed different 
zone');
+
+close $s;
+unlike(http_get('/1'), qr/^HTTP\/1.. 503 /, 'passed');
+unlike(http_get('/cond'), qr/^HTTP\/1.. 503 /, 'passed (if=1)');
+
+###############################################################################


More information about the nginx-devel mailing list