How to use starlette - 10 common examples

To help you get started, we’ve selected a few starlette examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github python-gino / gino / tests / test_starlette.py View on Github external
def _test(app):
    client = TestClient(app)
    with client:
        for method in '01234':
            response = client.get('/users/1?method=' + method)
            assert response.status_code == 404

        response = client.post('/users', json=dict(name='fantix'))
        assert response.status_code == 200
        assert response.json() == dict(id=1, nickname='fantix')

        for method in '01234':
            response = client.get('/users/1?method=' + method)
            assert response.status_code == 200
            assert response.json() == dict(id=1, nickname='fantix')
github encode / starlette / tests / test_responses.py View on Github external
def test_populate_headers():
    app = Response(content="hi", headers={}, media_type="text/html")
    client = TestClient(app)
    response = client.get("/")
    assert response.text == "hi"
    assert response.headers["content-length"] == "2"
    assert response.headers["content-type"] == "text/html; charset=utf-8"
github laactech / foxcross / tests / test_pandas_serving.py View on Github external
def test_single_model_html_responses(endpoint):
    app = InterpolateMultiFrameModelServing(debug=True)
    client = TestClient(app)
    response = client.get(endpoint, headers={"Accept": MediaTypes.HTML.value})
    assert response.status_code == 200
github encode / starlette / tests / test_applications.py View on Github external
app = Starlette()

    def run_startup():
        nonlocal startup_complete
        startup_complete = True

    def run_cleanup():
        nonlocal cleanup_complete
        cleanup_complete = True

    app.add_event_handler("startup", run_startup)
    app.add_event_handler("shutdown", run_cleanup)

    assert not startup_complete
    assert not cleanup_complete
    with TestClient(app):
        assert startup_complete
        assert not cleanup_complete
    assert startup_complete
    assert cleanup_complete
github encode / starlette / tests / test_graphql.py View on Github external
def test_graphql_async_old_style_executor():
    # See https://github.com/encode/starlette/issues/242
    client = TestClient(old_style_async_app)
    response = client.get("/?query={ hello }")
    assert response.status_code == 200
    assert response.json() == {"data": {"hello": "Hello stranger"}}
github laactech / foxcross / tests / test_failed_serving.py View on Github external
def test_bad_input_data():
    app = BadInputData(debug=True)
    client = TestClient(app)
    response = client.get("/input-format/")
    assert response.status_code == 500
github encode / starlette / tests / test_staticfiles.py View on Github external
def test_staticfiles_with_package():
    app = StaticFiles(packages=["tests"])
    client = TestClient(app)
    response = client.get("/example.txt")
    assert response.status_code == 200
    assert response.text == "123\n"
github encode / starlette / tests / test_requests.py View on Github external
def test_request_client():
    async def app(scope, receive, send):
        request = Request(scope, receive)
        response = JSONResponse(
            {"host": request.client.host, "port": request.client.port}
        )
        await response(scope, receive, send)

    client = TestClient(app)
    response = client.get("/")
    assert response.json() == {"host": "testclient", "port": 50000}
github drkane / find-that-charity / tests / test_reconcile.py View on Github external
def test_propose_properties_callback():
    client = TestClient(app)
    response = client.get('/reconcile/propose_properties?callback=test_func')
    assert response.status_code == 200
    result = response.text
    assert result.startswith("test_func(")
    assert result.endswith(");")
github encode / apistar / tests / test_cli.py View on Github external
def homepage(request):
    return JSONResponse({"hello": "world"})