mirror of https://github.com/tiangolo/fastapi.git
✅ Improve test debugging (#1222)
This commit is contained in:
parent
483bce3ae1
commit
e9b189e9f2
|
|
@ -34,14 +34,14 @@ def test_create_user():
|
|||
"/users/",
|
||||
json={"email": "deadpool@example.com", "password": "chimichangas4life"},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert data["email"] == "deadpool@example.com"
|
||||
assert "id" in data
|
||||
user_id = data["id"]
|
||||
|
||||
response = client.get(f"/users/{user_id}")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert data["email"] == "deadpool@example.com"
|
||||
assert data["id"] == user_id
|
||||
|
|
|
|||
|
|
@ -100,11 +100,11 @@ openapi_schema = {
|
|||
|
||||
def test_additional_properties_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_additional_properties_post():
|
||||
response = client.post("/foo", json={"items": {"foo": 1, "bar": 2}})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"foo": 1, "bar": 2}
|
||||
|
|
|
|||
|
|
@ -42,11 +42,11 @@ client = TestClient(app)
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_path_operation():
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"id": "foo"}
|
||||
|
|
|
|||
|
|
@ -96,5 +96,5 @@ client = TestClient(app)
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
|
|
|||
|
|
@ -81,5 +81,5 @@ client = TestClient(app)
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
|
|
|||
|
|
@ -113,5 +113,5 @@ client = TestClient(app)
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
|
|
|||
|
|
@ -89,23 +89,23 @@ client = TestClient(app)
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_a():
|
||||
response = client.get("/a")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == "a"
|
||||
|
||||
|
||||
def test_b():
|
||||
response = client.get("/b")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == "b"
|
||||
|
||||
|
||||
def test_c():
|
||||
response = client.get("/c")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == "c"
|
||||
|
|
|
|||
|
|
@ -1128,7 +1128,7 @@ def test_get_path(path, expected_status, expected_response):
|
|||
|
||||
def test_swagger_ui():
|
||||
response = client.get("/docs")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.headers["content-type"] == "text/html; charset=utf-8"
|
||||
assert "swagger-ui-dist" in response.text
|
||||
assert (
|
||||
|
|
@ -1139,13 +1139,13 @@ def test_swagger_ui():
|
|||
|
||||
def test_swagger_ui_oauth2_redirect():
|
||||
response = client.get("/docs/oauth2-redirect")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.headers["content-type"] == "text/html; charset=utf-8"
|
||||
assert "window.opener.swaggerUIRedirectOauth2" in response.text
|
||||
|
||||
|
||||
def test_redoc():
|
||||
response = client.get("/redoc")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.headers["content-type"] == "text/html; charset=utf-8"
|
||||
assert "redoc@next" in response.text
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ client = TestClient(app)
|
|||
|
||||
def test_swagger_ui():
|
||||
response = client.get("/docs")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.headers["content-type"] == "text/html; charset=utf-8"
|
||||
assert "swagger-ui-dist" in response.text
|
||||
print(client.base_url)
|
||||
|
|
@ -28,7 +28,7 @@ def test_swagger_ui():
|
|||
|
||||
def test_swagger_ui_oauth2_redirect():
|
||||
response = client.get(swagger_ui_oauth2_redirect_url)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.headers["content-type"] == "text/html; charset=utf-8"
|
||||
assert "window.opener.swaggerUIRedirectOauth2" in response.text
|
||||
|
||||
|
|
|
|||
|
|
@ -41,28 +41,28 @@ client = TestClient(app)
|
|||
def test_normal_counter():
|
||||
counter_holder["counter"] = 0
|
||||
response = client.get("/counter/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"counter": 1}
|
||||
response = client.get("/counter/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"counter": 2}
|
||||
|
||||
|
||||
def test_sub_counter():
|
||||
counter_holder["counter"] = 0
|
||||
response = client.get("/sub-counter/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"counter": 1, "subcounter": 1}
|
||||
response = client.get("/sub-counter/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"counter": 2, "subcounter": 2}
|
||||
|
||||
|
||||
def test_sub_counter_no_cache():
|
||||
counter_holder["counter"] = 0
|
||||
response = client.get("/sub-counter-no-cache/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"counter": 2, "subcounter": 1}
|
||||
response = client.get("/sub-counter-no-cache/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"counter": 4, "subcounter": 3}
|
||||
|
|
|
|||
|
|
@ -66,5 +66,5 @@ client = TestClient(app)
|
|||
)
|
||||
def test_class_dependency(route, value):
|
||||
response = client.get(route, params={"value": value})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == value
|
||||
|
|
|
|||
|
|
@ -206,7 +206,7 @@ client = TestClient(app)
|
|||
def test_async_state():
|
||||
assert state["/async"] == f"asyncgen not started"
|
||||
response = client.get("/async")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == f"asyncgen started"
|
||||
assert state["/async"] == f"asyncgen completed"
|
||||
|
||||
|
|
@ -214,7 +214,7 @@ def test_async_state():
|
|||
def test_sync_state():
|
||||
assert state["/sync"] == f"generator not started"
|
||||
response = client.get("/sync")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == f"generator started"
|
||||
assert state["/sync"] == f"generator completed"
|
||||
|
||||
|
|
@ -237,7 +237,7 @@ def test_sync_raise_other():
|
|||
|
||||
def test_async_raise():
|
||||
response = client.get("/async_raise")
|
||||
assert response.status_code == 500
|
||||
assert response.status_code == 500, response.text
|
||||
assert state["/async_raise"] == "asyncgen raise finalized"
|
||||
assert "/async_raise" in errors
|
||||
errors.clear()
|
||||
|
|
@ -272,7 +272,7 @@ def test_background_tasks():
|
|||
|
||||
def test_sync_raise():
|
||||
response = client.get("/sync_raise")
|
||||
assert response.status_code == 500
|
||||
assert response.status_code == 500, response.text
|
||||
assert state["/sync_raise"] == "generator raise finalized"
|
||||
assert "/sync_raise" in errors
|
||||
errors.clear()
|
||||
|
|
@ -280,14 +280,14 @@ def test_sync_raise():
|
|||
|
||||
def test_sync_async_state():
|
||||
response = client.get("/sync_async")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == f"asyncgen started"
|
||||
assert state["/async"] == f"asyncgen completed"
|
||||
|
||||
|
||||
def test_sync_sync_state():
|
||||
response = client.get("/sync_sync")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == f"generator started"
|
||||
assert state["/sync"] == f"generator completed"
|
||||
|
||||
|
|
@ -308,7 +308,7 @@ def test_sync_sync_raise_other():
|
|||
|
||||
def test_sync_async_raise():
|
||||
response = client.get("/sync_async_raise")
|
||||
assert response.status_code == 500
|
||||
assert response.status_code == 500, response.text
|
||||
assert state["/async_raise"] == "asyncgen raise finalized"
|
||||
assert "/async_raise" in errors
|
||||
errors.clear()
|
||||
|
|
@ -316,7 +316,7 @@ def test_sync_async_raise():
|
|||
|
||||
def test_sync_sync_raise():
|
||||
response = client.get("/sync_sync_raise")
|
||||
assert response.status_code == 500
|
||||
assert response.status_code == 500, response.text
|
||||
assert state["/sync_raise"] == "generator raise finalized"
|
||||
assert "/sync_raise" in errors
|
||||
errors.clear()
|
||||
|
|
|
|||
|
|
@ -190,13 +190,13 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_no_duplicates_invalid():
|
||||
response = client.post("/no-duplicates", json={"item": {"data": "myitem"}})
|
||||
assert response.status_code == 422
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
|
|
@ -213,19 +213,19 @@ def test_no_duplicates():
|
|||
"/no-duplicates",
|
||||
json={"item": {"data": "myitem"}, "item2": {"data": "myitem2"}},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [{"data": "myitem"}, {"data": "myitem2"}]
|
||||
|
||||
|
||||
def test_duplicates():
|
||||
response = client.post("/with-duplicates", json={"data": "myitem"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [{"data": "myitem"}, {"data": "myitem"}]
|
||||
|
||||
|
||||
def test_sub_duplicates():
|
||||
response = client.post("/with-duplicates-sub", json={"data": "myitem"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [
|
||||
{"data": "myitem"},
|
||||
[{"data": "myitem"}, {"data": "myitem"}],
|
||||
|
|
|
|||
|
|
@ -21,11 +21,11 @@ client = TestClient(app)
|
|||
def test_use_empty():
|
||||
with client:
|
||||
response = client.get("/prefix")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == ["OK"]
|
||||
|
||||
response = client.get("/prefix/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == ["OK"]
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -314,47 +314,47 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get_api_route():
|
||||
response = client.get("/items/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item_id": "foo"}
|
||||
|
||||
|
||||
def test_get_api_route_not_decorated():
|
||||
response = client.get("/items-not-decorated/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item_id": "foo"}
|
||||
|
||||
|
||||
def test_delete():
|
||||
response = client.delete("/items/foo", json={"name": "Foo"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item_id": "foo", "item": {"name": "Foo", "price": None}}
|
||||
|
||||
|
||||
def test_head():
|
||||
response = client.head("/items/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.headers["x-fastapi-item-id"] == "foo"
|
||||
|
||||
|
||||
def test_options():
|
||||
response = client.options("/items/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.headers["x-fastapi-item-id"] == "foo"
|
||||
|
||||
|
||||
def test_patch():
|
||||
response = client.patch("/items/foo", json={"name": "Foo"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item_id": "foo", "item": {"name": "Foo", "price": None}}
|
||||
|
||||
|
||||
def test_trace():
|
||||
response = client.request("trace", "/items/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.headers["content-type"] == "message/http"
|
||||
|
|
|
|||
|
|
@ -127,13 +127,13 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_filter_sub_model():
|
||||
response = client.get("/model/modelA")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {
|
||||
"name": "modelA",
|
||||
"description": "model-a-desc",
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ def test_python_list_param_as_form():
|
|||
response = client.post(
|
||||
"/form/python-list", data={"items": ["first", "second", "third"]}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == ["first", "second", "third"]
|
||||
|
||||
|
||||
|
|
@ -34,7 +34,7 @@ def test_python_set_param_as_form():
|
|||
response = client.post(
|
||||
"/form/python-set", data={"items": ["first", "second", "third"]}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert set(response.json()) == {"first", "second", "third"}
|
||||
|
||||
|
||||
|
|
@ -42,5 +42,5 @@ def test_python_tuple_param_as_form():
|
|||
response = client.post(
|
||||
"/form/python-tuple", data={"items": ["first", "second", "third"]}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == ["first", "second", "third"]
|
||||
|
|
|
|||
|
|
@ -18,5 +18,5 @@ client = TestClient(app)
|
|||
|
||||
def test_sub_router():
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"hello": "world"}
|
||||
|
|
|
|||
|
|
@ -46,21 +46,21 @@ client = TestClient(app)
|
|||
def test_get_users():
|
||||
"""Check that /users returns expected data"""
|
||||
response = client.get("/users")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [{"user_id": "u1"}, {"user_id": "u2"}]
|
||||
|
||||
|
||||
def test_get_user():
|
||||
"""Check that /users/{user_id} returns expected data"""
|
||||
response = client.get("/users/abc123")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"user_id": "abc123"}
|
||||
|
||||
|
||||
def test_get_items_1():
|
||||
"""Check that /items returns expected data"""
|
||||
response = client.get("/items")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [
|
||||
{"item_id": "i1", "user_id": "u1"},
|
||||
{"item_id": "i2", "user_id": "u2"},
|
||||
|
|
@ -70,42 +70,42 @@ def test_get_items_1():
|
|||
def test_get_items_2():
|
||||
"""Check that /items returns expected data with user_id specified"""
|
||||
response = client.get("/items?user_id=abc123")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [{"item_id": "i2", "user_id": "abc123"}]
|
||||
|
||||
|
||||
def test_get_item_1():
|
||||
"""Check that /items/{item_id} returns expected data"""
|
||||
response = client.get("/items/item01")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item_id": "item01"}
|
||||
|
||||
|
||||
def test_get_item_2():
|
||||
"""Check that /items/{item_id} returns expected data with user_id specified"""
|
||||
response = client.get("/items/item01?user_id=abc123")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item_id": "item01", "user_id": "abc123"}
|
||||
|
||||
|
||||
def test_get_users_items():
|
||||
"""Check that /users/{user_id}/items returns expected data"""
|
||||
response = client.get("/users/abc123/items")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [{"item_id": "i2", "user_id": "abc123"}]
|
||||
|
||||
|
||||
def test_get_users_item():
|
||||
"""Check that /users/{user_id}/items returns expected data"""
|
||||
response = client.get("/users/abc123/items/item01")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item_id": "item01", "user_id": "abc123"}
|
||||
|
||||
|
||||
def test_schema_1():
|
||||
"""Check that the user_id is a required path parameter under /users"""
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
r = response.json()
|
||||
|
||||
d = {
|
||||
|
|
@ -122,7 +122,7 @@ def test_schema_1():
|
|||
def test_schema_2():
|
||||
"""Check that the user_id is an optional query parameter under /items"""
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
r = response.json()
|
||||
|
||||
d = {
|
||||
|
|
|
|||
|
|
@ -125,31 +125,31 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_post_a():
|
||||
data = {"a": 2, "b": "foo"}
|
||||
response = client.post("/a/compute", json=data)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
|
||||
|
||||
def test_post_a_invalid():
|
||||
data = {"a": "bar", "b": "foo"}
|
||||
response = client.post("/a/compute", json=data)
|
||||
assert response.status_code == 422
|
||||
assert response.status_code == 422, response.text
|
||||
|
||||
|
||||
def test_post_b():
|
||||
data = {"a": 2, "b": "foo"}
|
||||
response = client.post("/b/compute/", json=data)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
|
||||
|
||||
def test_post_b_invalid():
|
||||
data = {"a": "bar", "b": "foo"}
|
||||
response = client.post("/b/compute/", json=data)
|
||||
assert response.status_code == 422
|
||||
assert response.status_code == 422, response.text
|
||||
|
|
|
|||
|
|
@ -139,23 +139,23 @@ multiple_errors = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_put_correct_body():
|
||||
response = client.post("/items/", json=[{"name": "Foo", "age": 5}])
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item": [{"name": "Foo", "age": 5}]}
|
||||
|
||||
|
||||
def test_jsonable_encoder_requiring_error():
|
||||
response = client.post("/items/", json=[{"name": "Foo", "age": -1.0}])
|
||||
assert response.status_code == 422
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == single_error
|
||||
|
||||
|
||||
def test_put_incorrect_body_multiple():
|
||||
response = client.post("/items/", json=[{"age": "five"}, {"age": "six"}])
|
||||
assert response.status_code == 422
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == multiple_errors
|
||||
|
|
|
|||
|
|
@ -102,17 +102,17 @@ multiple_errors = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_multi_query():
|
||||
response = client.get("/items/?q=5&q=6")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"q": [5, 6]}
|
||||
|
||||
|
||||
def test_multi_query_incorrect():
|
||||
response = client.get("/items/?q=five&q=six")
|
||||
assert response.status_code == 422
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == multiple_errors
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ client = TestClient(app)
|
|||
|
||||
def test_swagger_ui():
|
||||
response = client.get("/docs")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.headers["content-type"] == "text/html; charset=utf-8"
|
||||
assert "swagger-ui-dist" in response.text
|
||||
print(client.base_url)
|
||||
|
|
@ -23,7 +23,7 @@ def test_swagger_ui():
|
|||
|
||||
def test_swagger_ui_no_oauth2_redirect():
|
||||
response = client.get("/docs/oauth2-redirect")
|
||||
assert response.status_code == 404
|
||||
assert response.status_code == 404, response.text
|
||||
|
||||
|
||||
def test_response():
|
||||
|
|
|
|||
|
|
@ -15,11 +15,11 @@ client = TestClient(app)
|
|||
|
||||
def test_default_param_query_none():
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"q": None}
|
||||
|
||||
|
||||
def test_default_param_query():
|
||||
response = client.get("/items/?q=foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"q": "foo"}
|
||||
|
|
|
|||
|
|
@ -90,4 +90,4 @@ def test_reused_param():
|
|||
|
||||
def test_read_users():
|
||||
response = client.get("/users/42")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
|
|
|
|||
|
|
@ -8,13 +8,13 @@ client = TestClient(app)
|
|||
|
||||
def test_text_get():
|
||||
response = client.get("/text")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == "Hello World"
|
||||
|
||||
|
||||
def test_nonexistent():
|
||||
response = client.get("/nonexistent")
|
||||
assert response.status_code == 404
|
||||
assert response.status_code == 404, response.text
|
||||
assert response.json() == {"detail": "Not Found"}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -81,17 +81,17 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_put_no_body():
|
||||
response = client.put("/items/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item_id": "foo"}
|
||||
|
||||
|
||||
def test_put_no_body_with_body():
|
||||
response = client.put("/items/foo", json={"name": "Foo"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item_id": "foo"}
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ client = TestClient(app)
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
openapi_schema = response.json()
|
||||
assert (
|
||||
openapi_schema["paths"]["/products"]["post"]["requestBody"]
|
||||
|
|
|
|||
|
|
@ -22,5 +22,5 @@ client = TestClient(app)
|
|||
|
||||
def test_dependency_set_status_code():
|
||||
response = client.get("/")
|
||||
assert response.status_code == 201
|
||||
assert response.status_code == 201, response.text
|
||||
assert response.json() == {"msg": "Hello World"}
|
||||
|
|
|
|||
|
|
@ -110,5 +110,5 @@ client = TestClient(app)
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
|
|
|||
|
|
@ -104,5 +104,5 @@ client = TestClient(app)
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
|
|
|||
|
|
@ -145,16 +145,16 @@ client = TestClient(app)
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_path_operations():
|
||||
response = client.get("/valid1")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
response = client.get("/valid2")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
response = client.get("/valid3")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
response = client.get("/valid4")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ def test_router_events():
|
|||
assert state.router_shutdown is False
|
||||
assert state.sub_router_shutdown is False
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"message": "Hello World"}
|
||||
assert state.app_startup is True
|
||||
assert state.router_startup is True
|
||||
|
|
|
|||
|
|
@ -19,5 +19,5 @@ client = TestClient(app)
|
|||
|
||||
def test_get():
|
||||
response = client.get("/seg/users/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"segment": "seg", "id": "foo"}
|
||||
|
|
|
|||
|
|
@ -52,17 +52,17 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_api_key():
|
||||
response = client.get("/users/me", cookies={"key": "secret"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "secret"}
|
||||
|
||||
|
||||
def test_security_api_key_no_key():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 403
|
||||
assert response.status_code == 403, response.text
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
|
|
|
|||
|
|
@ -59,17 +59,17 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_api_key():
|
||||
response = client.get("/users/me", cookies={"key": "secret"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "secret"}
|
||||
|
||||
|
||||
def test_security_api_key_no_key():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Create an account first"}
|
||||
|
|
|
|||
|
|
@ -52,17 +52,17 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_api_key():
|
||||
response = client.get("/users/me", headers={"key": "secret"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "secret"}
|
||||
|
||||
|
||||
def test_security_api_key_no_key():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 403
|
||||
assert response.status_code == 403, response.text
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
|
|
|
|||
|
|
@ -58,17 +58,17 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_api_key():
|
||||
response = client.get("/users/me", headers={"key": "secret"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "secret"}
|
||||
|
||||
|
||||
def test_security_api_key_no_key():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Create an account first"}
|
||||
|
|
|
|||
|
|
@ -52,17 +52,17 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_api_key():
|
||||
response = client.get("/users/me?key=secret")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "secret"}
|
||||
|
||||
|
||||
def test_security_api_key_no_key():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 403
|
||||
assert response.status_code == 403, response.text
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
|
|
|
|||
|
|
@ -58,17 +58,17 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_api_key():
|
||||
response = client.get("/users/me?key=secret")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "secret"}
|
||||
|
||||
|
||||
def test_security_api_key_no_key():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Create an account first"}
|
||||
|
|
|
|||
|
|
@ -40,17 +40,17 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_http_base():
|
||||
response = client.get("/users/me", headers={"Authorization": "Other foobar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"scheme": "Other", "credentials": "foobar"}
|
||||
|
||||
|
||||
def test_security_http_base_no_credentials():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 403
|
||||
assert response.status_code == 403, response.text
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
|
|
|
|||
|
|
@ -46,17 +46,17 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_http_base():
|
||||
response = client.get("/users/me", headers={"Authorization": "Other foobar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"scheme": "Other", "credentials": "foobar"}
|
||||
|
||||
|
||||
def test_security_http_base_no_credentials():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Create an account first"}
|
||||
|
|
|
|||
|
|
@ -46,20 +46,20 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_http_basic():
|
||||
auth = HTTPBasicAuth(username="john", password="secret")
|
||||
response = client.get("/users/me", auth=auth)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "john", "password": "secret"}
|
||||
|
||||
|
||||
def test_security_http_basic_no_credentials():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Create an account first"}
|
||||
|
||||
|
||||
|
|
@ -67,7 +67,7 @@ def test_security_http_basic_invalid_credentials():
|
|||
response = client.get(
|
||||
"/users/me", headers={"Authorization": "Basic notabase64token"}
|
||||
)
|
||||
assert response.status_code == 401
|
||||
assert response.status_code == 401, response.text
|
||||
assert response.headers["WWW-Authenticate"] == "Basic"
|
||||
assert response.json() == {"detail": "Invalid authentication credentials"}
|
||||
|
||||
|
|
@ -76,6 +76,6 @@ def test_security_http_basic_non_basic_credentials():
|
|||
payload = b64encode(b"johnsecret").decode("ascii")
|
||||
auth_header = f"Basic {payload}"
|
||||
response = client.get("/users/me", headers={"Authorization": auth_header})
|
||||
assert response.status_code == 401
|
||||
assert response.status_code == 401, response.text
|
||||
assert response.headers["WWW-Authenticate"] == "Basic"
|
||||
assert response.json() == {"detail": "Invalid authentication credentials"}
|
||||
|
|
|
|||
|
|
@ -43,21 +43,21 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_http_basic():
|
||||
auth = HTTPBasicAuth(username="john", password="secret")
|
||||
response = client.get("/users/me", auth=auth)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "john", "password": "secret"}
|
||||
|
||||
|
||||
def test_security_http_basic_no_credentials():
|
||||
response = client.get("/users/me")
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
assert response.status_code == 401
|
||||
assert response.status_code == 401, response.text
|
||||
assert response.headers["WWW-Authenticate"] == 'Basic realm="simple"'
|
||||
|
||||
|
||||
|
|
@ -65,7 +65,7 @@ def test_security_http_basic_invalid_credentials():
|
|||
response = client.get(
|
||||
"/users/me", headers={"Authorization": "Basic notabase64token"}
|
||||
)
|
||||
assert response.status_code == 401
|
||||
assert response.status_code == 401, response.text
|
||||
assert response.headers["WWW-Authenticate"] == 'Basic realm="simple"'
|
||||
assert response.json() == {"detail": "Invalid authentication credentials"}
|
||||
|
||||
|
|
@ -74,6 +74,6 @@ def test_security_http_basic_non_basic_credentials():
|
|||
payload = b64encode(b"johnsecret").decode("ascii")
|
||||
auth_header = f"Basic {payload}"
|
||||
response = client.get("/users/me", headers={"Authorization": auth_header})
|
||||
assert response.status_code == 401
|
||||
assert response.status_code == 401, response.text
|
||||
assert response.headers["WWW-Authenticate"] == 'Basic realm="simple"'
|
||||
assert response.json() == {"detail": "Invalid authentication credentials"}
|
||||
|
|
|
|||
|
|
@ -40,23 +40,23 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_http_bearer():
|
||||
response = client.get("/users/me", headers={"Authorization": "Bearer foobar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"scheme": "Bearer", "credentials": "foobar"}
|
||||
|
||||
|
||||
def test_security_http_bearer_no_credentials():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 403
|
||||
assert response.status_code == 403, response.text
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
|
||||
|
||||
def test_security_http_bearer_incorrect_scheme_credentials():
|
||||
response = client.get("/users/me", headers={"Authorization": "Basic notreally"})
|
||||
assert response.status_code == 403
|
||||
assert response.status_code == 403, response.text
|
||||
assert response.json() == {"detail": "Invalid authentication credentials"}
|
||||
|
|
|
|||
|
|
@ -46,23 +46,23 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_http_bearer():
|
||||
response = client.get("/users/me", headers={"Authorization": "Bearer foobar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"scheme": "Bearer", "credentials": "foobar"}
|
||||
|
||||
|
||||
def test_security_http_bearer_no_credentials():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Create an account first"}
|
||||
|
||||
|
||||
def test_security_http_bearer_incorrect_scheme_credentials():
|
||||
response = client.get("/users/me", headers={"Authorization": "Basic notreally"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Create an account first"}
|
||||
|
|
|
|||
|
|
@ -40,19 +40,19 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_http_digest():
|
||||
response = client.get("/users/me", headers={"Authorization": "Digest foobar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"scheme": "Digest", "credentials": "foobar"}
|
||||
|
||||
|
||||
def test_security_http_digest_no_credentials():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 403
|
||||
assert response.status_code == 403, response.text
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
|
||||
|
||||
|
|
@ -60,5 +60,5 @@ def test_security_http_digest_incorrect_scheme_credentials():
|
|||
response = client.get(
|
||||
"/users/me", headers={"Authorization": "Other invalidauthorization"}
|
||||
)
|
||||
assert response.status_code == 403
|
||||
assert response.status_code == 403, response.text
|
||||
assert response.json() == {"detail": "Invalid authentication credentials"}
|
||||
|
|
|
|||
|
|
@ -46,19 +46,19 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_http_digest():
|
||||
response = client.get("/users/me", headers={"Authorization": "Digest foobar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"scheme": "Digest", "credentials": "foobar"}
|
||||
|
||||
|
||||
def test_security_http_digest_no_credentials():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Create an account first"}
|
||||
|
||||
|
||||
|
|
@ -66,5 +66,5 @@ def test_security_http_digest_incorrect_scheme_credentials():
|
|||
response = client.get(
|
||||
"/users/me", headers={"Authorization": "Other invalidauthorization"}
|
||||
)
|
||||
assert response.status_code == 403
|
||||
assert response.status_code == 403, response.text
|
||||
assert response.json() == {"detail": "Invalid authentication credentials"}
|
||||
|
|
|
|||
|
|
@ -156,25 +156,25 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_oauth2():
|
||||
response = client.get("/users/me", headers={"Authorization": "Bearer footokenbar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "Bearer footokenbar"}
|
||||
|
||||
|
||||
def test_security_oauth2_password_other_header():
|
||||
response = client.get("/users/me", headers={"Authorization": "Other footokenbar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "Other footokenbar"}
|
||||
|
||||
|
||||
def test_security_oauth2_password_bearer_no_header():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 403
|
||||
assert response.status_code == 403, response.text
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -55,23 +55,23 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_no_token():
|
||||
response = client.get("/items")
|
||||
assert response.status_code == 401
|
||||
assert response.status_code == 401, response.text
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
|
||||
|
||||
def test_incorrect_token():
|
||||
response = client.get("/items", headers={"Authorization": "Non-existent testtoken"})
|
||||
assert response.status_code == 401
|
||||
assert response.status_code == 401, response.text
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
|
||||
|
||||
def test_token():
|
||||
response = client.get("/items", headers={"Authorization": "Bearer testtoken"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"token": "testtoken"}
|
||||
|
|
|
|||
|
|
@ -160,25 +160,25 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_oauth2():
|
||||
response = client.get("/users/me", headers={"Authorization": "Bearer footokenbar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "Bearer footokenbar"}
|
||||
|
||||
|
||||
def test_security_oauth2_password_other_header():
|
||||
response = client.get("/users/me", headers={"Authorization": "Other footokenbar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "Other footokenbar"}
|
||||
|
||||
|
||||
def test_security_oauth2_password_bearer_no_header():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Create an account first"}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -49,23 +49,23 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_no_token():
|
||||
response = client.get("/items")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Create an account first"}
|
||||
|
||||
|
||||
def test_token():
|
||||
response = client.get("/items", headers={"Authorization": "Bearer testtoken"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"token": "testtoken"}
|
||||
|
||||
|
||||
def test_incorrect_token():
|
||||
response = client.get("/items", headers={"Authorization": "Notexistent testtoken"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Create an account first"}
|
||||
|
|
|
|||
|
|
@ -52,23 +52,23 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_oauth2():
|
||||
response = client.get("/users/me", headers={"Authorization": "Bearer footokenbar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "Bearer footokenbar"}
|
||||
|
||||
|
||||
def test_security_oauth2_password_other_header():
|
||||
response = client.get("/users/me", headers={"Authorization": "Other footokenbar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "Other footokenbar"}
|
||||
|
||||
|
||||
def test_security_oauth2_password_bearer_no_header():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 403
|
||||
assert response.status_code == 403, response.text
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
|
|
|
|||
|
|
@ -58,23 +58,23 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_security_oauth2():
|
||||
response = client.get("/users/me", headers={"Authorization": "Bearer footokenbar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "Bearer footokenbar"}
|
||||
|
||||
|
||||
def test_security_oauth2_password_other_header():
|
||||
response = client.get("/users/me", headers={"Authorization": "Other footokenbar"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"username": "Other footokenbar"}
|
||||
|
||||
|
||||
def test_security_oauth2_password_bearer_no_header():
|
||||
response = client.get("/users/me")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Create an account first"}
|
||||
|
|
|
|||
|
|
@ -126,31 +126,31 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get_item():
|
||||
response = client.get("/items/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item": "The Foo Wrestlers"}
|
||||
|
||||
|
||||
def test_get_item_not_found():
|
||||
response = client.get("/items/bar")
|
||||
assert response.status_code == 404
|
||||
assert response.status_code == 404, response.text
|
||||
assert response.headers.get("x-error") == "Some custom header"
|
||||
assert response.json() == {"detail": "Item not found"}
|
||||
|
||||
|
||||
def test_get_starlette_item():
|
||||
response = client.get("/starlette-items/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item": "The Foo Wrestlers"}
|
||||
|
||||
|
||||
def test_get_starlette_item_not_found():
|
||||
response = client.get("/starlette-items/bar")
|
||||
assert response.status_code == 404
|
||||
assert response.status_code == 404, response.text
|
||||
assert response.headers.get("x-error") is None
|
||||
assert response.json() == {"detail": "Item not found"}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ client = TestClient(app)
|
|||
def test_route_converters_int():
|
||||
# Test integer conversion
|
||||
response = client.get("/int/5")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"int": 5}
|
||||
assert app.url_path_for("int_convertor", param=5) == "/int/5"
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ def test_route_converters_int():
|
|||
def test_route_converters_float():
|
||||
# Test float conversion
|
||||
response = client.get("/float/25.5")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"float": 25.5}
|
||||
assert app.url_path_for("float_convertor", param=25.5) == "/float/25.5"
|
||||
|
||||
|
|
@ -41,7 +41,7 @@ def test_route_converters_float():
|
|||
def test_route_converters_path():
|
||||
# Test path conversion
|
||||
response = client.get("/path/some/example")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"path": "some/example"}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -221,7 +221,7 @@ def test_get():
|
|||
response = client.post(
|
||||
"/invoices/", json={"id": "fooinvoice", "customer": "John", "total": 5.3}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Invoice received"}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ client = TestClient(app)
|
|||
|
||||
def test_swagger_ui():
|
||||
response = client.get("/docs")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
print(response.text)
|
||||
assert f"ui.initOAuth" in response.text
|
||||
assert f'"appName": "The Predendapp"' in response.text
|
||||
|
|
|
|||
|
|
@ -100,17 +100,17 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_path_operation():
|
||||
response = client.get("/items/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"id": "foo", "value": "there goes my hero"}
|
||||
|
||||
|
||||
def test_path_operation_not_found():
|
||||
response = client.get("/items/bar")
|
||||
assert response.status_code == 404
|
||||
assert response.status_code == 404, response.text
|
||||
assert response.json() == {"message": "Item not found"}
|
||||
|
|
|
|||
|
|
@ -96,20 +96,20 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_path_operation():
|
||||
response = client.get("/items/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"id": "foo", "value": "there goes my hero"}
|
||||
|
||||
|
||||
def test_path_operation_img():
|
||||
shutil.copy("./docs/en/docs/img/favicon.png", "./image.png")
|
||||
response = client.get("/items/foo?img=1")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.headers["Content-Type"] == "image/png"
|
||||
assert len(response.content)
|
||||
os.remove("./image.png")
|
||||
|
|
|
|||
|
|
@ -101,17 +101,17 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_path_operation():
|
||||
response = client.get("/items/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"id": "foo", "value": "there goes my hero"}
|
||||
|
||||
|
||||
def test_path_operation_not_found():
|
||||
response = client.get("/items/bar")
|
||||
assert response.status_code == 404
|
||||
assert response.status_code == 404, response.text
|
||||
assert response.json() == {"message": "Item not found"}
|
||||
|
|
|
|||
|
|
@ -99,20 +99,20 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_path_operation():
|
||||
response = client.get("/items/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"id": "foo", "value": "there goes my hero"}
|
||||
|
||||
|
||||
def test_path_operation_img():
|
||||
shutil.copy("./docs/en/docs/img/favicon.png", "./image.png")
|
||||
response = client.get("/items/foo?img=1")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.headers["Content-Type"] == "image/png"
|
||||
assert len(response.content)
|
||||
os.remove("./image.png")
|
||||
|
|
|
|||
|
|
@ -7,11 +7,11 @@ client = TestClient(app)
|
|||
|
||||
def test_update():
|
||||
response = client.put("/items/foo", json={"name": "Wrestlers"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"name": "Wrestlers", "size": None}
|
||||
|
||||
|
||||
def test_create():
|
||||
response = client.put("/items/red", json={"name": "Chillies"})
|
||||
assert response.status_code == 201
|
||||
assert response.status_code == 201, response.text
|
||||
assert response.json() == {"name": "Chillies", "size": None}
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@ from advanced_middleware.tutorial001 import app
|
|||
def test_middleware():
|
||||
client = TestClient(app, base_url="https://testserver")
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
|
||||
client = TestClient(app)
|
||||
response = client.get("/", allow_redirects=False)
|
||||
assert response.status_code == 307
|
||||
assert response.status_code == 307, response.text
|
||||
assert response.headers["location"] == "https://testserver/"
|
||||
|
|
|
|||
|
|
@ -6,10 +6,10 @@ from advanced_middleware.tutorial002 import app
|
|||
def test_middleware():
|
||||
client = TestClient(app, base_url="http://example.com")
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
client = TestClient(app, base_url="http://subdomain.example.com")
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
client = TestClient(app, base_url="http://invalidhost")
|
||||
response = client.get("/")
|
||||
assert response.status_code == 400
|
||||
assert response.status_code == 400, response.text
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@ client = TestClient(app)
|
|||
|
||||
def test_middleware():
|
||||
response = client.get("/large", headers={"accept-encoding": "gzip"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.text == "x" * 4000
|
||||
assert response.headers["Content-Encoding"] == "gzip"
|
||||
assert int(response.headers["Content-Length"]) < 4000
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
|
|
|
|||
|
|
@ -113,7 +113,7 @@ openapi_schema = {
|
|||
def test_openapi_schema():
|
||||
with TestClient(app) as client:
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
|
|
@ -121,11 +121,11 @@ def test_create_read():
|
|||
with TestClient(app) as client:
|
||||
note = {"text": "Foo bar", "completed": False}
|
||||
response = client.post("/notes/", json=note)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert data["text"] == note["text"]
|
||||
assert data["completed"] == note["completed"]
|
||||
assert "id" in data
|
||||
response = client.get(f"/notes/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert data in response.json()
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ def test():
|
|||
if log.is_file():
|
||||
os.remove(log) # pragma: no cover
|
||||
response = client.post("/send-notification/foo@example.com")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"message": "Notification sent in the background"}
|
||||
with open("./log.txt") as f:
|
||||
assert "notification for foo@example.com: some notification" in f.read()
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ def test():
|
|||
if log.is_file():
|
||||
os.remove(log) # pragma: no cover
|
||||
response = client.post("/send-notification/foo@example.com?q=some-query")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"message": "Message sent"}
|
||||
with open("./log.txt") as f:
|
||||
assert "found query: some-query\nmessage to foo@example.com" in f.read()
|
||||
|
|
|
|||
|
|
@ -270,7 +270,7 @@ def test_get_path(path, expected_status, expected_response, headers):
|
|||
|
||||
def test_put_no_header():
|
||||
response = client.put("/items/foo")
|
||||
assert response.status_code == 422
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
|
|
@ -284,17 +284,17 @@ def test_put_no_header():
|
|||
|
||||
def test_put_invalid_header():
|
||||
response = client.put("/items/foo", headers={"X-Token": "invalid"})
|
||||
assert response.status_code == 400
|
||||
assert response.status_code == 400, response.text
|
||||
assert response.json() == {"detail": "X-Token header invalid"}
|
||||
|
||||
|
||||
def test_put():
|
||||
response = client.put("/items/foo", headers={"X-Token": "fake-super-secret-token"})
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item_id": "foo", "name": "The Fighters"}
|
||||
|
||||
|
||||
def test_put_forbidden():
|
||||
response = client.put("/items/bar", headers={"X-Token": "fake-super-secret-token"})
|
||||
assert response.status_code == 403
|
||||
assert response.status_code == 403, response.text
|
||||
assert response.json() == {"detail": "You can only update the item: foo"}
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
|
|
@ -176,5 +176,5 @@ def test_post_body(path, body, expected_status, expected_response):
|
|||
|
||||
def test_post_broken_body():
|
||||
response = client.post("/items/", data={"name": "Foo", "price": 50.5})
|
||||
assert response.status_code == 400
|
||||
assert response.status_code == 400, response.text
|
||||
assert response.json() == {"detail": "There was an error parsing the body"}
|
||||
|
|
|
|||
|
|
@ -120,7 +120,7 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -77,21 +77,21 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_post_body():
|
||||
data = {"2": 2.2, "3": 3.3}
|
||||
response = client.post("/index-weights/", json=data)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == data
|
||||
|
||||
|
||||
def test_post_invalid_body():
|
||||
data = {"foo": 2.2, "3": 3.3}
|
||||
response = client.post("/index-weights/", json=data)
|
||||
assert response.status_code == 422
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
|
|
|
|||
|
|
@ -133,13 +133,13 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get():
|
||||
response = client.get("/items/baz")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {
|
||||
"name": "Baz",
|
||||
"description": None,
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ def test_cors():
|
|||
"Access-Control-Request-Headers": "X-Example",
|
||||
}
|
||||
response = client.options("/", headers=headers)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.text == "OK"
|
||||
assert (
|
||||
response.headers["access-control-allow-origin"]
|
||||
|
|
@ -23,7 +23,7 @@ def test_cors():
|
|||
# Test standard response
|
||||
headers = {"Origin": "https://localhost.tiangolo.com"}
|
||||
response = client.get("/", headers=headers)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"message": "Hello World"}
|
||||
assert (
|
||||
response.headers["access-control-allow-origin"]
|
||||
|
|
@ -32,6 +32,6 @@ def test_cors():
|
|||
|
||||
# Test non-CORS response
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"message": "Hello World"}
|
||||
assert "access-control-allow-origin" not in response.headers
|
||||
|
|
|
|||
|
|
@ -26,11 +26,11 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get_custom_response():
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [{"item_id": "Foo"}]
|
||||
|
|
|
|||
|
|
@ -37,11 +37,11 @@ html_contents = """
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get_custom_response():
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.text == html_contents
|
||||
|
|
|
|||
|
|
@ -26,11 +26,11 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get():
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.text == "Hello World"
|
||||
|
|
|
|||
|
|
@ -7,5 +7,5 @@ client = TestClient(app)
|
|||
|
||||
def test_get():
|
||||
response = client.get("/typer", allow_redirects=False)
|
||||
assert response.status_code == 307
|
||||
assert response.status_code == 307, response.text
|
||||
assert response.headers["location"] == "https://typer.tiangolo.com"
|
||||
|
|
|
|||
|
|
@ -128,7 +128,7 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -79,13 +79,13 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get_no_headers():
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 422
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
|
|
@ -104,7 +104,7 @@ def test_get_no_headers():
|
|||
|
||||
def test_get_invalid_one_header():
|
||||
response = client.get("/items/", headers={"X-Token": "invalid"})
|
||||
assert response.status_code == 400
|
||||
assert response.status_code == 400, response.text
|
||||
assert response.json() == {"detail": "X-Token header invalid"}
|
||||
|
||||
|
||||
|
|
@ -112,7 +112,7 @@ def test_get_invalid_second_header():
|
|||
response = client.get(
|
||||
"/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert response.status_code == 400, response.text
|
||||
assert response.json() == {"detail": "X-Key header invalid"}
|
||||
|
||||
|
||||
|
|
@ -124,5 +124,5 @@ def test_get_valid_headers():
|
|||
"X-Key": "fake-super-secret-key",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [{"item": "Foo"}, {"item": "Bar"}]
|
||||
|
|
|
|||
|
|
@ -72,8 +72,8 @@ openapi_schema = {
|
|||
def test_events():
|
||||
with TestClient(app) as client:
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
response = client.get("/items/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"name": "Fighters"}
|
||||
|
|
|
|||
|
|
@ -25,10 +25,10 @@ openapi_schema = {
|
|||
def test_events():
|
||||
with TestClient(app) as client:
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [{"name": "Foo"}]
|
||||
with open("log.txt") as log:
|
||||
assert "Application shutdown" in log.read()
|
||||
|
|
|
|||
|
|
@ -31,14 +31,14 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test():
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [{"name": "Foo"}]
|
||||
|
|
|
|||
|
|
@ -19,24 +19,24 @@ def client():
|
|||
|
||||
def test_swagger_ui_html(client: TestClient):
|
||||
response = client.get("/docs")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert "/static/swagger-ui-bundle.js" in response.text
|
||||
assert "/static/swagger-ui.css" in response.text
|
||||
|
||||
|
||||
def test_swagger_ui_oauth2_redirect_html(client: TestClient):
|
||||
response = client.get("/docs/oauth2-redirect")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert "window.opener.swaggerUIRedirectOauth2" in response.text
|
||||
|
||||
|
||||
def test_redoc_html(client: TestClient):
|
||||
response = client.get("/redoc")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert "/static/redoc.standalone.js" in response.text
|
||||
|
||||
|
||||
def test_api(client: TestClient):
|
||||
response = client.get("/users/john")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json()["message"] == "Hello john"
|
||||
|
|
|
|||
|
|
@ -113,7 +113,7 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
|
|
@ -134,5 +134,5 @@ def test_extra_types():
|
|||
}
|
||||
)
|
||||
response = client.put(f"/items/{item_id}", json=data)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == expected_response
|
||||
|
|
|
|||
|
|
@ -102,13 +102,13 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get_car():
|
||||
response = client.get("/items/item1")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {
|
||||
"description": "All my friends drive a low rider",
|
||||
"type": "car",
|
||||
|
|
@ -117,7 +117,7 @@ def test_get_car():
|
|||
|
||||
def test_get_plane():
|
||||
response = client.get("/items/item2")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {
|
||||
"description": "Music is my aeroplane, it's my aeroplane",
|
||||
"type": "plane",
|
||||
|
|
|
|||
|
|
@ -47,13 +47,13 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get_items():
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [
|
||||
{"name": "Foo", "description": "There comes my hero"},
|
||||
{"name": "Red", "description": "It's my aeroplane"},
|
||||
|
|
|
|||
|
|
@ -34,11 +34,11 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get_items():
|
||||
response = client.get("/keyword-weights/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"foo": 2.3, "bar": 3.4}
|
||||
|
|
|
|||
|
|
@ -73,18 +73,18 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get_item():
|
||||
response = client.get("/items/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item": "The Foo Wrestlers"}
|
||||
|
||||
|
||||
def test_get_item_not_found():
|
||||
response = client.get("/items/bar")
|
||||
assert response.status_code == 404
|
||||
assert response.status_code == 404, response.text
|
||||
assert response.headers.get("x-error") is None
|
||||
assert response.json() == {"detail": "Item not found"}
|
||||
|
|
|
|||
|
|
@ -73,18 +73,18 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get_item_header():
|
||||
response = client.get("/items-header/foo")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item": "The Foo Wrestlers"}
|
||||
|
||||
|
||||
def test_get_item_not_found_header():
|
||||
response = client.get("/items-header/bar")
|
||||
assert response.status_code == 404
|
||||
assert response.status_code == 404, response.text
|
||||
assert response.headers.get("x-error") == "There goes my error"
|
||||
assert response.json() == {"detail": "Item not found"}
|
||||
|
|
|
|||
|
|
@ -73,19 +73,19 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get():
|
||||
response = client.get("/unicorns/shinny")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"unicorn_name": "shinny"}
|
||||
|
||||
|
||||
def test_get_exception():
|
||||
response = client.get("/unicorns/yolo")
|
||||
assert response.status_code == 418
|
||||
assert response.status_code == 418, response.text
|
||||
assert response.json() == {
|
||||
"message": "Oops! yolo did something. There goes a rainbow..."
|
||||
}
|
||||
|
|
|
|||
|
|
@ -73,13 +73,13 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get_validation_error():
|
||||
response = client.get("/items/foo")
|
||||
assert response.status_code == 400
|
||||
assert response.status_code == 400, response.text
|
||||
validation_error_str_lines = [
|
||||
b"1 validation error for Request",
|
||||
b"path -> item_id",
|
||||
|
|
@ -90,11 +90,11 @@ def test_get_validation_error():
|
|||
|
||||
def test_get_http_error():
|
||||
response = client.get("/items/3")
|
||||
assert response.status_code == 418
|
||||
assert response.status_code == 418, response.text
|
||||
assert response.content == b"Nope! I don't like 3."
|
||||
|
||||
|
||||
def test_get():
|
||||
response = client.get("/items/2")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item_id": 2}
|
||||
|
|
|
|||
|
|
@ -82,13 +82,13 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_post_validation_error():
|
||||
response = client.post("/items/", json={"title": "towel", "size": "XL"})
|
||||
assert response.status_code == 422
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
|
|
@ -104,5 +104,5 @@ def test_post_validation_error():
|
|||
def test_post():
|
||||
data = {"title": "towel", "size": 5}
|
||||
response = client.post("/items/", json=data)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == data
|
||||
|
|
|
|||
|
|
@ -73,13 +73,13 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get_validation_error():
|
||||
response = client.get("/items/foo")
|
||||
assert response.status_code == 422
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
{
|
||||
|
|
@ -93,11 +93,11 @@ def test_get_validation_error():
|
|||
|
||||
def test_get_http_error():
|
||||
response = client.get("/items/3")
|
||||
assert response.status_code == 418
|
||||
assert response.status_code == 418, response.text
|
||||
assert response.json() == {"detail": "Nope! I don't like 3."}
|
||||
|
||||
|
||||
def test_get():
|
||||
response = client.get("/items/2")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item_id": 2}
|
||||
|
|
|
|||
|
|
@ -30,11 +30,11 @@ openapi_schema = {
|
|||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_items():
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [{"name": "Foo"}]
|
||||
|
|
|
|||
|
|
@ -165,7 +165,7 @@ def test_get():
|
|||
response = client.post(
|
||||
"/invoices/", json={"id": "fooinvoice", "customer": "John", "total": 5.3}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"msg": "Invoice received"}
|
||||
|
||||
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue