diff options
author | Andrei Zeliankou <zelenkov@nginx.com> | 2023-07-11 15:51:53 +0100 |
---|---|---|
committer | Andrei Zeliankou <zelenkov@nginx.com> | 2023-07-11 15:51:53 +0100 |
commit | 2ad03caf327ce1e88b4d45112cf75480bb12a436 (patch) | |
tree | 3af123e19ea02464bc309ab258a78b4b7a42d9f4 | |
parent | 458722df55bb9727680b7734e7fff8fb5639db28 (diff) | |
download | unit-2ad03caf327ce1e88b4d45112cf75480bb12a436.tar.gz unit-2ad03caf327ce1e88b4d45112cf75480bb12a436.tar.bz2 |
Tests: added tests for response header variables.
-rw-r--r-- | test/python/chunked/wsgi.py | 18 | ||||
-rw-r--r-- | test/test_variables.py | 93 |
2 files changed, 111 insertions, 0 deletions
diff --git a/test/python/chunked/wsgi.py b/test/python/chunked/wsgi.py new file mode 100644 index 00000000..23ee81fc --- /dev/null +++ b/test/python/chunked/wsgi.py @@ -0,0 +1,18 @@ +def application(environ, start_response): + + content_length = int(environ.get('CONTENT_LENGTH', 0)) + body = bytes(environ['wsgi.input'].read(content_length)) + + header_transfer = environ.get('HTTP_X_TRANSFER') + header_length = environ.get('HTTP_X_LENGTH') + + headers = [] + + if header_length: + headers.append(('Content-Length', '0')) + + if header_transfer: + headers.append(('Transfer-Encoding', header_transfer)) + + start_response('200', headers) + return [body] diff --git a/test/test_variables.py b/test/test_variables.py index 7322a802..c9b173fa 100644 --- a/test/test_variables.py +++ b/test/test_variables.py @@ -5,9 +5,11 @@ import time 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) @@ -400,6 +402,97 @@ def test_variables_dynamic_cookies(search_in_file, wait_for_record): check_no_cookie('foo_bar=') +def test_variables_response_header(temp_dir, wait_for_record): + # If response has two headers with the same name then first value + # will be stored in variable. + # $response_header_transfer_encoding value can be 'chunked' or null only. + + # return + + set_format( + 'return@$response_header_server@$response_header_date@' + '$response_header_content_length@$response_header_connection' + ) + + assert client.get()['status'] == 200 + assert ( + wait_for_record(r'return@Unit/.*@.*GMT@0@close', 'access.log') + is not None + ) + + # share + + Path(f'{temp_dir}/foo').mkdir() + Path(f'{temp_dir}/foo/index.html').write_text('index') + + assert 'success' in client.conf( + { + "listeners": {"*:7080": {"pass": "routes"}}, + "routes": [ + { + "action": { + "share": f'{temp_dir}$uri', + } + } + ], + } + ) + + set_format( + 'share@$response_header_last_modified@$response_header_etag@' + '$response_header_content_type@$response_header_server@' + '$response_header_date@$response_header_content_length@' + '$response_header_connection' + ) + + assert client.get(url='/foo/index.html')['status'] == 200 + assert ( + wait_for_record( + r'share@.*GMT@".*"@text/html@Unit/.*@.*GMT@5@close', 'access.log' + ) + is not None + ) + + # redirect + + set_format( + 'redirect@$response_header_location@$response_header_server@' + '$response_header_date@$response_header_content_length@' + '$response_header_connection' + ) + + assert client.get(url='/foo')['status'] == 301 + assert ( + wait_for_record(r'redirect@/foo/@Unit/.*@.*GMT@0@close', 'access.log') + is not None + ) + + # error + + set_format( + 'error@$response_header_content_type@$response_header_server@' + '$response_header_date@$response_header_content_length@' + '$response_header_connection' + ) + + assert client.get(url='/blah')['status'] == 404 + assert ( + wait_for_record(r'error@text/html@Unit/.*@.*GMT@54@close', 'access.log') + is not None + ) + + +def test_variables_response_header_application(require, wait_for_record): + require({'modules': {'python': 'any'}}) + + client_python.load('chunked') + + set_format('$uri@$response_header_transfer_encoding') + + assert client_python.get(url='/1')['status'] == 200 + assert wait_for_record(r'/1@chunked', 'access.log') is not None + + def test_variables_invalid(temp_dir): def check_variables(format): assert 'error' in client.conf( |