summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrei Zeliankou <zelenkov@nginx.com>2023-02-21 15:35:38 +0000
committerAndrei Zeliankou <zelenkov@nginx.com>2023-02-21 15:35:38 +0000
commitfcabbf09d85397e244f6486d6eca25e2366b61cd (patch)
treeff0e1f437c9620620ae66c780069c787f6b5f875
parent7f046c80b9439b100965b3ee3c82b71a3b3a5aee (diff)
downloadunit-fcabbf09d85397e244f6486d6eca25e2366b61cd.tar.gz
unit-fcabbf09d85397e244f6486d6eca25e2366b61cd.tar.bz2
Tests: added Python tests with encoding.
-rw-r--r--test/python/encoding/wsgi.py12
-rw-r--r--test/python/unicode/wsgi.py8
-rw-r--r--test/test_python_application.py78
3 files changed, 98 insertions, 0 deletions
diff --git a/test/python/encoding/wsgi.py b/test/python/encoding/wsgi.py
new file mode 100644
index 00000000..0a916a9b
--- /dev/null
+++ b/test/python/encoding/wsgi.py
@@ -0,0 +1,12 @@
+import sys
+
+
+def application(environ, start_response):
+ start_response(
+ '200',
+ [
+ ('Content-Length', '0'),
+ ('X-Encoding', sys.getfilesystemencoding()),
+ ],
+ )
+ return []
diff --git a/test/python/unicode/wsgi.py b/test/python/unicode/wsgi.py
new file mode 100644
index 00000000..40043af9
--- /dev/null
+++ b/test/python/unicode/wsgi.py
@@ -0,0 +1,8 @@
+def application(environ, start_response):
+ temp_dir = environ.get('HTTP_TEMP_DIR')
+
+ with open(temp_dir + '/tempfile', 'w') as f:
+ f.write('\u26a0\ufe0f')
+
+ start_response('200', [('Content-Length', '0')])
+ return []
diff --git a/test/test_python_application.py b/test/test_python_application.py
index c9483b6a..c9065eae 100644
--- a/test/test_python_application.py
+++ b/test/test_python_application.py
@@ -2,9 +2,12 @@ import grp
import os
import pwd
import re
+import subprocess
import time
+import venv
import pytest
+from packaging import version
from unit.applications.lang.python import TestApplicationPython
@@ -526,6 +529,81 @@ last line: 987654321
assert self.get()['body'] == '0123456789', 'write'
+ def test_python_application_encoding(self):
+ self.load('encoding')
+
+ try:
+ locales = (
+ subprocess.check_output(
+ ['locale', '-a'],
+ stderr=subprocess.STDOUT,
+ )
+ .decode()
+ .splitlines()
+ )
+ except (
+ FileNotFoundError,
+ UnicodeDecodeError,
+ subprocess.CalledProcessError,
+ ):
+ pytest.skip('require locale')
+
+ to_check = [
+ re.compile(r'.*UTF[-_]?8'),
+ re.compile(r'.*ISO[-_]?8859[-_]?1'),
+ ]
+ matches = [
+ loc
+ for loc in locales
+ if any(pattern.match(loc.upper()) for pattern in to_check)
+ ]
+
+ if not matches:
+ pytest.skip('no available locales')
+
+ def unify(str):
+ str.upper().replace('-', '').replace('_', '')
+
+ for loc in matches:
+ assert 'success' in self.conf(
+ {"LC_CTYPE": loc, "LC_ALL": ""},
+ '/config/applications/encoding/environment',
+ )
+ resp = self.get()
+ assert resp['status'] == 200, 'status'
+ assert unify(resp['headers']['X-Encoding']) == unify(
+ loc.split('.')[-1]
+ )
+
+ def test_python_application_unicode(self, temp_dir):
+ try:
+ app_type = self.get_application_type()
+ v = version.Version(app_type.split()[-1])
+ if v.major != 3:
+ raise version.InvalidVersion
+
+ except version.InvalidVersion:
+ pytest.skip('require python module version 3')
+
+ venv_path = temp_dir + '/venv'
+ venv.create(venv_path)
+
+ self.load('unicode')
+ assert 'success' in self.conf(
+ '"' + venv_path + '"',
+ '/config/applications/unicode/home',
+ )
+ assert (
+ self.get(
+ headers={
+ 'Host': 'localhost',
+ 'Temp-dir': temp_dir,
+ 'Connection': 'close',
+ }
+ )['status']
+ == 200
+ )
+
def test_python_application_threading(self):
"""wait_for_record() timeouts after 5s while every thread works at
least 3s. So without releasing GIL test should fail.