Skip to content

Commit 1b7cf1f

Browse files
Tests: added Python tests with encoding.
1 parent 7d7b5a9 commit 1b7cf1f

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

test/python/encoding/wsgi.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import sys
2+
3+
4+
def application(environ, start_response):
5+
start_response(
6+
'200',
7+
[
8+
('Content-Length', '0'),
9+
('X-Encoding', sys.getfilesystemencoding()),
10+
],
11+
)
12+
return []

test/python/unicode/wsgi.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def application(environ, start_response):
2+
temp_dir = environ.get('HTTP_TEMP_DIR')
3+
4+
with open(temp_dir + '/tempfile', 'w') as f:
5+
f.write('\u26a0\ufe0f')
6+
7+
start_response('200', [('Content-Length', '0')])
8+
return []

test/test_python_application.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
import os
33
import pwd
44
import re
5+
import subprocess
56
import time
7+
import venv
68

79
import pytest
10+
from packaging import version
811
from unit.applications.lang.python import TestApplicationPython
912

1013

@@ -526,6 +529,81 @@ def test_python_application_write(self):
526529

527530
assert self.get()['body'] == '0123456789', 'write'
528531

532+
def test_python_application_encoding(self):
533+
self.load('encoding')
534+
535+
try:
536+
locales = (
537+
subprocess.check_output(
538+
['locale', '-a'],
539+
stderr=subprocess.STDOUT,
540+
)
541+
.decode()
542+
.splitlines()
543+
)
544+
except (
545+
FileNotFoundError,
546+
UnicodeDecodeError,
547+
subprocess.CalledProcessError,
548+
):
549+
pytest.skip('require locale')
550+
551+
to_check = [
552+
re.compile(r'.*UTF[-_]?8'),
553+
re.compile(r'.*ISO[-_]?8859[-_]?1'),
554+
]
555+
matches = [
556+
loc
557+
for loc in locales
558+
if any(pattern.match(loc.upper()) for pattern in to_check)
559+
]
560+
561+
if not matches:
562+
pytest.skip('no available locales')
563+
564+
def unify(str):
565+
str.upper().replace('-', '').replace('_', '')
566+
567+
for loc in matches:
568+
assert 'success' in self.conf(
569+
{"LC_CTYPE": loc, "LC_ALL": ""},
570+
'/config/applications/encoding/environment',
571+
)
572+
resp = self.get()
573+
assert resp['status'] == 200, 'status'
574+
assert unify(resp['headers']['X-Encoding']) == unify(
575+
loc.split('.')[-1]
576+
)
577+
578+
def test_python_application_unicode(self, temp_dir):
579+
try:
580+
app_type = self.get_application_type()
581+
v = version.Version(app_type.split()[-1])
582+
if v.major != 3:
583+
raise version.InvalidVersion
584+
585+
except version.InvalidVersion:
586+
pytest.skip('require python module version 3')
587+
588+
venv_path = temp_dir + '/venv'
589+
venv.create(venv_path)
590+
591+
self.load('unicode')
592+
assert 'success' in self.conf(
593+
'"' + venv_path + '"',
594+
'/config/applications/unicode/home',
595+
)
596+
assert (
597+
self.get(
598+
headers={
599+
'Host': 'localhost',
600+
'Temp-dir': temp_dir,
601+
'Connection': 'close',
602+
}
603+
)['status']
604+
== 200
605+
)
606+
529607
def test_python_application_threading(self):
530608
"""wait_for_record() timeouts after 5s while every thread works at
531609
least 3s. So without releasing GIL test should fail.

0 commit comments

Comments
 (0)