mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-13 02:05:20 -04:00
⬆ Upgrade Starlette to 0.21.0, including the new [TestClient based on HTTPX](https://github.com/encode/starlette/releases/tag/0.21.0) (#5471)
Co-authored-by: Paweł Rubin <pawel.rubin@ocado.com> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
@@ -9,6 +9,6 @@ def test_middleware():
|
||||
assert response.status_code == 200, response.text
|
||||
|
||||
client = TestClient(app)
|
||||
response = client.get("/", allow_redirects=False)
|
||||
response = client.get("/", follow_redirects=False)
|
||||
assert response.status_code == 307, response.text
|
||||
assert response.headers["location"] == "https://testserver/"
|
||||
|
||||
@@ -176,7 +176,7 @@ def test_post_broken_body():
|
||||
response = client.post(
|
||||
"/items/",
|
||||
headers={"content-type": "application/json"},
|
||||
data="{some broken json}",
|
||||
content="{some broken json}",
|
||||
)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == {
|
||||
@@ -214,7 +214,7 @@ def test_post_form_for_json():
|
||||
def test_explicit_content_type():
|
||||
response = client.post(
|
||||
"/items/",
|
||||
data='{"name": "Foo", "price": 50.5}',
|
||||
content='{"name": "Foo", "price": 50.5}',
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
@@ -223,7 +223,7 @@ def test_explicit_content_type():
|
||||
def test_geo_json():
|
||||
response = client.post(
|
||||
"/items/",
|
||||
data='{"name": "Foo", "price": 50.5}',
|
||||
content='{"name": "Foo", "price": 50.5}',
|
||||
headers={"Content-Type": "application/geo+json"},
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
@@ -232,7 +232,7 @@ def test_geo_json():
|
||||
def test_no_content_type_is_json():
|
||||
response = client.post(
|
||||
"/items/",
|
||||
data='{"name": "Foo", "price": 50.5}',
|
||||
content='{"name": "Foo", "price": 50.5}',
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {
|
||||
@@ -255,17 +255,19 @@ def test_wrong_headers():
|
||||
]
|
||||
}
|
||||
|
||||
response = client.post("/items/", data=data, headers={"Content-Type": "text/plain"})
|
||||
response = client.post(
|
||||
"/items/", content=data, headers={"Content-Type": "text/plain"}
|
||||
)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == invalid_dict
|
||||
|
||||
response = client.post(
|
||||
"/items/", data=data, headers={"Content-Type": "application/geo+json-seq"}
|
||||
"/items/", content=data, headers={"Content-Type": "application/geo+json-seq"}
|
||||
)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == invalid_dict
|
||||
response = client.post(
|
||||
"/items/", data=data, headers={"Content-Type": "application/not-really-json"}
|
||||
"/items/", content=data, headers={"Content-Type": "application/not-really-json"}
|
||||
)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == invalid_dict
|
||||
|
||||
@@ -185,7 +185,7 @@ def test_post_broken_body(client: TestClient):
|
||||
response = client.post(
|
||||
"/items/",
|
||||
headers={"content-type": "application/json"},
|
||||
data="{some broken json}",
|
||||
content="{some broken json}",
|
||||
)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == {
|
||||
@@ -225,7 +225,7 @@ def test_post_form_for_json(client: TestClient):
|
||||
def test_explicit_content_type(client: TestClient):
|
||||
response = client.post(
|
||||
"/items/",
|
||||
data='{"name": "Foo", "price": 50.5}',
|
||||
content='{"name": "Foo", "price": 50.5}',
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
@@ -235,7 +235,7 @@ def test_explicit_content_type(client: TestClient):
|
||||
def test_geo_json(client: TestClient):
|
||||
response = client.post(
|
||||
"/items/",
|
||||
data='{"name": "Foo", "price": 50.5}',
|
||||
content='{"name": "Foo", "price": 50.5}',
|
||||
headers={"Content-Type": "application/geo+json"},
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
@@ -245,7 +245,7 @@ def test_geo_json(client: TestClient):
|
||||
def test_no_content_type_is_json(client: TestClient):
|
||||
response = client.post(
|
||||
"/items/",
|
||||
data='{"name": "Foo", "price": 50.5}',
|
||||
content='{"name": "Foo", "price": 50.5}',
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {
|
||||
@@ -269,17 +269,19 @@ def test_wrong_headers(client: TestClient):
|
||||
]
|
||||
}
|
||||
|
||||
response = client.post("/items/", data=data, headers={"Content-Type": "text/plain"})
|
||||
response = client.post(
|
||||
"/items/", content=data, headers={"Content-Type": "text/plain"}
|
||||
)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == invalid_dict
|
||||
|
||||
response = client.post(
|
||||
"/items/", data=data, headers={"Content-Type": "application/geo+json-seq"}
|
||||
"/items/", content=data, headers={"Content-Type": "application/geo+json-seq"}
|
||||
)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == invalid_dict
|
||||
response = client.post(
|
||||
"/items/", data=data, headers={"Content-Type": "application/not-really-json"}
|
||||
"/items/", content=data, headers={"Content-Type": "application/not-really-json"}
|
||||
)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == invalid_dict
|
||||
|
||||
@@ -3,8 +3,6 @@ from fastapi.testclient import TestClient
|
||||
|
||||
from docs_src.cookie_params.tutorial001 import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
openapi_schema = {
|
||||
"openapi": "3.0.2",
|
||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
||||
@@ -88,6 +86,7 @@ openapi_schema = {
|
||||
],
|
||||
)
|
||||
def test(path, cookies, expected_status, expected_response):
|
||||
response = client.get(path, cookies=cookies)
|
||||
client = TestClient(app, cookies=cookies)
|
||||
response = client.get(path)
|
||||
assert response.status_code == expected_status
|
||||
assert response.json() == expected_response
|
||||
|
||||
@@ -70,14 +70,6 @@ openapi_schema = {
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(name="client")
|
||||
def get_client():
|
||||
from docs_src.cookie_params.tutorial001_py310 import app
|
||||
|
||||
client = TestClient(app)
|
||||
return client
|
||||
|
||||
|
||||
@needs_py310
|
||||
@pytest.mark.parametrize(
|
||||
"path,cookies,expected_status,expected_response",
|
||||
@@ -94,7 +86,10 @@ def get_client():
|
||||
("/items", {"session": "cookiesession"}, 200, {"ads_id": None}),
|
||||
],
|
||||
)
|
||||
def test(path, cookies, expected_status, expected_response, client: TestClient):
|
||||
response = client.get(path, cookies=cookies)
|
||||
def test(path, cookies, expected_status, expected_response):
|
||||
from docs_src.cookie_params.tutorial001_py310 import app
|
||||
|
||||
client = TestClient(app, cookies=cookies)
|
||||
response = client.get(path)
|
||||
assert response.status_code == expected_status
|
||||
assert response.json() == expected_response
|
||||
|
||||
@@ -26,7 +26,7 @@ def test_gzip_request(compress):
|
||||
data = gzip.compress(data)
|
||||
headers["Content-Encoding"] = "gzip"
|
||||
headers["Content-Type"] = "application/json"
|
||||
response = client.post("/sum", data=data, headers=headers)
|
||||
response = client.post("/sum", content=data, headers=headers)
|
||||
assert response.json() == {"sum": n}
|
||||
|
||||
|
||||
|
||||
@@ -32,6 +32,6 @@ def test_openapi_schema():
|
||||
|
||||
|
||||
def test_get():
|
||||
response = client.get("/typer", allow_redirects=False)
|
||||
response = client.get("/typer", follow_redirects=False)
|
||||
assert response.status_code == 307, response.text
|
||||
assert response.headers["location"] == "https://typer.tiangolo.com"
|
||||
|
||||
@@ -27,6 +27,6 @@ def test_openapi_schema():
|
||||
|
||||
|
||||
def test_redirect_response_class():
|
||||
response = client.get("/fastapi", allow_redirects=False)
|
||||
response = client.get("/fastapi", follow_redirects=False)
|
||||
assert response.status_code == 307
|
||||
assert response.headers["location"] == "https://fastapi.tiangolo.com"
|
||||
|
||||
@@ -27,6 +27,6 @@ def test_openapi_schema():
|
||||
|
||||
|
||||
def test_redirect_status_code():
|
||||
response = client.get("/pydantic", allow_redirects=False)
|
||||
response = client.get("/pydantic", follow_redirects=False)
|
||||
assert response.status_code == 302
|
||||
assert response.headers["location"] == "https://pydantic-docs.helpmanual.io/"
|
||||
|
||||
@@ -47,7 +47,7 @@ def test_openapi_schema():
|
||||
|
||||
|
||||
def test_post():
|
||||
response = client.post("/items/", data=b"this is actually not validated")
|
||||
response = client.post("/items/", content=b"this is actually not validated")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {
|
||||
"size": 30,
|
||||
|
||||
@@ -58,7 +58,7 @@ def test_post():
|
||||
- x-men
|
||||
- x-avengers
|
||||
"""
|
||||
response = client.post("/items/", data=yaml_data)
|
||||
response = client.post("/items/", content=yaml_data)
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {
|
||||
"name": "Deadpoolio",
|
||||
@@ -74,7 +74,7 @@ def test_post_broken_yaml():
|
||||
x - x-men
|
||||
x - x-avengers
|
||||
"""
|
||||
response = client.post("/items/", data=yaml_data)
|
||||
response = client.post("/items/", content=yaml_data)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == {"detail": "Invalid YAML"}
|
||||
|
||||
@@ -88,7 +88,7 @@ def test_post_invalid():
|
||||
- x-avengers
|
||||
- sneaky: object
|
||||
"""
|
||||
response = client.post("/items/", data=yaml_data)
|
||||
response = client.post("/items/", content=yaml_data)
|
||||
assert response.status_code == 422, response.text
|
||||
assert response.json() == {
|
||||
"detail": [
|
||||
|
||||
@@ -4,20 +4,18 @@ from fastapi.websockets import WebSocketDisconnect
|
||||
|
||||
from docs_src.websockets.tutorial002 import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_main():
|
||||
client = TestClient(app)
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200, response.text
|
||||
assert b"<!DOCTYPE html>" in response.content
|
||||
|
||||
|
||||
def test_websocket_with_cookie():
|
||||
client = TestClient(app, cookies={"session": "fakesession"})
|
||||
with pytest.raises(WebSocketDisconnect):
|
||||
with client.websocket_connect(
|
||||
"/items/foo/ws", cookies={"session": "fakesession"}
|
||||
) as websocket:
|
||||
with client.websocket_connect("/items/foo/ws") as websocket:
|
||||
message = "Message one"
|
||||
websocket.send_text(message)
|
||||
data = websocket.receive_text()
|
||||
@@ -33,6 +31,7 @@ def test_websocket_with_cookie():
|
||||
|
||||
|
||||
def test_websocket_with_header():
|
||||
client = TestClient(app)
|
||||
with pytest.raises(WebSocketDisconnect):
|
||||
with client.websocket_connect("/items/bar/ws?token=some-token") as websocket:
|
||||
message = "Message one"
|
||||
@@ -50,6 +49,7 @@ def test_websocket_with_header():
|
||||
|
||||
|
||||
def test_websocket_with_header_and_query():
|
||||
client = TestClient(app)
|
||||
with pytest.raises(WebSocketDisconnect):
|
||||
with client.websocket_connect("/items/2/ws?q=3&token=some-token") as websocket:
|
||||
message = "Message one"
|
||||
@@ -71,6 +71,7 @@ def test_websocket_with_header_and_query():
|
||||
|
||||
|
||||
def test_websocket_no_credentials():
|
||||
client = TestClient(app)
|
||||
with pytest.raises(WebSocketDisconnect):
|
||||
with client.websocket_connect("/items/foo/ws"):
|
||||
pytest.fail(
|
||||
@@ -79,6 +80,7 @@ def test_websocket_no_credentials():
|
||||
|
||||
|
||||
def test_websocket_invalid_data():
|
||||
client = TestClient(app)
|
||||
with pytest.raises(WebSocketDisconnect):
|
||||
with client.websocket_connect("/items/foo/ws?q=bar&token=some-token"):
|
||||
pytest.fail(
|
||||
|
||||
Reference in New Issue
Block a user