summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorOutOfFocus4 <jeff.iadarola@gmail.com>2021-11-14 10:47:07 -0500
committerAlejandro Colomar <alx@nginx.com>2022-12-14 11:30:30 +0100
commit6dae517ebd20baa2066541e703d6aa594326dd69 (patch)
tree6fe6a7cc8a767bc536545b640542590bbb222d30 /test
parent7a81d9d61d0c4e3cc7e6b74b6be367cbf26ea2ab (diff)
downloadunit-6dae517ebd20baa2066541e703d6aa594326dd69.tar.gz
unit-6dae517ebd20baa2066541e703d6aa594326dd69.tar.bz2
Python: Added "prefix" to configuration.
This patch gives users the option to set a `"prefix"` attribute for Python applications, either at the top level or for specific `"target"`s. If the attribute is present, the value of `"prefix"` must be a string beginning with `"/"`. If the value of the `"prefix"` attribute is longer than 1 character and ends in `"/"`, the trailing `"/"` is stripped. The purpose of the `"prefix"` attribute is to set the `SCRIPT_NAME` context value for WSGI applications and the `root_path` context value for ASGI applications, allowing applications to properly route requests regardless of the path that the server uses to expose the application. The context value is only set if the request's URL path begins with the value of the `"prefix"` attribute. In all other cases, the `SCRIPT_NAME` or `root_path` values are not set. In addition, for WSGI applications, the value of `"prefix"` will be stripped from the beginning of the request's URL path before it is sent to the application. Reviewed-by: Andrei Zeliankou <zelenkov@nginx.com> Reviewed-by: Artem Konev <artem.konev@nginx.com> Signed-off-by: Alejandro Colomar <alx@nginx.com>
Diffstat (limited to 'test')
-rw-r--r--test/python/prefix/asgi.py15
-rw-r--r--test/python/prefix/wsgi.py10
-rw-r--r--test/python/targets/asgi.py17
-rw-r--r--test/python/targets/wsgi.py9
-rw-r--r--test/test_asgi_application.py37
-rw-r--r--test/test_asgi_targets.py45
-rw-r--r--test/test_configuration.py86
-rw-r--r--test/test_python_application.py38
-rw-r--r--test/test_python_targets.py52
-rw-r--r--test/unit/applications/lang/python.py1
10 files changed, 310 insertions, 0 deletions
diff --git a/test/python/prefix/asgi.py b/test/python/prefix/asgi.py
new file mode 100644
index 00000000..234f084f
--- /dev/null
+++ b/test/python/prefix/asgi.py
@@ -0,0 +1,15 @@
+async def application(scope, receive, send):
+ assert scope['type'] == 'http'
+
+ await send(
+ {
+ 'type': 'http.response.start',
+ 'status': 200,
+ 'headers': [
+ (b'content-length', b'0'),
+ (b'prefix', scope.get('root_path', 'NULL').encode()),
+ ],
+ }
+ )
+
+ await send({'type': 'http.response.body', 'body': b''})
diff --git a/test/python/prefix/wsgi.py b/test/python/prefix/wsgi.py
new file mode 100644
index 00000000..83b58c9a
--- /dev/null
+++ b/test/python/prefix/wsgi.py
@@ -0,0 +1,10 @@
+def application(environ, start_response):
+ start_response(
+ '200',
+ [
+ ('Content-Length', '0'),
+ ('Script-Name', environ.get('SCRIPT_NAME', 'NULL')),
+ ('Path-Info', environ['PATH_INFO']),
+ ],
+ )
+ return []
diff --git a/test/python/targets/asgi.py b/test/python/targets/asgi.py
index b51f3964..749ec5b1 100644
--- a/test/python/targets/asgi.py
+++ b/test/python/targets/asgi.py
@@ -22,6 +22,23 @@ async def application_200(scope, receive, send):
)
+async def application_prefix(scope, receive, send):
+ assert scope['type'] == 'http'
+
+ await send(
+ {
+ 'type': 'http.response.start',
+ 'status': 200,
+ 'headers': [
+ (b'content-length', b'0'),
+ (b'prefix', scope.get('root_path', 'NULL').encode()),
+ ],
+ }
+ )
+
+ await send({'type': 'http.response.body', 'body': b''})
+
+
def legacy_application_200(scope):
assert scope['type'] == 'http'
diff --git a/test/python/targets/wsgi.py b/test/python/targets/wsgi.py
index fa17ab87..3f3d4b27 100644
--- a/test/python/targets/wsgi.py
+++ b/test/python/targets/wsgi.py
@@ -6,3 +6,12 @@ def wsgi_target_a(env, start_response):
def wsgi_target_b(env, start_response):
start_response('200', [('Content-Length', '1')])
return [b'2']
+
+
+def wsgi_target_prefix(env, start_response):
+ data = u'%s %s' % (
+ env.get('SCRIPT_NAME', 'No Script Name'),
+ env['PATH_INFO'],
+ )
+ start_response('200', [('Content-Length', '%d' % len(data))])
+ return [data.encode('utf-8')]
diff --git a/test/test_asgi_application.py b/test/test_asgi_application.py
index 7f5f1578..121a2fbc 100644
--- a/test/test_asgi_application.py
+++ b/test/test_asgi_application.py
@@ -79,6 +79,43 @@ custom-header: BLAH
resp['headers']['query-string'] == 'var1=val1&var2=val2'
), 'query-string header'
+ def test_asgi_application_prefix(self):
+ self.load('prefix', prefix='/api/rest')
+
+ def set_prefix(prefix):
+ self.conf('"' + prefix + '"', 'applications/prefix/prefix')
+
+ def check_prefix(url, prefix):
+ resp = self.get(url=url)
+ assert resp['status'] == 200
+ assert resp['headers']['prefix'] == prefix
+
+ check_prefix('/ap', 'NULL')
+ check_prefix('/api', 'NULL')
+ check_prefix('/api/', 'NULL')
+ check_prefix('/api/res', 'NULL')
+ check_prefix('/api/restful', 'NULL')
+ check_prefix('/api/rest', '/api/rest')
+ check_prefix('/api/rest/', '/api/rest')
+ check_prefix('/api/rest/get', '/api/rest')
+ check_prefix('/api/rest/get/blah', '/api/rest')
+
+ set_prefix('/api/rest/')
+ check_prefix('/api/rest', '/api/rest')
+ check_prefix('/api/restful', 'NULL')
+ check_prefix('/api/rest/', '/api/rest')
+ check_prefix('/api/rest/blah', '/api/rest')
+
+ set_prefix('/app')
+ check_prefix('/ap', 'NULL')
+ check_prefix('/app', '/app')
+ check_prefix('/app/', '/app')
+ check_prefix('/application/', 'NULL')
+
+ set_prefix('/')
+ check_prefix('/', 'NULL')
+ check_prefix('/app', 'NULL')
+
def test_asgi_application_query_string_space(self):
self.load('query_string')
diff --git a/test/test_asgi_targets.py b/test/test_asgi_targets.py
index c1e345ef..84d7b3b0 100644
--- a/test/test_asgi_targets.py
+++ b/test/test_asgi_targets.py
@@ -90,3 +90,48 @@ class TestASGITargets(TestApplicationPython):
)
assert self.get(url='/1')['status'] != 200
+
+ def test_asgi_targets_prefix(self):
+ self.conf_targets(
+ {
+ "1": {
+ "module": "asgi",
+ "callable": "application_prefix",
+ "prefix": "/1/",
+ },
+ "2": {
+ "module": "asgi",
+ "callable": "application_prefix",
+ "prefix": "/api",
+ },
+ }
+ )
+ self.conf(
+ [
+ {
+ "match": {"uri": "/1*"},
+ "action": {"pass": "applications/targets/1"},
+ },
+ {
+ "match": {"uri": "*"},
+ "action": {"pass": "applications/targets/2"},
+ },
+ ],
+ "routes",
+ )
+
+ def check_prefix(url, prefix):
+ resp = self.get(url=url)
+ assert resp['status'] == 200
+ assert resp['headers']['prefix'] == prefix
+
+ check_prefix('/1', '/1')
+ check_prefix('/11', 'NULL')
+ check_prefix('/1/', '/1')
+ check_prefix('/', 'NULL')
+ check_prefix('/ap', 'NULL')
+ check_prefix('/api', '/api')
+ check_prefix('/api/', '/api')
+ check_prefix('/api/test/', '/api')
+ check_prefix('/apis', 'NULL')
+ check_prefix('/apis/', 'NULL')
diff --git a/test/test_configuration.py b/test/test_configuration.py
index 7c612db0..9c27222c 100644
--- a/test/test_configuration.py
+++ b/test/test_configuration.py
@@ -318,6 +318,92 @@ class TestConfiguration(TestControl):
assert 'success' in self.conf(conf)
+ def test_json_application_python_prefix(self):
+ conf = {
+ "applications": {
+ "sub-app": {
+ "type": "python",
+ "processes": {"spare": 0},
+ "path": "/app",
+ "module": "wsgi",
+ "prefix": "/app",
+ }
+ },
+ "listeners": {"*:7080": {"pass": "routes"}},
+ "routes": [
+ {
+ "match": {"uri": "/app/*"},
+ "action": {"pass": "applications/sub-app"},
+ }
+ ],
+ }
+
+ assert 'success' in self.conf(conf)
+
+ def test_json_application_prefix_target(self):
+ conf = {
+ "applications": {
+ "sub-app": {
+ "type": "python",
+ "processes": {"spare": 0},
+ "path": "/app",
+ "targets": {
+ "foo": {"module": "foo.wsgi", "prefix": "/app"},
+ "bar": {
+ "module": "bar.wsgi",
+ "callable": "bar",
+ "prefix": "/api",
+ },
+ },
+ }
+ },
+ "listeners": {"*:7080": {"pass": "routes"}},
+ "routes": [
+ {
+ "match": {"uri": "/app/*"},
+ "action": {"pass": "applications/sub-app/foo"},
+ },
+ {
+ "match": {"uri": "/api/*"},
+ "action": {"pass": "applications/sub-app/bar"},
+ },
+ ],
+ }
+
+ assert 'success' in self.conf(conf)
+
+ def test_json_application_invalid_python_prefix(self):
+ conf = {
+ "applications": {
+ "sub-app": {
+ "type": "python",
+ "processes": {"spare": 0},
+ "path": "/app",
+ "module": "wsgi",
+ "prefix": "app",
+ }
+ },
+ "listeners": {"*:7080": {"pass": "applications/sub-app"}},
+ }
+
+ assert 'error' in self.conf(conf)
+
+ def test_json_application_empty_python_prefix(self):
+ conf = {
+ "applications": {
+ "sub-app": {
+ "type": "python",
+ "processes": {"spare": 0},
+ "path": "/app",
+ "module": "wsgi",
+ "prefix": "",
+ }
+ },
+ "listeners": {"*:7080": {"pass": "applications/sub-app"}},
+ }
+
+ assert 'error' in self.conf(conf)
+
def test_json_application_many2(self):
conf = {
"applications": {
diff --git a/test/test_python_application.py b/test/test_python_application.py
index 946d2118..c9483b6a 100644
--- a/test/test_python_application.py
+++ b/test/test_python_application.py
@@ -94,6 +94,44 @@ custom-header: BLAH
resp['headers']['Query-String'] == ' var1= val1 & var2=val2'
), 'Query-String space 4'
+ def test_python_application_prefix(self):
+ self.load('prefix', prefix='/api/rest')
+
+ def set_prefix(prefix):
+ self.conf('"' + prefix + '"', 'applications/prefix/prefix')
+
+ def check_prefix(url, script_name, path_info):
+ resp = self.get(url=url)
+ assert resp['status'] == 200
+ assert resp['headers']['Script-Name'] == script_name
+ assert resp['headers']['Path-Info'] == path_info
+
+ check_prefix('/ap', 'NULL', '/ap')
+ check_prefix('/api', 'NULL', '/api')
+ check_prefix('/api/', 'NULL', '/api/')
+ check_prefix('/api/res', 'NULL', '/api/res')
+ check_prefix('/api/restful', 'NULL', '/api/restful')
+ check_prefix('/api/rest', '/api/rest', '')
+ check_prefix('/api/rest/', '/api/rest', '/')
+ check_prefix('/api/rest/get', '/api/rest', '/get')
+ check_prefix('/api/rest/get/blah', '/api/rest', '/get/blah')
+
+ set_prefix('/api/rest/')
+ check_prefix('/api/rest', '/api/rest', '')
+ check_prefix('/api/restful', 'NULL', '/api/restful')
+ check_prefix('/api/rest/', '/api/rest', '/')
+ check_prefix('/api/rest/blah', '/api/rest', '/blah')
+
+ set_prefix('/app')
+ check_prefix('/ap', 'NULL', '/ap')
+ check_prefix('/app', '/app', '')
+ check_prefix('/app/', '/app', '/')
+ check_prefix('/application/', 'NULL', '/application/')
+
+ set_prefix('/')
+ check_prefix('/', 'NULL', '/')
+ check_prefix('/app', 'NULL', '/app')
+
def test_python_application_query_string_empty(self):
self.load('query_string')
diff --git a/test/test_python_targets.py b/test/test_python_targets.py
index 8e9ecb87..ae271b5f 100644
--- a/test/test_python_targets.py
+++ b/test/test_python_targets.py
@@ -47,3 +47,55 @@ class TestPythonTargets(TestApplicationPython):
resp = self.get(url='/2')
assert resp['status'] == 200
assert resp['body'] == '2'
+
+ def test_python_targets_prefix(self):
+ assert 'success' in self.conf(
+ {
+ "listeners": {"*:7080": {"pass": "routes"}},
+ "routes": [
+ {
+ "match": {"uri": ["/app*"]},
+ "action": {"pass": "applications/targets/app"},
+ },
+ {
+ "match": {"uri": "*"},
+ "action": {"pass": "applications/targets/catchall"},
+ },
+ ],
+ "applications": {
+ "targets": {
+ "type": "python",
+ "working_directory": option.test_dir
+ + "/python/targets/",
+ "path": option.test_dir + '/python/targets/',
+ "protocol": "wsgi",
+ "targets": {
+ "app": {
+ "module": "wsgi",
+ "callable": "wsgi_target_prefix",
+ "prefix": "/app/",
+ },
+ "catchall": {
+ "module": "wsgi",
+ "callable": "wsgi_target_prefix",
+ "prefix": "/api",
+ },
+ },
+ }
+ },
+ }
+ )
+
+ def check_prefix(url, body):
+ resp = self.get(url=url)
+ assert resp['status'] == 200
+ assert resp['body'] == body
+
+ check_prefix('/app', '/app ')
+ check_prefix('/app/', '/app /')
+ check_prefix('/app/rest/user/', '/app /rest/user/')
+ check_prefix('/catchall', 'No Script Name /catchall')
+ check_prefix('/api', '/api ')
+ check_prefix('/api/', '/api /')
+ check_prefix('/apis', 'No Script Name /apis')
+ check_prefix('/api/users/', '/api /users/')
diff --git a/test/unit/applications/lang/python.py b/test/unit/applications/lang/python.py
index 1e38f3fa..3768cf07 100644
--- a/test/unit/applications/lang/python.py
+++ b/test/unit/applications/lang/python.py
@@ -50,6 +50,7 @@ class TestApplicationPython(TestApplicationProto):
'protocol',
'targets',
'threads',
+ 'prefix',
):
if attr in kwargs:
app[attr] = kwargs.pop(attr)