/
test_tutorial001.py
63 lines (51 loc) · 1.76 KB
/
test_tutorial001.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import importlib
from fastapi.testclient import TestClient
from ...utils import needs_pydanticv2
def get_client() -> TestClient:
from docs_src.conditional_openapi import tutorial001
importlib.reload(tutorial001)
client = TestClient(tutorial001.app)
return client
@needs_pydanticv2
def test_disable_openapi(monkeypatch):
monkeypatch.setenv("OPENAPI_URL", "")
# Load the client after setting the env var
client = get_client()
response = client.get("/openapi.json")
assert response.status_code == 404, response.text
response = client.get("/docs")
assert response.status_code == 404, response.text
response = client.get("/redoc")
assert response.status_code == 404, response.text
@needs_pydanticv2
def test_root():
client = get_client()
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}
@needs_pydanticv2
def test_default_openapi():
client = get_client()
response = client.get("/docs")
assert response.status_code == 200, response.text
response = client.get("/redoc")
assert response.status_code == 200, response.text
response = client.get("/openapi.json")
assert response.json() == {
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/": {
"get": {
"summary": "Root",
"operationId": "root__get",
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
}
},
}
}
},
}