summaryrefslogtreecommitdiffhomepage
path: root/test/test_njs.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_njs.py')
-rw-r--r--test/test_njs.py43
1 files changed, 39 insertions, 4 deletions
diff --git a/test/test_njs.py b/test/test_njs.py
index 162cc0bd..8ef815fd 100644
--- a/test/test_njs.py
+++ b/test/test_njs.py
@@ -1,6 +1,7 @@
-import os
+from pathlib import Path
import pytest
+
from unit.applications.proto import ApplicationProto
from unit.option import option
from unit.utils import waitforfiles
@@ -14,7 +15,7 @@ client = ApplicationProto()
def setup_method_fixture(temp_dir):
assert 'success' in client.conf(
{
- "listeners": {"*:7080": {"pass": "routes"}},
+ "listeners": {"*:8080": {"pass": "routes"}},
"routes": [{"action": {"share": f"{temp_dir}/assets$uri"}}],
}
)
@@ -22,9 +23,9 @@ def setup_method_fixture(temp_dir):
def create_files(*files):
assets_dir = f'{option.temp_dir}/assets/'
- os.makedirs(assets_dir)
+ Path(assets_dir).mkdir(exist_ok=True)
- [open(assets_dir + f, 'a') for f in files]
+ _ = [Path(assets_dir + f).touch() for f in files]
waitforfiles(*[assets_dir + f for f in files])
@@ -82,6 +83,38 @@ def test_njs_variables(temp_dir):
set_share(f'"`{temp_dir}/assets/${{args.foo}}`"')
assert client.get(url='/?foo=str')['status'] == 200, 'args'
+ check_expression('/${vars.header_host}')
+
+ set_share(f'"`{temp_dir}/assets/${{vars[\\"arg_foo\\"]}}`"')
+ assert client.get(url='/?foo=str')['status'] == 200, 'vars'
+
+ set_share(f'"`{temp_dir}/assets/${{vars.non_exist}}`"')
+ assert client.get()['status'] == 404, 'undefined'
+
+ create_files('undefined')
+ assert client.get()['status'] == 200, 'undefined 2'
+
+
+def test_njs_variables_cacheable(temp_dir):
+ create_files('str')
+
+ def check_rewrite(rewrite, uri):
+ assert 'success' in client.conf(
+ [
+ {
+ "action": {
+ "rewrite": rewrite,
+ "share": f"`{temp_dir}/assets{uri}`",
+ },
+ },
+ ],
+ 'routes',
+ )
+ assert client.get()['status'] == 200
+
+ check_rewrite('/str', '${uri}')
+ check_rewrite('/str', '${vars.uri}')
+
def test_njs_invalid(skip_alert):
skip_alert(r'js exception:')
@@ -92,6 +125,7 @@ def test_njs_invalid(skip_alert):
check_invalid('"`a"')
check_invalid('"`a``"')
check_invalid('"`a`/"')
+ check_invalid('"`${vars.}`"')
def check_invalid_resolve(template):
assert 'success' in client.conf(template, 'routes/0/action/share')
@@ -99,3 +133,4 @@ def test_njs_invalid(skip_alert):
check_invalid_resolve('"`${a}`"')
check_invalid_resolve('"`${uri.a.a}`"')
+ check_invalid_resolve('"`${vars.a.a}`"')