Пример #1
0
from apistar.frameworks.asyncio import ASyncIOApp as App
from apistar.backends import sqlalchemy_backend

from example.routes import routes
from example.settings import settings

app = App(routes=routes,
          settings=settings,
          components=sqlalchemy_backend.components,
          commands=sqlalchemy_backend.commands)

if __name__ == '__main__':
    app.main()
Пример #2
0
        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
    }


@pytest.mark.parametrize('client', [wsgi_client, async_client])
def test_authenticated_request(client):
Пример #3
0
    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):
    response = client.get('/404/')
Пример #4
0
from apistar import Include, Route
from apistar.frameworks.asyncio import ASyncIOApp as App
from apistar.handlers import docs_urls, static_urls


async def welcome(name=None):
    return 'Hello, world'


routes = [
    Route('/', 'GET', welcome),
    Include('/docs', docs_urls),
    Include('/static', static_urls)
]

app = App(routes=routes)


if __name__ == '__main__':
    app.main()