Docs and tests, responses with headers and cookies (#185)

This commit is contained in:
Sebastián Ramírez 2019-04-26 15:13:59 +04:00 committed by GitHub
parent 8e3a7699a3
commit 528ef7e079
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,12 @@
from fastapi import FastAPI
from starlette.responses import JSONResponse
app = FastAPI()
@app.post("/cookie/")
def create_cookie():
content = {"message": "Come to the dark side, we have cookies"}
response = JSONResponse(content=content)
response.set_cookie(key="fakesession", value="fake-cookie-session-value")
return response

View File

@ -0,0 +1,11 @@
from fastapi import FastAPI
from starlette.responses import JSONResponse
app = FastAPI()
@app.get("/headers/")
def get_headers():
content = {"message": "Hello World"}
headers = {"X-Cat-Dog": "alone in the world", "Content-Language": "en-US"}
return JSONResponse(content=content, headers=headers)

View File

@ -28,6 +28,12 @@ The middleware function receives:
!!! tip
This technique is used in the tutorial about <a href="https://fastapi.tiangolo.com/tutorial/sql-databases/" target="_blank">SQL (Relational) Databases</a>.
!!! tip
Have in mind that custom proprietary headers can be added <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers" target="_blank">using the 'X-' prefix</a>.
But if you have custom headers that you want a client in a browser to be able to see, you need to add them to your <a href="https://fastapi.tiangolo.com/tutorial/cors/" target="_blank">CORS configurations</a>, using the parameter `expose_headers` documented in <a href="https://www.starlette.io/middleware/#corsmiddleware" target="_blank">Starlette's CORS docs</a>.
### Before and after the `response`
You can add code to be run with the `request`, before any *path operation* receives it.

View File

@ -0,0 +1,13 @@
You can create (set) Cookies in your response.
To do that, you can create a response as described in <a href="https://fastapi.tiangolo.com/tutorial/response-directly/" target="_blank">Return a Response directly</a>.
Then set Cookies in it, and then return it:
```Python hl_lines="10 11 12"
{!./src/response_cookies/tutorial001.py!}
```
## More info
To see all the available parameters and options, check the <a href="https://www.starlette.io/responses/#set-cookie" target="_blank">documentation in Starlette</a>.

View File

@ -0,0 +1,12 @@
You can add headers to your response.
Create a response as described in <a href="https://fastapi.tiangolo.com/tutorial/response-directly/" target="_blank">Return a Response directly</a> and pass the headers as an additional parameter:
```Python hl_lines="10 11 12"
{!./src/response_headers/tutorial001.py!}
```
!!! tip
Have in mind that custom proprietary headers can be added <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers" target="_blank">using the 'X-' prefix</a>.
But if you have custom headers that you want a client in a browser to be able to see, you need to add them to your <a href="https://fastapi.tiangolo.com/tutorial/cors/" target="_blank">CORS configurations</a>, using the parameter `expose_headers` documented in <a href="https://www.starlette.io/middleware/#corsmiddleware" target="_blank">Starlette's CORS docs</a>.

View File

@ -48,6 +48,8 @@ nav:
- Return a Response directly: 'tutorial/response-directly.md'
- Custom Response Class: 'tutorial/custom-response.md'
- Additional Responses in OpenAPI: 'tutorial/additional-responses.md'
- Response Cookies: 'tutorial/response-cookies.md'
- Response Headers: 'tutorial/response-headers.md'
- Dependencies:
- First Steps: 'tutorial/dependencies/first-steps.md'
- Classes as Dependencies: 'tutorial/dependencies/classes-as-dependencies.md'

View File

@ -0,0 +1,12 @@
from starlette.testclient import TestClient
from response_cookies.tutorial001 import app
client = TestClient(app)
def test_path_operation():
response = client.post("/cookie/")
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"

View File

@ -0,0 +1,13 @@
from starlette.testclient import TestClient
from response_headers.tutorial001 import app
client = TestClient(app)
def test_path_operation():
response = client.get("/headers/")
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}
assert response.headers["X-Cat-Dog"] == "alone in the world"
assert response.headers["Content-Language"] == "en-US"