diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/aiohttp_test.py b/test/aiohttp_test.py index c27eb7b..8849a59 100644 --- a/test/aiohttp_test.py +++ b/test/aiohttp_test.py @@ -1,33 +1,61 @@ -from multiprocessing import Process - import pytest -from common import kwargs_list -from common import mode_list -from common import send_requests +from aiohttp import web + +from swagger_ui import aiohttp_api_doc +from swagger_ui import api_doc +from .common import config_content +from .common import parametrize_list -def server_process(port, mode, **kwargs): - from aiohttp import web +@pytest.fixture +def app(): async def hello(request): return web.Response(text="Hello, world") app = web.Application() app.add_routes([web.get('/hello/world', hello)]) + return app + + +@pytest.mark.asyncio +@pytest.mark.parametrize('mode, kwargs', parametrize_list) +async def test_aiohttp(app, aiohttp_client, mode, kwargs): + if kwargs.get('config_rel_url'): + async def swagger_config_handler(request): + return web.Response(text=config_content) + app.add_routes([web.get(kwargs['config_rel_url'], swagger_config_handler)]) if mode == 'auto': - from swagger_ui import api_doc api_doc(app, **kwargs) else: - from swagger_ui import aiohttp_api_doc aiohttp_api_doc(app, **kwargs) - web.run_app(app, host='localhost', port=port) + url_prefix = kwargs['url_prefix'] + if url_prefix.endswith('/'): + url_prefix = url_prefix[:-1] + + client = await aiohttp_client(app) + + resp = await client.get('/hello/world') + assert resp.status == 200, await resp.text() -@pytest.mark.parametrize('mode', mode_list) -@pytest.mark.parametrize('kwargs', kwargs_list) -def test_aiohttp(port, mode, kwargs): - proc = Process(target=server_process, args=(port, mode), kwargs=kwargs) - proc.start() - send_requests(port, mode, kwargs) - proc.terminate() + resp = await client.get(url_prefix) + assert resp.status == 200, await resp.text() + + resp = await client.get(f'{url_prefix}/static/LICENSE') + assert resp.status == 200, await resp.text() + + if kwargs.get('editor'): + resp = await client.get(f'{url_prefix}/editor') + assert resp.status == 200, await resp.text() + else: + resp = await client.get(f'{url_prefix}/editor') + assert resp.status == 404, await resp.text() + + if kwargs.get('config_rel_url'): + resp = await client.get(kwargs['config_rel_url']) + assert resp.status == 200, await resp.text() + else: + resp = await client.get(f'{url_prefix}/swagger.json') + assert resp.status == 200, await resp.text() diff --git a/test/bottle_test.py b/test/bottle_test.py index 9aa60de..9014385 100644 --- a/test/bottle_test.py +++ b/test/bottle_test.py @@ -1,34 +1,64 @@ -from multiprocessing import Process - import pytest -from common import kwargs_list -from common import mode_list -from common import send_requests +from bottle import Bottle +from webtest import AppError +from webtest import TestApp + +from swagger_ui import api_doc +from swagger_ui import bottle_api_doc +from .common import config_content +from .common import parametrize_list -def server_process(port, mode, **kwargs): - from bottle import Bottle - from bottle import run +@pytest.fixture +def app(): app = Bottle() @app.route('/hello/world') def hello(): return 'Hello World!!!' + return app + + +@pytest.mark.parametrize('mode, kwargs', parametrize_list) +def test_bottle(app, mode, kwargs): + if kwargs.get('config_rel_url'): + @app.route(kwargs['config_rel_url']) + def swagger_config_handler(): + return config_content if mode == 'auto': - from swagger_ui import api_doc api_doc(app, **kwargs) else: - from swagger_ui import bottle_api_doc bottle_api_doc(app, **kwargs) - run(app, host='localhost', port=port) + url_prefix = kwargs['url_prefix'] + if url_prefix.endswith('/'): + url_prefix = url_prefix[:-1] + + client = TestApp(app) + + resp = client.get('/hello/world') + assert resp.status_code == 200, resp.data -@pytest.mark.parametrize('mode', mode_list) -@pytest.mark.parametrize('kwargs', kwargs_list) -def test_bottle(port, mode, kwargs): - proc = Process(target=server_process, args=(port, mode), kwargs=kwargs) - proc.start() - send_requests(port, mode, kwargs) - proc.terminate() + resp = client.get(url_prefix) + assert resp.status_code == 200, resp.data + + resp = client.get(f'{url_prefix}/static/LICENSE') + assert resp.status_code == 200, resp.data + + if kwargs.get('editor'): + resp = client.get(f'{url_prefix}/editor') + assert resp.status_code == 200, resp.data + else: + try: + resp = client.get(f'{url_prefix}/editor') + except AppError as ex: + assert '404 Not Found' in str(ex), str(ex) + + if kwargs.get('config_rel_url'): + resp = client.get(kwargs['config_rel_url']) + assert resp.status_code == 200, resp.data + else: + resp = client.get(f'{url_prefix}/swagger.json') + assert resp.status_code == 200, resp.data diff --git a/test/chalice_test.py b/test/chalice_test.py index 7a322f4..28c13a9 100644 --- a/test/chalice_test.py +++ b/test/chalice_test.py @@ -1,40 +1,79 @@ -from multiprocessing import Process - import pytest -from common import kwargs_list -from common import mode_list -from common import send_requests +from chalice import Chalice +from chalice.config import Config +from chalice.local import ForbiddenError +from chalice.local import LocalGateway + +from swagger_ui import api_doc +from swagger_ui import chalice_api_doc +from .common import config_content +from .common import parametrize_list -def server_process(port, mode, **kwargs): - from chalice import Chalice - from chalice.config import Config - from chalice.local import create_local_server +@pytest.fixture +def app(): app = Chalice(__name__) @app.route('/hello/world') - def index(): - return {'hello': 'world'} + def hello(): + return 'Hello World!!!' + return app + + +@pytest.mark.parametrize('mode, kwargs', parametrize_list) +def test_chalice(app, mode, kwargs): + if kwargs.get('config_rel_url'): + @app.route(kwargs['config_rel_url']) + def swagger_config_handler(): + return config_content if mode == 'auto': - from swagger_ui import api_doc api_doc(app, **kwargs) else: - from swagger_ui import chalice_api_doc chalice_api_doc(app, **kwargs) - config = Config() - create_local_server(app, config, 'localhost', port).serve_forever() + url_prefix = kwargs['url_prefix'] + if url_prefix.endswith('/'): + url_prefix = url_prefix[:-1] + + client = LocalGateway(app, config=Config()) + headers = {'Host': 'localhost'} -@pytest.mark.parametrize('mode', mode_list) -@pytest.mark.parametrize('kwargs', kwargs_list) -def test_chalice(port, mode, kwargs): - if kwargs['url_prefix'] in ('/', ''): - return + resp = client.handle_request( + method='GET', path='/hello/world', headers=headers, body=None) + assert resp['statusCode'] == 200, resp['body'] - proc = Process(target=server_process, args=(port, mode), kwargs=kwargs) - proc.start() - send_requests(port, mode, kwargs) - proc.terminate() + resp = client.handle_request( + method='GET', path=url_prefix, headers=headers, body=None) + assert resp['statusCode'] == 200, resp['body'] + + resp = client.handle_request( + method='GET', path=f'{url_prefix}/static/LICENSE', + headers=headers, body=None) + assert resp['statusCode'] == 200, resp['body'] + + if kwargs.get('editor'): + resp = client.handle_request( + method='GET', path=f'{url_prefix}/editor', + headers=headers, body=None) + assert resp['statusCode'] == 200, resp['body'] + else: + try: + resp = client.handle_request( + method='GET', path=f'{url_prefix}/editor', + headers=headers, body=None) + except ForbiddenError as ex: + assert "Missing Authentication Token" in str(ex), str(ex) + + if kwargs.get('config_rel_url'): + resp = client.handle_request( + method='GET', path=kwargs['config_rel_url'], + headers=headers, body=None) + assert resp['statusCode'] == 200, resp['body'] + else: + resp = client.handle_request( + method='GET', path=f'{url_prefix}/swagger.json', + headers=headers, body=None) + assert resp['statusCode'] == 200, resp['body'] diff --git a/test/common.py b/test/common.py index 2dba0ef..9584840 100644 --- a/test/common.py +++ b/test/common.py @@ -1,12 +1,10 @@ -import socket -import time +from itertools import product from pathlib import Path -import requests - cur_dir = Path(__file__).resolve().parent config_path = str(cur_dir.joinpath('conf/test3.yaml')) +config_content = Path(config_path).read_text() mode_list = ['auto', None] @@ -35,48 +33,4 @@ }, ] - -def detect_port(port): - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - try: - s.connect(('localhost', int(port))) - s.shutdown(2) - return True - except Exception: - return False - - -def wait_port_listen(port): - counter = 0 - while counter <= 50: - if detect_port(port): - return True - time.sleep(0.1) - counter += 1 - return False - - -def send_requests(port, mode, kwargs): - assert wait_port_listen(port), 'port: {} not listen!'.format(port) - - url_prefix = 'http://localhost:{}{}'.format(port, kwargs['url_prefix']) - if url_prefix.endswith('/'): - url_prefix = url_prefix[:-1] - server_url = 'http://localhost:{}/hello/world'.format(port) - - # Step 1: test server - assert requests.get(server_url).status_code == 200 - - # Step 2: test root - assert requests.get(url_prefix).status_code == 200 - assert requests.get(url_prefix + '/').status_code == 200 - - # Step 3: test static file - assert requests.get(url_prefix + '/static/LICENSE').status_code == 200 - - # Step 4: test editor - if kwargs.get('editor', False): - assert requests.get(url_prefix + '/editor').status_code == 200 - - # Step 5: test swagger.json - assert requests.get(url_prefix + '/swagger.json').status_code == 200 +parametrize_list = list(product(mode_list, kwargs_list)) diff --git a/test/conftest.py b/test/conftest.py index 775fe3c..e69de29 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,12 +0,0 @@ -import socket - -import pytest - - -@pytest.fixture(scope='function') -def port(): - sock = socket.socket() - sock.bind(('', 0)) - ip, port = sock.getsockname() - sock.close() - return port diff --git a/test/falcon_test.py b/test/falcon_test.py index 0e4a8f7..b9618ac 100644 --- a/test/falcon_test.py +++ b/test/falcon_test.py @@ -1,53 +1,71 @@ -from multiprocessing import Process +from distutils.version import StrictVersion +import falcon import pytest -from common import kwargs_list -from common import mode_list -from common import send_requests +from falcon import testing +from swagger_ui import api_doc +from swagger_ui import falcon_api_doc -def server_process(port, mode, **kwargs): - import json - from distutils.version import StrictVersion - from wsgiref import simple_server +from .common import config_content +from .common import parametrize_list - import falcon - class HelloWorldResource(object): +@pytest.fixture +def app(): + class HelloWorldHandler(object): def on_get(self, req, resp): - resp.body = json.dumps({'text': 'Hello World!!!'}) + resp.body = 'Hello World!!!' if StrictVersion(falcon.__version__) < StrictVersion('3.0.0'): app = falcon.API() else: app = falcon.App() - app.add_route('/hello/world', HelloWorldResource()) + app.add_route('/hello/world', HelloWorldHandler()) + return app + + +@pytest.mark.parametrize('mode, kwargs', parametrize_list) +def test_falcon(app, mode, kwargs): + if kwargs['url_prefix'] in ('', '/'): + return + + if kwargs.get('config_rel_url'): + class SwaggerConfigHandler(object): + def on_get(self, req, resp): + resp.body = config_content + app.add_route(kwargs['config_rel_url'], SwaggerConfigHandler()) if mode == 'auto': - from swagger_ui import api_doc api_doc(app, **kwargs) else: - from swagger_ui import falcon_api_doc falcon_api_doc(app, **kwargs) - httpd = simple_server.make_server('localhost', port, app) - httpd.serve_forever() + url_prefix = kwargs['url_prefix'] + if url_prefix.endswith('/'): + url_prefix = url_prefix[:-1] + client = testing.TestClient(app) -@pytest.mark.parametrize('mode', mode_list) -@pytest.mark.parametrize('kwargs', kwargs_list) -def test_falcon(port, mode, kwargs): - if kwargs['url_prefix'] in ('', '/'): - return + resp = client.get('/hello/world') + assert resp.status_code == 200, resp.text - proc = Process(target=server_process, args=(port, mode), kwargs=kwargs) - proc.start() - send_requests(port, mode, kwargs) - proc.terminate() + resp = client.get(url_prefix) + assert resp.status_code == 200, resp.text + resp = client.get(f'{url_prefix}/static/LICENSE') + assert resp.status_code == 200, resp.text -if __name__ == '__main__': - kwargs = dict(url_prefix='/api/doc', config_path='conf/test3.yaml') - # server_process(8090, 'auto', **kwargs) - server_process(8090, None, **kwargs) + resp = client.get(f'{url_prefix}/editor') + if kwargs.get('editor'): + assert resp.status_code == 200, resp.text + else: + assert resp.status_code == 404, resp.text + + if kwargs.get('config_rel_url'): + resp = client.get(kwargs['config_rel_url']) + assert resp.status_code == 200, resp.text + else: + resp = client.get(f'{url_prefix}/swagger.json') + assert resp.status_code == 200, resp.text diff --git a/test/flask_test.py b/test/flask_test.py index b197800..4e3759d 100644 --- a/test/flask_test.py +++ b/test/flask_test.py @@ -1,37 +1,62 @@ -from multiprocessing import Process - import pytest -from common import kwargs_list -from common import mode_list -from common import send_requests +from flask import Flask + +from swagger_ui import api_doc +from swagger_ui import flask_api_doc +from .common import config_content +from .common import parametrize_list -def server_process(port, mode, **kwargs): - from flask import Flask +@pytest.fixture +def app(): app = Flask(__name__) @app.route(r'/hello/world') def hello(): return 'Hello World!!!' + return app + + +@pytest.mark.parametrize('mode, kwargs', parametrize_list) +def test_flask(app, mode, kwargs): + if kwargs['url_prefix'] in ('/', ''): + return + + if kwargs.get('config_rel_url'): + @app.route(kwargs['config_rel_url']) + def swagger_config(): + return config_content if mode == 'auto': - from swagger_ui import api_doc api_doc(app, **kwargs) else: - from swagger_ui import flask_api_doc flask_api_doc(app, **kwargs) - app.run(host='localhost', port=port) + url_prefix = kwargs['url_prefix'] + if url_prefix.endswith('/'): + url_prefix = url_prefix[:-1] + client = app.test_client() -@pytest.mark.parametrize('mode', mode_list) -@pytest.mark.parametrize('kwargs', kwargs_list) -def test_flask(port, mode, kwargs): - if kwargs['url_prefix'] in ('/', ''): - return + resp = client.get('/hello/world') + assert resp.status_code == 200, resp.data + + resp = client.get(url_prefix) + assert resp.status_code == 200, resp.data + + resp = client.get(f'{url_prefix}/static/LICENSE') + assert resp.status_code == 200, resp.data - proc = Process(target=server_process, args=(port, mode), kwargs=kwargs) - proc.start() - send_requests(port, mode, kwargs) - proc.terminate() + resp = client.get(f'{url_prefix}/editor') + if kwargs.get('editor'): + assert resp.status_code == 200, resp.data + else: + assert resp.status_code == 404, resp.data + + if kwargs.get('config_rel_url'): + resp = client.get(kwargs['config_rel_url']) + assert resp.status_code == 200, resp.data + else: + resp = client.get(f'{url_prefix}/swagger.json') + assert resp.status_code == 200, resp.data diff --git a/test/quart_test.py b/test/quart_test.py index ee8bdb3..86be6fc 100644 --- a/test/quart_test.py +++ b/test/quart_test.py @@ -1,36 +1,63 @@ -from multiprocessing import Process - import pytest -from common import kwargs_list -from common import mode_list -from common import send_requests +from quart import Quart + +from swagger_ui import api_doc +from swagger_ui import quart_api_doc +from .common import config_content +from .common import parametrize_list -def server_process(port, mode, **kwargs): - from quart import Quart +@pytest.fixture +def app(): app = Quart(__name__) @app.route(r'/hello/world', methods=['GET']) - async def index_handler(): + async def hello(): return 'Hello World!!!' + return app + + +@pytest.mark.asyncio +@pytest.mark.parametrize('mode, kwargs', parametrize_list) +async def test_quart(app, mode, kwargs): + if kwargs['url_prefix'] in ('/', ''): + return + + if kwargs.get('config_rel_url'): + @app.route(kwargs['config_rel_url'], methods=['GET']) + def swagger_config(): + return config_content if mode == 'auto': - from swagger_ui import api_doc api_doc(app, **kwargs) else: - from swagger_ui import quart_api_doc quart_api_doc(app, **kwargs) - app.run(host='localhost', port=port) + url_prefix = kwargs['url_prefix'] + if url_prefix.endswith('/'): + url_prefix = url_prefix[:-1] -@pytest.mark.parametrize('mode', mode_list) -@pytest.mark.parametrize('kwargs', kwargs_list) -def test_quart(port, mode, kwargs): - if kwargs['url_prefix'] in ('/', ''): - return + client = app.test_client() + + resp = await client.get('/hello/world', follow_redirects=True) + assert resp.status_code == 200, await resp.get_data() + + resp = await client.get(url_prefix, follow_redirects=True) + assert resp.status_code == 200, await resp.get_data() + + resp = await client.get(f'{url_prefix}/static/LICENSE', follow_redirects=True) + assert resp.status_code == 200, await resp.get_data() - proc = Process(target=server_process, args=(port, mode), kwargs=kwargs) - proc.start() - send_requests(port, mode, kwargs) - proc.terminate() + resp = await client.get(f'{url_prefix}/editor', follow_redirects=True) + if kwargs.get('editor'): + assert resp.status_code == 200, await resp.get_data() + else: + assert resp.status_code == 404, await resp.get_data() + + if kwargs.get('config_rel_url'): + resp = await client.get(kwargs['config_rel_url'], follow_redirects=True) + assert resp.status_code == 200, await resp.get_data() + else: + resp = await client.get(f'{url_prefix}/swagger.json', follow_redirects=True) + assert resp.status_code == 200, await resp.get_data() diff --git a/test/sanic_test.py b/test/sanic_test.py index a3f7805..eb92687 100644 --- a/test/sanic_test.py +++ b/test/sanic_test.py @@ -1,66 +1,61 @@ -from pathlib import Path - import pytest -from common import kwargs_list -from common import mode_list from sanic import Sanic from sanic import response from sanic_testing.testing import SanicTestClient +from swagger_ui import api_doc +from swagger_ui import sanic_api_doc -@pytest.fixture(scope="module") -def app(): - app = Sanic("test_sanic") - return app +from .common import config_content +from .common import parametrize_list -@pytest.mark.parametrize('mode', mode_list) -@pytest.mark.parametrize('kwargs', kwargs_list) -def test_sanic(app, port, mode, kwargs): - # app = Sanic(f"server_{port}") +@pytest.fixture +def app(): + app = Sanic('test_sanic') @app.get(r'/hello/world') async def index_handler(request): return response.text('Hello World!!!') + return app - if kwargs["config_rel_url"]: - @app.get(kwargs["config_rel_url"]) + +@pytest.mark.parametrize('mode, kwargs', parametrize_list) +def test_sanic(app, mode, kwargs): + if kwargs.get('config_rel_url'): + @app.get(kwargs['config_rel_url']) async def swagger_config_handler(request): - return response.json(Path(kwargs["config_path"]).read_text()) + return response.json(config_content) if mode == 'auto': - from swagger_ui import api_doc api_doc(app, **kwargs) else: - from swagger_ui import sanic_api_doc sanic_api_doc(app, **kwargs) - url_prefix = kwargs["url_prefix"] - if url_prefix.endswith("/"): + url_prefix = kwargs['url_prefix'] + if url_prefix.endswith('/'): url_prefix = url_prefix[:-1] client = SanicTestClient(app) - _, resp = client.get("/hello/world") - # print(resp.text) + _, resp = client.get('/hello/world') assert resp.status == 200, resp.text _, resp = client.get(url_prefix) - # print(resp.text) assert resp.status == 200, resp.text - _, resp = client.get(f"{url_prefix}/static/LICENSE") - # print(resp.text) + _, resp = client.get(f'{url_prefix}/static/LICENSE') assert resp.status == 200, resp.text - if kwargs.get("editor"): - _, resp = client.get(f"{url_prefix}/editor") - # print(resp.text) + if kwargs.get('editor'): + _, resp = client.get(f'{url_prefix}/editor') assert resp.status == 200, resp.text else: - _, resp = client.get(f"{url_prefix}/editor") - # print(resp.text) + _, resp = client.get(f'{url_prefix}/editor') assert resp.status == 404, resp.text - _, resp = client.get(f"{url_prefix}/swagger.json") - # print(resp.text) - assert resp.status == 200, resp.text + if kwargs.get('config_rel_url'): + _, resp = client.get(kwargs['config_rel_url']) + assert resp.status == 200, resp.text + else: + _, resp = client.get(f'{url_prefix}/swagger.json') + assert resp.status == 200, resp.text diff --git a/test/starlette_test.py b/test/starlette_test.py index c6d78e1..4220103 100644 --- a/test/starlette_test.py +++ b/test/starlette_test.py @@ -1,43 +1,65 @@ -from multiprocessing import Process - import pytest -from common import kwargs_list -from common import mode_list -from common import send_requests +from starlette.applications import Starlette +from starlette.responses import PlainTextResponse +from starlette.testclient import TestClient + +from swagger_ui import api_doc +from swagger_ui import starlette_api_doc + +from .common import config_content +from .common import parametrize_list -def server_process(port, mode, **kwargs): - import uvicorn - from starlette.applications import Starlette - from starlette.responses import PlainTextResponse - from starlette.routing import Route +@pytest.fixture +def app(): + app = Starlette() + @app.route('/hello/world') def hello_world(request): return PlainTextResponse('Hello World!!!') + return app - def startup(): - if mode == 'auto': - from swagger_ui import api_doc - api_doc(app, **kwargs) - else: - from swagger_ui import starlette_api_doc - starlette_api_doc(app, **kwargs) - routes = [ - Route('/hello/world', hello_world), - ] +@pytest.mark.parametrize('mode, kwargs', parametrize_list) +def test_starlette(app, mode, kwargs): + if kwargs['url_prefix'] in ('/', ''): + return - app = Starlette(debug=True, routes=routes, on_startup=[startup]) - uvicorn.run(app, host="localhost", port=port, log_level="info") + if kwargs.get('config_rel_url'): + @app.route(kwargs['config_rel_url']) + def swagger_config(request): + return PlainTextResponse(config_content) + if mode == 'auto': + api_doc(app, **kwargs) + else: + starlette_api_doc(app, **kwargs) -@pytest.mark.parametrize('mode', mode_list) -@pytest.mark.parametrize('kwargs', kwargs_list) -def test_starlette(port, mode, kwargs): - if kwargs['url_prefix'] in ('/', ''): - return + url_prefix = kwargs['url_prefix'] + if url_prefix.endswith('/'): + url_prefix = url_prefix[:-1] + + client = TestClient(app) + + resp = client.get('/hello/world') + assert resp.status_code == 200, resp.text + + resp = client.get(url_prefix) + assert resp.status_code == 200, resp.text + + resp = client.get(f'{url_prefix}/static/LICENSE') + assert resp.status_code == 200, resp.text + + if kwargs.get('editor'): + resp = client.get(f'{url_prefix}/editor') + assert resp.status_code == 200, resp.text + else: + resp = client.get(f'{url_prefix}/editor') + assert resp.status_code == 404, resp.text - proc = Process(target=server_process, args=(port, mode), kwargs=kwargs) - proc.start() - send_requests(port, mode, kwargs) - proc.terminate() + if kwargs.get('config_rel_url'): + resp = client.get(kwargs['config_rel_url']) + assert resp.status_code == 200, resp.text + else: + resp = client.get(f'{url_prefix}/swagger.json') + assert resp.status_code == 200, resp.text diff --git a/test/tornado_test.py b/test/tornado_test.py index 360eba1..f22ba0e 100644 --- a/test/tornado_test.py +++ b/test/tornado_test.py @@ -1,15 +1,18 @@ -from multiprocessing import Process - import pytest -from common import kwargs_list -from common import mode_list -from common import send_requests +import tornado.httpclient +import tornado.httpserver +import tornado.ioloop +import tornado.web + +from swagger_ui import api_doc +from swagger_ui import tornado_api_doc +from .common import config_content +from .common import parametrize_list -def server_process(port, mode, **kwargs): - import tornado.ioloop - import tornado.web +@pytest.fixture +def app(): class HelloWorldHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): return self.write('Hello World!!!') @@ -17,22 +20,57 @@ def get(self, *args, **kwargs): app = tornado.web.Application([ (r'/hello/world', HelloWorldHandler), ]) + return app + + +@pytest.mark.asyncio +@pytest.mark.parametrize('mode, kwargs', parametrize_list) +async def test_tornado(app, mode, kwargs): + if kwargs.get('config_rel_url'): + class SwaggerConfigHandler(tornado.web.RequestHandler): + def get(self, *args, **kwargs): + return self.write(config_content) + app.add_handlers('.*', [(kwargs['config_rel_url'], SwaggerConfigHandler)]) if mode == 'auto': - from swagger_ui import api_doc api_doc(app, **kwargs) else: - from swagger_ui import tornado_api_doc tornado_api_doc(app, **kwargs) - app.listen(address='localhost', port=port) - tornado.ioloop.IOLoop.current().start() + server = tornado.httpserver.HTTPServer(app) + server.listen(0) + + host, port = list(server._sockets.values())[0].getsockname() + server_addr = f'http://{host}:{port}' + + url_prefix = kwargs['url_prefix'] + if url_prefix.endswith('/'): + url_prefix = url_prefix[:-1] + url_prefix = f'{server_addr}{url_prefix}' + http_client = tornado.httpclient.AsyncHTTPClient() -@pytest.mark.parametrize('mode', mode_list) -@pytest.mark.parametrize('kwargs', kwargs_list) -def test_tornado(port, mode, kwargs): - proc = Process(target=server_process, args=(port, mode), kwargs=kwargs) - proc.start() - send_requests(port, mode, kwargs) - proc.terminate() + resp = await http_client.fetch(f'{server_addr}/hello/world') + assert resp.code == 200, resp.body + + resp = await http_client.fetch(url_prefix) + assert resp.code == 200, resp.body + + resp = await http_client.fetch(f'{url_prefix}/static/LICENSE') + assert resp.code == 200, resp.body + + if kwargs.get('editor'): + resp = await http_client.fetch(f'{url_prefix}/editor') + assert resp.code == 200, resp.body + else: + try: + resp = await http_client.fetch(f'{url_prefix}/editor') + except tornado.httpclient.HTTPClientError as e: + assert e.code == 404, e.response.body + + if kwargs.get('config_rel_url'): + resp = await http_client.fetch(server_addr + kwargs['config_rel_url']) + assert resp.code == 200, resp.body + else: + resp = await http_client.fetch(f'{url_prefix}/swagger.json') + assert resp.code == 200, resp.body