def login(): routes = [ Route('/', 'GET', welcome), Include('/docs', docs_urls), Include('/static', static_urls) ] app = App(routes=routes) if __name__ == '__main__': app.main()
def test_misconfigured_routes(): def view(): raise NotImplementedError with pytest.raises(exceptions.ConfigurationError): WSGIApp( routes=[Route('/', 'GET', view), Route('/another', 'POST', view)])
def __init__(self): routes = import_module(settings.APISTAR_ROUTE_CONF).routes if settings.DEBUG: routes.append(Include('/static', static_urls)) routes.append(Include('/docs', docs_urls)) self.django_wsgi_app = get_wsgi_application() self.apistar_wsgi_app = WSGIApp(routes=routes, settings=settings.APISTAR_SETTINGS)
from api import routes def welcome(name=None): if name is None: return {'message': 'Welcome to db Star!'} return {'message': 'Welcome to db Star, %s!' % name} routes = routes + \ [ Include('/docs', docs_urls), Include('/static', static_urls) ] # Configure database settings settings = { "DATABASE": { "URL": "sqlite:///Test.db", "METADATA": Base.metadata } } app = App(routes=routes, settings=settings, commands=sqlalchemy_backend.commands, components=sqlalchemy_backend.components) if __name__ == '__main__': app.main()
def test_misconfigured_type_on_route(): def set_type(var: set): raise NotImplementedError with pytest.raises(exceptions.ConfigurationError): WSGIApp(routes=[Route('/{var}/', 'GET', set_type)])
Route('/path_params/{var}/', 'GET', path_params), Route('/path_param/{var}/', 'GET', path_param), Route('/int/{var}/', 'GET', path_param_with_int), Route('/full_path/{var}', 'GET', path_param_with_full_path), Route('/max_length/{var}/', 'GET', path_param_with_max_length), Route('/number/{var}/', 'GET', path_param_with_number), Route('/integer/{var}/', 'GET', path_param_with_integer), Route('/string/{var}/', 'GET', path_param_with_string), Include('/subpath', [ Route('/{var}/', 'GET', subpath), ], namespace='included'), Route('/x', 'GET', lambda: 1, name='x'), Include('/another_subpath', [Route('/x', 'GET', lambda: 1, name='x2')]), ] wsgi_app = WSGIApp(routes=routes) async_app = ASyncIOApp(routes=routes) wsgi_client = TestClient(wsgi_app) async_client = TestClient(async_app) @pytest.mark.parametrize('client', [wsgi_client, async_client]) def test_200(client): response = client.get('/found/') assert response.status_code == 200 assert response.json() == {'message': 'Found'} @pytest.mark.parametrize('client', [wsgi_client, async_client]) def test_404(client):
return None scheme, token = authorization.split() if scheme.lower() != 'basic': return None username, password = base64.b64decode(token).decode('utf-8').split(':') return Authenticated(username) routes = [ Route('/auth/', 'GET', get_auth), ] settings = {'AUTHENTICATION': [BasicAuthentication()]} wsgi_app = WSGIApp(routes=routes, settings=settings) wsgi_client = TestClient(wsgi_app) async_app = ASyncIOApp(routes=routes, settings=settings) async_client = TestClient(async_app) @pytest.mark.parametrize('client', [wsgi_client, async_client]) def test_unauthenticated_request(client): response = client.get('http://example.com/auth/') assert response.json() == { 'display_name': None, 'user_id': None, 'is_authenticated': False }
from apistar import Route, Include from apistar.frameworks.wsgi import WSGIApp as App from apistar.handlers import docs_urls from view.user import user_auth, user_join def ping(): ''' :return: dict ''' return {"status": "ok"} routes = [ Route('/ping', 'GET', ping), # 認証 Route('/user/auth', "GET", user_auth), Route('/user/join', "POST", user_join), Include('/docs', docs_urls), ] app = App(routes=routes) # Install custom components.) if __name__ == "__main__": app.main(["run"])