Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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')
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"
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
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
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"}}
def test_bad_input_data():
app = BadInputData(debug=True)
client = TestClient(app)
response = client.get("/input-format/")
assert response.status_code == 500
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"
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}
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(");")