summaryrefslogtreecommitdiffhomepage
path: root/test/test_response_headers.py
diff options
context:
space:
mode:
authorIppolitov Igor <iippolitov@nginx.com>2023-10-19 12:50:39 +0100
committerIppolitov Igor <iippolitov@nginx.com>2023-10-19 12:50:39 +0100
commitc43629880472bba8d389dfb0b7ae6d883b0ba499 (patch)
treeacecdcb36cfb85fac3d8cdbfbe473c26ac2e2686 /test/test_response_headers.py
parent8c4425ccb9a413e8d0506e0254f0e84bd89a32a6 (diff)
parentfb33ec86a3b6ca6a844dfa6980bb9e083094abec (diff)
downloadunit-1.31.1-1.tar.gz
unit-1.31.1-1.tar.bz2
Merged with the default branch.1.31.1-1
Diffstat (limited to '')
-rw-r--r--test/test_response_headers.py173
1 files changed, 173 insertions, 0 deletions
diff --git a/test/test_response_headers.py b/test/test_response_headers.py
new file mode 100644
index 00000000..50f47d9a
--- /dev/null
+++ b/test/test_response_headers.py
@@ -0,0 +1,173 @@
+from pathlib import Path
+
+import pytest
+from unit.applications.proto import ApplicationProto
+from unit.applications.lang.python import ApplicationPython
+from unit.option import option
+
+client = ApplicationProto()
+client_python = ApplicationPython()
+
+
+@pytest.fixture(autouse=True)
+def setup_method_fixture(temp_dir):
+ path = Path(f'{temp_dir}/index.html')
+ path.write_text('0123456789')
+
+ assert 'success' in client.conf(
+ {
+ "listeners": {
+ "*:7080": {"pass": "routes"},
+ },
+ "routes": [
+ {
+ "action": {
+ "share": str(path),
+ "response_headers": {
+ "X-Foo": "foo",
+ },
+ }
+ }
+ ],
+ }
+ )
+
+
+def action_update(conf):
+ assert 'success' in client.conf(conf, 'routes/0/action')
+
+
+def test_response_headers(temp_dir):
+ resp = client.get()
+ assert resp['status'] == 200, 'status 200'
+ assert resp['headers']['X-Foo'] == 'foo', 'header 200'
+
+ assert 'success' in client.conf(f'"{temp_dir}"', 'routes/0/action/share')
+
+ resp = client.get()
+ assert resp['status'] == 301, 'status 301'
+ assert resp['headers']['X-Foo'] == 'foo', 'header 301'
+
+ assert 'success' in client.conf('"/blah"', 'routes/0/action/share')
+
+ resp = client.get()
+ assert resp['status'] == 404, 'status 404'
+ assert 'X-Foo' not in client.get()['headers'], 'header 404'
+
+
+def test_response_last_action():
+ assert 'success' in client.conf(
+ {
+ "listeners": {
+ "*:7080": {"pass": "routes/first"},
+ },
+ "routes": {
+ "first": [
+ {
+ "action": {
+ "pass": "routes/second",
+ "response_headers": {
+ "X-Foo": "foo",
+ },
+ }
+ }
+ ],
+ "second": [
+ {
+ "action": {"return": 200},
+ }
+ ],
+ },
+ "applications": {},
+ }
+ )
+
+ assert 'X-Foo' not in client.get()['headers']
+
+
+def test_response_pass(require):
+ require({'modules': {'python': 'any'}})
+
+ assert 'success' in client_python.conf(
+ {
+ "listeners": {
+ "*:7080": {"pass": "routes"},
+ },
+ "routes": [
+ {
+ "action": {
+ "pass": "applications/empty",
+ "response_headers": {
+ "X-Foo": "foo",
+ },
+ }
+ },
+ ],
+ "applications": {
+ "empty": {
+ "type": client_python.get_application_type(),
+ "processes": {"spare": 0},
+ "path": f'{option.test_dir}/python/empty',
+ "working_directory": f'{option.test_dir}/python/empty',
+ "module": "wsgi",
+ }
+ },
+ }
+ )
+
+ assert client.get()['headers']['X-Foo'] == 'foo'
+
+
+def test_response_fallback():
+ assert 'success' in client.conf(
+ {
+ "listeners": {"*:7080": {"pass": "routes"}},
+ "routes": [
+ {
+ "action": {
+ "share": "/blah",
+ "fallback": {
+ "return": 200,
+ "response_headers": {
+ "X-Foo": "foo",
+ },
+ },
+ }
+ }
+ ],
+ }
+ )
+
+ assert client.get()['headers']['X-Foo'] == 'foo'
+
+
+def test_response_headers_var():
+ assert 'success' in client.conf(
+ {
+ "X-Foo": "$uri",
+ },
+ 'routes/0/action/response_headers',
+ )
+
+ assert client.get()['headers']['X-Foo'] == '/'
+
+
+def test_response_headers_remove():
+ assert 'success' in client.conf(
+ {"etag": None},
+ 'routes/0/action/response_headers',
+ )
+
+ assert 'ETag' not in client.get()['headers']
+
+
+def test_response_headers_invalid(skip_alert):
+ skip_alert(r'failed to apply new conf')
+
+ def check_invalid(conf):
+ assert 'error' in client.conf(
+ conf,
+ 'routes/0/action/response_headers',
+ )
+
+ check_invalid({"X-Foo": "$u"})