mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-03 13:13:54 -04:00
✨ Add support for Response parameters to set headers, cookies, and status codes (#294)
* ✨ Add support for declaring a Response parameter to set headers and cookies * ✅ Add source for docs and tests * 📝 Add docs for setting headers, cookies and status code * 📝 Add attribution to Hug for inspiring response parameters
This commit is contained in:
committed by
GitHub
parent
c8eea09664
commit
5f7fe926ab
@@ -0,0 +1,15 @@
|
||||
from starlette.testclient import TestClient
|
||||
|
||||
from response_change_status_code.tutorial001 import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_path_operation():
|
||||
response = client.put("/get-or-create-task/foo")
|
||||
print(response.content)
|
||||
assert response.status_code == 200
|
||||
assert response.json() == "Listen to the Bar Fighters"
|
||||
response = client.put("/get-or-create-task/bar")
|
||||
assert response.status_code == 201
|
||||
assert response.json() == "This didn't exist before"
|
||||
@@ -0,0 +1,12 @@
|
||||
from starlette.testclient import TestClient
|
||||
|
||||
from response_cookies.tutorial002 import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_path_operation():
|
||||
response = client.post("/cookie-and-object/")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"message": "Come to the dark side, we have cookies"}
|
||||
assert response.cookies["fakesession"] == "fake-cookie-session-value"
|
||||
@@ -0,0 +1,12 @@
|
||||
from starlette.testclient import TestClient
|
||||
|
||||
from response_headers.tutorial002 import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_path_operation():
|
||||
response = client.get("/headers-and-object/")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"message": "Hello World"}
|
||||
assert response.headers["X-Cat-Dog"] == "alone in the world"
|
||||
Reference in New Issue
Block a user