Exemplo n.º 1
0
def serve_documentation(app: App):
    template_name = 'webtypes/docs/index.html'
    code_style = None  # pygments_css('emacs')
    return app.render_template(template_name,
                               document=app.document,
                               langs=['javascript', 'python'],
                               code_style=code_style)
Exemplo n.º 2
0
def test_on_error_loads_the_same_component_instances():
    # Given that I have an app that uses an AComponent and the AEventHooks
    app = App(
        components=[AComponent()],
        event_hooks=[AEventHooks],
        routes=[Route('/uses-a', method='GET', handler=uses_a)],
    )

    # When I call an endpoint that raises an error
    with pytest.raises(RuntimeError):
        test.TestClient(app).get('/uses-a')

    # Then all the instances of A are the same
    assert len(A_INSTANCES) >= 2
    for i in range(1, len(A_INSTANCES)):
        assert A_INSTANCES[i] is A_INSTANCES[i - 1]
Exemplo n.º 3
0
    raise RuntimeError('something bad happened')


def error():
    assert 1 == 2


routes = [
    Route('/hello', method='GET', handler=hello_world),
    Route('/error', method='GET', handler=error),
    Route('/uses-a', method='GET', handler=uses_a),
]

event_hooks = [CustomResponseHeader]

app = App(routes=routes, event_hooks=event_hooks)

client = test.TestClient(app)


def test_on_response():
    response = client.get('/hello')
    assert response.status_code == 200
    assert response.headers['Custom'] == 'Ran hooks'


def test_on_error():
    global ON_ERROR

    ON_ERROR = None
    with pytest.raises(AssertionError):
Exemplo n.º 4
0
async def serve_static_asgi(app: App, scope: ASGIScope, receive: ASGIReceive,
                            send: ASGISend):
    instance = app.statics(scope)
    await instance(receive, send)
Exemplo n.º 5
0
def serve_static_wsgi(app: App, environ: WSGIEnviron,
                      start_response: WSGIStartResponse):
    return app.statics(environ, start_response)
Exemplo n.º 6
0

def post_endpoint(user: User):
    raise NotImplementedError()


routes = [
    Route(url='/get-endpoint/', method='GET', handler=get_endpoint),
    Route(url='/get-endpoint-with-type/',
          method='GET',
          handler=get_endpoint_with_type),
    Route(url='/post-endpoint/', method='POST', handler=post_endpoint),
    Route(url='/schema/', method='GET', handler=serve_schema,
          documented=False),
]
app = App(routes=routes)
test_client = TestClient(app)

expected_schema = """{
    "openapi": "3.0.0",
    "info": {
        "title": "",
        "description": "",
        "version": ""
    },
    "paths": {
        "/get-endpoint/": {
            "get": {
                "description": "endpoint description",
                "operationId": "get_endpoint",
                "parameters": [
Exemplo n.º 7
0
from webtypes import App, ASyncApp, TestClient

async_app = ASyncApp(routes=[])
async_test_client = TestClient(async_app)

app = App(routes=[])
test_client = TestClient(app)


def test_docs():
    response = test_client.get('/docs/')
    assert response.status_code == 200


def test_docs_async():
    response = async_test_client.get('/docs/')
    assert response.status_code == 200