Browse Source

🎨 [pre-commit.ci] Auto format from pre-commit.com hooks

pull/13525/head
pre-commit-ci[bot] 2 months ago
parent
commit
ebfbf18aa3
  1. 4
      docs/en/docs/tutorial/middleware.md
  2. 2
      docs_src/middleware/tutorial002.py
  3. 4
      tests/test_tutorial/test_middleware/test_tutorial001.py
  4. 3
      tests/test_tutorial/test_middleware/test_tutorial002.py

4
docs/en/docs/tutorial/middleware.md

@ -67,7 +67,7 @@ Here we use <a href="https://docs.python.org/3/library/time.html#time.perf_count
### Class-Based Middleware ### Class-Based Middleware
It is possible to define a custom `http` middleware using classes. It is possible to define a custom `http` middleware using classes.
We will implement the class-based middleware with exactly the same functionality as in the example above. We will implement the class-based middleware with exactly the same functionality as in the example above.
@ -75,7 +75,7 @@ We will use an abstract `BaseHTTPMiddleware` class from Starlette that allows to
{* ../../docs_src/middleware/tutorial002.py hl[7:13,17] *} {* ../../docs_src/middleware/tutorial002.py hl[7:13,17] *}
To create the middleware we need to override the `async dispatch` method of the `BaseHTTPMiddleware` class. To create the middleware we need to override the `async dispatch` method of the `BaseHTTPMiddleware` class.
The functionality of the method is very similar to the decorated middleware function from the previous example. The functionality of the method is very similar to the decorated middleware function from the previous example.

2
docs_src/middleware/tutorial002.py

@ -19,4 +19,4 @@ app.add_middleware(ProcessTimeHeaderMiddleware)
@app.get("/") @app.get("/")
def hello(): def hello():
return {"hello": "world"} return {"hello": "world"}

4
tests/test_tutorial/test_middleware/test_tutorial001.py

@ -1,6 +1,7 @@
from docs_src.middleware.tutorial001 import app
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from docs_src.middleware.tutorial001 import app
def test_add_process_time_header_middleware(): def test_add_process_time_header_middleware():
client = TestClient(app) client = TestClient(app)
@ -9,4 +10,3 @@ def test_add_process_time_header_middleware():
assert response.json() == {"hello": "world"} assert response.json() == {"hello": "world"}
assert "X-Process-Time" in response.headers assert "X-Process-Time" in response.headers
assert len(response.headers["X-Process-Time"]) > 0 assert len(response.headers["X-Process-Time"]) > 0

3
tests/test_tutorial/test_middleware/test_tutorial002.py

@ -1,6 +1,7 @@
from docs_src.middleware.tutorial002 import app
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from docs_src.middleware.tutorial002 import app
def test_add_process_time_header_middleware(): def test_add_process_time_header_middleware():
client = TestClient(app) client = TestClient(app)

Loading…
Cancel
Save