Debian Codegraph @

tests/test_tutorial/test_response_directly/test_tutorial002.py

raw
1 import importlib
2
3 import pytest
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7
8 @pytest.fixture(
9 name="client",
10 params=[
11 pytest.param("tutorial002_py310"),
12 ],
13 )
14 def get_client(request: pytest.FixtureRequest):
15 mod = importlib.import_module(f"docs_src.response_directly.{request.param}")
16
17 client = TestClient(mod.app)
18 return client
19
20
21 def test_path_operation(client: TestClient):
22 expected_content = """<?xml version="1.0"?>
23 <shampoo>
24 <Header>
25 Apply shampoo here.
26 </Header>
27 <Body>
28 You'll have to use soap here.
29 </Body>
30 </shampoo>
31 """
32
33 response = client.get("/legacy/")
34 assert response.status_code == 200, response.text
35 assert response.headers["content-type"] == "application/xml"
36 assert response.text == expected_content
37
38
39 def test_openapi_schema(client: TestClient):
40 response = client.get("/openapi.json")
41 assert response.status_code == 200, response.text
42 assert response.json() == snapshot(
43 {
44 "info": {
45 "title": "FastAPI",
46 "version": "0.1.0",
47 },
48 "openapi": "3.1.0",
49 "paths": {
50 "/legacy/": {
51 "get": {
52 "operationId": "get_legacy_data_legacy__get",
53 "responses": {
54 "200": {
55 "content": {
56 "application/json": {
57 "schema": {},
58 },
59 },
60 "description": "Successful Response",
61 },
62 },
63 "summary": "Get Legacy Data",
64 },
65 },
66 },
67 }
68 )