summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMax Romanov <max.romanov@nginx.com>2020-10-01 23:55:43 +0300
committerMax Romanov <max.romanov@nginx.com>2020-10-01 23:55:43 +0300
commit12f225a43acc6b5086b08c3d7df6f6ac2322efa1 (patch)
treeb74841e98672e5fe9233175ec11cb9a63739f27d
parentd97e3a3296db77f6a33ce010a66d2a0b2d4bac49 (diff)
downloadunit-12f225a43acc6b5086b08c3d7df6f6ac2322efa1.tar.gz
unit-12f225a43acc6b5086b08c3d7df6f6ac2322efa1.tar.bz2
Tests: added ASGI HTTP applications.
Diffstat (limited to '')
-rw-r--r--test/python/204_no_content/asgi.py8
-rw-r--r--test/python/delayed/asgi.py51
-rw-r--r--test/python/empty/asgi.py10
-rw-r--r--test/python/mirror/asgi.py22
-rw-r--r--test/python/query_string/asgi.py11
-rw-r--r--test/python/server_port/asgi.py11
-rw-r--r--test/python/threading/asgi.py42
-rw-r--r--test/python/variables/asgi.py40
-rw-r--r--test/test_asgi_application.py403
9 files changed, 598 insertions, 0 deletions
diff --git a/test/python/204_no_content/asgi.py b/test/python/204_no_content/asgi.py
new file mode 100644
index 00000000..634facc2
--- /dev/null
+++ b/test/python/204_no_content/asgi.py
@@ -0,0 +1,8 @@
+async def application(scope, receive, send):
+ assert scope['type'] == 'http'
+
+ await send({
+ 'type': 'http.response.start',
+ 'status': 204,
+ 'headers': [],
+ })
diff --git a/test/python/delayed/asgi.py b/test/python/delayed/asgi.py
new file mode 100644
index 00000000..d5cad929
--- /dev/null
+++ b/test/python/delayed/asgi.py
@@ -0,0 +1,51 @@
+import asyncio
+
+async def application(scope, receive, send):
+ assert scope['type'] == 'http'
+
+ body = b''
+ while True:
+ m = await receive()
+ body += m.get('body', b'')
+ if not m.get('more_body', False):
+ break
+
+ headers = scope.get('headers', [])
+
+ def get_header(n, v=None):
+ for h in headers:
+ if h[0] == n:
+ return h[1]
+ return v
+
+ parts = int(get_header(b'x-parts', 1))
+ delay = int(get_header(b'x-delay', 0))
+
+ loop = asyncio.get_event_loop()
+
+ async def sleep(n):
+ future = loop.create_future()
+ loop.call_later(n, future.set_result, None)
+ await future
+
+ await send({
+ 'type': 'http.response.start',
+ 'status': 200,
+ 'headers': [
+ (b'content-length', str(len(body)).encode()),
+ ]
+ })
+
+ if not body:
+ await sleep(delay)
+ return
+
+ step = int(len(body) / parts)
+ for i in range(0, len(body), step):
+ await send({
+ 'type': 'http.response.body',
+ 'body': body[i : i + step],
+ 'more_body': True,
+ })
+
+ await sleep(delay)
diff --git a/test/python/empty/asgi.py b/test/python/empty/asgi.py
new file mode 100644
index 00000000..58b7c1f2
--- /dev/null
+++ b/test/python/empty/asgi.py
@@ -0,0 +1,10 @@
+async def application(scope, receive, send):
+ assert scope['type'] == 'http'
+
+ await send({
+ 'type': 'http.response.start',
+ 'status': 200,
+ 'headers': [
+ (b'content-length', b'0'),
+ ]
+ })
diff --git a/test/python/mirror/asgi.py b/test/python/mirror/asgi.py
new file mode 100644
index 00000000..7088e893
--- /dev/null
+++ b/test/python/mirror/asgi.py
@@ -0,0 +1,22 @@
+async def application(scope, receive, send):
+ assert scope['type'] == 'http'
+
+ body = b''
+ while True:
+ m = await receive()
+ body += m.get('body', b'')
+ if not m.get('more_body', False):
+ break
+
+ await send({
+ 'type': 'http.response.start',
+ 'status': 200,
+ 'headers': [
+ (b'content-length', str(len(body)).encode()),
+ ]
+ })
+
+ await send({
+ 'type': 'http.response.body',
+ 'body': body,
+ })
diff --git a/test/python/query_string/asgi.py b/test/python/query_string/asgi.py
new file mode 100644
index 00000000..28f4d107
--- /dev/null
+++ b/test/python/query_string/asgi.py
@@ -0,0 +1,11 @@
+async def application(scope, receive, send):
+ assert scope['type'] == 'http'
+
+ await send({
+ 'type': 'http.response.start',
+ 'status': 200,
+ 'headers': [
+ (b'content-length', b'0'),
+ (b'query-string', scope['query_string']),
+ ]
+ })
diff --git a/test/python/server_port/asgi.py b/test/python/server_port/asgi.py
new file mode 100644
index 00000000..e79ced00
--- /dev/null
+++ b/test/python/server_port/asgi.py
@@ -0,0 +1,11 @@
+async def application(scope, receive, send):
+ assert scope['type'] == 'http'
+
+ await send({
+ 'type': 'http.response.start',
+ 'status': 200,
+ 'headers': [
+ (b'content-length', b'0'),
+ (b'server-port', str(scope['server'][1]).encode()),
+ ]
+ })
diff --git a/test/python/threading/asgi.py b/test/python/threading/asgi.py
new file mode 100644
index 00000000..3c978e50
--- /dev/null
+++ b/test/python/threading/asgi.py
@@ -0,0 +1,42 @@
+import asyncio
+import sys
+import time
+import threading
+
+
+class Foo(threading.Thread):
+ num = 10
+
+ def __init__(self, x):
+ self.__x = x
+ threading.Thread.__init__(self)
+
+ def log_index(self, index):
+ sys.stderr.write(
+ "(" + str(index) + ") Thread: " + str(self.__x) + "\n"
+ )
+ sys.stderr.flush()
+
+ def run(self):
+ i = 0
+ for _ in range(3):
+ self.log_index(i)
+ i += 1
+ time.sleep(1)
+ self.log_index(i)
+ i += 1
+
+
+async def application(scope, receive, send):
+ assert scope['type'] == 'http'
+
+ Foo(Foo.num).start()
+ Foo.num += 10
+
+ await send({
+ 'type': 'http.response.start',
+ 'status': 200,
+ 'headers': [
+ (b'content-length', b'0'),
+ ]
+ })
diff --git a/test/python/variables/asgi.py b/test/python/variables/asgi.py
new file mode 100644
index 00000000..dd1cca72
--- /dev/null
+++ b/test/python/variables/asgi.py
@@ -0,0 +1,40 @@
+async def application(scope, receive, send):
+ assert scope['type'] == 'http'
+
+ body = b''
+ while True:
+ m = await receive()
+ body += m.get('body', b'')
+ if not m.get('more_body', False):
+ break
+
+ headers = scope.get('headers', [])
+
+ def get_header(n):
+ res = []
+ for h in headers:
+ if h[0] == n:
+ res.append(h[1])
+ return b', '.join(res)
+
+ await send({
+ 'type': 'http.response.start',
+ 'status': 200,
+ 'headers': [
+ (b'content-type', get_header(b'content-type')),
+ (b'content-length', str(len(body)).encode()),
+ (b'request-method', scope['method'].encode()),
+ (b'request-uri', scope['path'].encode()),
+ (b'http-host', get_header(b'host')),
+ (b'http-version', scope['http_version'].encode()),
+ (b'asgi-version', scope['asgi']['version'].encode()),
+ (b'asgi-spec-version', scope['asgi']['spec_version'].encode()),
+ (b'scheme', scope['scheme'].encode()),
+ (b'custom-header', get_header(b'custom-header')),
+ ]
+ })
+
+ await send({
+ 'type': 'http.response.body',
+ 'body': body,
+ })
diff --git a/test/test_asgi_application.py b/test/test_asgi_application.py
new file mode 100644
index 00000000..7816caec
--- /dev/null
+++ b/test/test_asgi_application.py
@@ -0,0 +1,403 @@
+import grp
+import pytest
+import pwd
+import re
+import time
+from distutils.version import LooseVersion
+
+from unit.applications.lang.python import TestApplicationPython
+from conftest import skip_alert
+
+
+class TestASGIApplication(TestApplicationPython):
+ prerequisites = {'modules': {'python':
+ lambda v: LooseVersion(v) >= LooseVersion('3.5')}}
+ load_module = 'asgi'
+
+ def findall(self, pattern):
+ with open(self.temp_dir + '/unit.log', 'r', errors='ignore') as f:
+ return re.findall(pattern, f.read())
+
+ def test_asgi_application__variables(self):
+ self.load('variables')
+
+ body = 'Test body string.'
+
+ resp = self.http(
+ b"""POST / HTTP/1.1
+Host: localhost
+Content-Length: %d
+Custom-Header: blah
+Custom-hEader: Blah
+Content-Type: text/html
+Connection: close
+custom-header: BLAH
+
+%s""" % (len(body), body.encode()),
+ raw=True,
+ )
+
+ assert resp['status'] == 200, 'status'
+ headers = resp['headers']
+ header_server = headers.pop('Server')
+ assert re.search(r'Unit/[\d\.]+', header_server), 'server header'
+
+ date = headers.pop('Date')
+ assert date[-4:] == ' GMT', 'date header timezone'
+ assert (
+ abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5
+ ), 'date header'
+
+ assert headers == {
+ 'Connection': 'close',
+ 'content-length': str(len(body)),
+ 'content-type': 'text/html',
+ 'request-method': 'POST',
+ 'request-uri': '/',
+ 'http-host': 'localhost',
+ 'http-version': '1.1',
+ 'custom-header': 'blah, Blah, BLAH',
+ 'asgi-version': '3.0',
+ 'asgi-spec-version': '2.1',
+ 'scheme': 'http',
+ }, 'headers'
+ assert resp['body'] == body, 'body'
+
+ def test_asgi_application__query_string(self):
+ self.load('query_string')
+
+ resp = self.get(url='/?var1=val1&var2=val2')
+
+ assert (
+ resp['headers']['query-string'] == 'var1=val1&var2=val2'
+ ), 'query-string header'
+
+ def test_asgi_application__query_string_space(self):
+ self.load('query_string')
+
+ resp = self.get(url='/ ?var1=val1&var2=val2')
+ assert (
+ resp['headers']['query-string'] == 'var1=val1&var2=val2'
+ ), 'query-string space'
+
+ resp = self.get(url='/ %20?var1=val1&var2=val2')
+ assert (
+ resp['headers']['query-string'] == 'var1=val1&var2=val2'
+ ), 'query-string space 2'
+
+ resp = self.get(url='/ %20 ?var1=val1&var2=val2')
+ assert (
+ resp['headers']['query-string'] == 'var1=val1&var2=val2'
+ ), 'query-string space 3'
+
+ resp = self.get(url='/blah %20 blah? var1= val1 & var2=val2')
+ assert (
+ resp['headers']['query-string'] == ' var1= val1 & var2=val2'
+ ), 'query-string space 4'
+
+ def test_asgi_application__query_string_empty(self):
+ self.load('query_string')
+
+ resp = self.get(url='/?')
+
+ assert resp['status'] == 200, 'query string empty status'
+ assert resp['headers']['query-string'] == '', 'query string empty'
+
+ def test_asgi_application__query_string_absent(self):
+ self.load('query_string')
+
+ resp = self.get()
+
+ assert resp['status'] == 200, 'query string absent status'
+ assert resp['headers']['query-string'] == '', 'query string absent'
+
+ @pytest.mark.skip('not yet')
+ def test_asgi_application__server_port(self):
+ self.load('server_port')
+
+ assert (
+ self.get()['headers']['Server-Port'] == '7080'
+ ), 'Server-Port header'
+
+ @pytest.mark.skip('not yet')
+ def test_asgi_application__working_directory_invalid(self):
+ self.load('empty')
+
+ assert 'success' in self.conf(
+ '"/blah"', 'applications/empty/working_directory'
+ ), 'configure invalid working_directory'
+
+ assert self.get()['status'] == 500, 'status'
+
+ def test_asgi_application__204_transfer_encoding(self):
+ self.load('204_no_content')
+
+ assert (
+ 'Transfer-Encoding' not in self.get()['headers']
+ ), '204 header transfer encoding'
+
+ def test_asgi_application__shm_ack_handle(self):
+ self.load('mirror')
+
+ # Minimum possible limit
+ shm_limit = 10 * 1024 * 1024
+
+ assert (
+ 'success' in self.conf('{"shm": ' + str(shm_limit) + '}',
+ 'applications/mirror/limits')
+ )
+
+ # Should exceed shm_limit
+ max_body_size = 12 * 1024 * 1024
+
+ assert (
+ 'success' in self.conf('{"http":{"max_body_size": '
+ + str(max_body_size) + ' }}',
+ 'settings')
+ )
+
+ assert self.get()['status'] == 200, 'init'
+
+ body = '0123456789AB' * 1024 * 1024 # 12 Mb
+ resp = self.post(
+ headers={
+ 'Host': 'localhost',
+ 'Connection': 'close',
+ 'Content-Type': 'text/html',
+ },
+ body=body,
+ read_buffer_size=1024 * 1024,
+ )
+
+ assert resp['body'] == body, 'keep-alive 1'
+
+ def test_asgi_keepalive_body(self):
+ self.load('mirror')
+
+ assert self.get()['status'] == 200, 'init'
+
+ body = '0123456789' * 500
+ (resp, sock) = self.post(
+ headers={
+ 'Host': 'localhost',
+ 'Connection': 'keep-alive',
+ 'Content-Type': 'text/html',
+ },
+ start=True,
+ body=body,
+ read_timeout=1,
+ )
+
+ assert resp['body'] == body, 'keep-alive 1'
+
+ body = '0123456789'
+ resp = self.post(
+ headers={
+ 'Host': 'localhost',
+ 'Connection': 'close',
+ 'Content-Type': 'text/html',
+ },
+ sock=sock,
+ body=body,
+ )
+
+ assert resp['body'] == body, 'keep-alive 2'
+
+ def test_asgi_keepalive_reconfigure(self):
+ skip_alert(
+ r'pthread_mutex.+failed',
+ r'failed to apply',
+ r'process \d+ exited on signal',
+ )
+ self.load('mirror')
+
+ assert self.get()['status'] == 200, 'init'
+
+ body = '0123456789'
+ conns = 3
+ socks = []
+
+ for i in range(conns):
+ (resp, sock) = self.post(
+ headers={
+ 'Host': 'localhost',
+ 'Connection': 'keep-alive',
+ 'Content-Type': 'text/html',
+ },
+ start=True,
+ body=body,
+ read_timeout=1,
+ )
+
+ assert resp['body'] == body, 'keep-alive open'
+ assert 'success' in self.conf(
+ str(i + 1), 'applications/mirror/processes'
+ ), 'reconfigure'
+
+ socks.append(sock)
+
+ for i in range(conns):
+ (resp, sock) = self.post(
+ headers={
+ 'Host': 'localhost',
+ 'Connection': 'keep-alive',
+ 'Content-Type': 'text/html',
+ },
+ start=True,
+ sock=socks[i],
+ body=body,
+ read_timeout=1,
+ )
+
+ assert resp['body'] == body, 'keep-alive request'
+ assert 'success' in self.conf(
+ str(i + 1), 'applications/mirror/processes'
+ ), 'reconfigure 2'
+
+ for i in range(conns):
+ resp = self.post(
+ headers={
+ 'Host': 'localhost',
+ 'Connection': 'close',
+ 'Content-Type': 'text/html',
+ },
+ sock=socks[i],
+ body=body,
+ )
+
+ assert resp['body'] == body, 'keep-alive close'
+ assert 'success' in self.conf(
+ str(i + 1), 'applications/mirror/processes'
+ ), 'reconfigure 3'
+
+ def test_asgi_keepalive_reconfigure_2(self):
+ self.load('mirror')
+
+ assert self.get()['status'] == 200, 'init'
+
+ body = '0123456789'
+
+ (resp, sock) = self.post(
+ headers={
+ 'Host': 'localhost',
+ 'Connection': 'keep-alive',
+ 'Content-Type': 'text/html',
+ },
+ start=True,
+ body=body,
+ read_timeout=1,
+ )
+
+ assert resp['body'] == body, 'reconfigure 2 keep-alive 1'
+
+ self.load('empty')
+
+ assert self.get()['status'] == 200, 'init'
+
+ (resp, sock) = self.post(
+ headers={
+ 'Host': 'localhost',
+ 'Connection': 'close',
+ 'Content-Type': 'text/html',
+ },
+ start=True,
+ sock=sock,
+ body=body,
+ )
+
+ assert resp['status'] == 200, 'reconfigure 2 keep-alive 2'
+ assert resp['body'] == '', 'reconfigure 2 keep-alive 2 body'
+
+ assert 'success' in self.conf(
+ {"listeners": {}, "applications": {}}
+ ), 'reconfigure 2 clear configuration'
+
+ resp = self.get(sock=sock)
+
+ assert resp == {}, 'reconfigure 2 keep-alive 3'
+
+ def test_asgi_keepalive_reconfigure_3(self):
+ self.load('empty')
+
+ assert self.get()['status'] == 200, 'init'
+
+ (_, sock) = self.http(
+ b"""GET / HTTP/1.1
+""",
+ start=True,
+ raw=True,
+ no_recv=True,
+ )
+
+ assert self.get()['status'] == 200
+
+ assert 'success' in self.conf(
+ {"listeners": {}, "applications": {}}
+ ), 'reconfigure 3 clear configuration'
+
+ resp = self.http(
+ b"""Host: localhost
+Connection: close
+
+""",
+ sock=sock,
+ raw=True,
+ )
+
+ assert resp['status'] == 200, 'reconfigure 3'
+
+ def test_asgi_process_switch(self):
+ self.load('delayed')
+
+ assert 'success' in self.conf(
+ '2', 'applications/delayed/processes'
+ ), 'configure 2 processes'
+
+ self.get(
+ headers={
+ 'Host': 'localhost',
+ 'Content-Length': '0',
+ 'X-Delay': '5',
+ 'Connection': 'close',
+ },
+ no_recv=True,
+ )
+
+ headers_delay_1 = {
+ 'Connection': 'close',
+ 'Host': 'localhost',
+ 'Content-Length': '0',
+ 'X-Delay': '1',
+ }
+
+ self.get(headers=headers_delay_1, no_recv=True)
+
+ time.sleep(0.5)
+
+ for _ in range(10):
+ self.get(headers=headers_delay_1, no_recv=True)
+
+ self.get(headers=headers_delay_1)
+
+ def test_asgi_application__loading_error(self):
+ skip_alert(r'Python failed to import module "blah"')
+
+ self.load('empty')
+
+ assert 'success' in self.conf('"blah"', 'applications/empty/module')
+
+ assert self.get()['status'] == 503, 'loading error'
+
+ def test_asgi_application__threading(self):
+ """wait_for_record() timeouts after 5s while every thread works at
+ least 3s. So without releasing GIL test should fail.
+ """
+
+ self.load('threading')
+
+ for _ in range(10):
+ self.get(no_recv=True)
+
+ assert (
+ self.wait_for_record(r'\(5\) Thread: 100') is not None
+ ), 'last thread finished'