summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrei Zeliankou <zelenkov@nginx.com>2024-04-04 11:31:31 +0100
committerandrey-zelenkov <xim.andrew@gmail.com>2024-04-10 13:34:11 +0100
commit5f606742433f965f8308c4b4cd2c34424b8158a9 (patch)
tree54d682b2f27a674dc6abeab7a4f0b410274f3cf6
parent6e79da4730099b6ecb79f2896e8334cb0f24e508 (diff)
downloadunit-5f606742433f965f8308c4b4cd2c34424b8158a9.tar.gz
unit-5f606742433f965f8308c4b4cd2c34424b8158a9.tar.bz2
Tests: added $request_uri tests with proxy
This patch consist of 3 tests: 1. Ensure that $request_uri won't change while proxying the request. 2. Same as 1, but modifying the request using the "rewrite" directive. 3. Same as 2, but with rewrite containing a percent-encoded string.
-rw-r--r--test/test_variables.py101
1 files changed, 100 insertions, 1 deletions
diff --git a/test/test_variables.py b/test/test_variables.py
index 9aab8a62..735a87b3 100644
--- a/test/test_variables.py
+++ b/test/test_variables.py
@@ -93,7 +93,9 @@ def test_variables_method(search_in_file, wait_for_record):
assert wait_for_record(reg, 'access.log') is not None, 'method POST'
-def test_variables_request_uri(search_in_file, wait_for_record):
+def test_variables_request_uri(
+ findall, search_in_file, temp_dir, wait_for_record
+):
set_format('$request_uri')
def check_request_uri(req_uri):
@@ -108,6 +110,103 @@ def test_variables_request_uri(search_in_file, wait_for_record):
check_request_uri('/4%2A')
check_request_uri('/9?q#a')
+ # $request_uri + proxy
+
+ assert 'success' in client.conf(
+ {
+ "listeners": {
+ "*:8080": {"pass": "routes/a"},
+ "[::1]:8081": {"pass": "routes/b"},
+ },
+ "routes": {
+ "a": [
+ {
+ "action": {
+ "proxy": "http://[::1]:8081",
+ }
+ }
+ ],
+ "b": [
+ {
+ "action": {
+ "return": 200,
+ }
+ }
+ ],
+ },
+ "access_log": {
+ "path": f'{temp_dir}/access.log',
+ "format": "$remote_addr $uri $request_uri",
+ },
+ }
+ )
+
+ assert search_in_file(r'::1', 'access.log') is None
+
+ assert client.get(url='/blah%25blah?a=b')['status'] == 200
+
+ assert (
+ wait_for_record(fr'^::1 /blah%blah /blah%25blah\?a=b$', 'access.log')
+ is not None
+ ), 'req 8081 (proxy)'
+ assert (
+ search_in_file(
+ fr'^127\.0\.0\.1 /blah%blah /blah%25blah\?a=b$', 'access.log'
+ )
+ is not None
+ ), 'req 8080'
+
+ # rewrite set $request_uri before proxy
+
+ assert 'success' in client.conf(
+ {
+ "a": [
+ {
+ "action": {
+ "rewrite": "/foo",
+ "proxy": "http://[::1]:8081",
+ }
+ }
+ ],
+ "b": [
+ {
+ "action": {
+ "rewrite": "/bar",
+ "return": 200,
+ }
+ }
+ ],
+ },
+ 'routes',
+ )
+
+ assert len(findall(r'::1', 'access.log')) == 1
+
+ assert client.get(url='/blah%2Fblah?a=b')['status'] == 200
+
+ assert (
+ wait_for_record(fr'^::1 /bar /bar\?a=b$', 'access.log') is not None
+ ), 'req 8081 (proxy) rewrite'
+ assert (
+ search_in_file(fr'^127\.0\.0\.1 /foo /foo\?a=b$', 'access.log')
+ is not None
+ ), 'req 8080 rewrite'
+
+ # percent-encoded rewrite
+
+ assert len(findall(r'::1', 'access.log')) == 2
+
+ assert 'success' in client.conf('"/foo%2Ffoo"', 'routes/a/0/action/rewrite')
+ assert client.get(url='/blah%2Fblah?a=b')['status'] == 200
+
+ assert (
+ wait_for_record(
+ fr'^127\.0\.0\.1 /foo/foo /foo%2Ffoo\?a=b$', 'access.log'
+ )
+ is not None
+ ), 'req 8080 percent'
+ assert len(findall(fr'^::1 /bar /bar\?a=b$', 'access.log')) == 2
+
def test_variables_uri(search_in_file, wait_for_record):
set_format('$uri')