| 1 |
import pytest |
| 2 |
from fastapi import Cookie, FastAPI, Header, Path, Query |
| 3 |
from fastapi.testclient import TestClient |
| 4 |
from inline_snapshot import snapshot |
| 5 |
|
| 6 |
app = FastAPI() |
| 7 |
|
| 8 |
|
| 9 |
@app.get("/hidden_cookie") |
| 10 |
async def hidden_cookie( |
| 11 |
hidden_cookie: str | None = Cookie(default=None, include_in_schema=False), |
| 12 |
): |
| 13 |
return {"hidden_cookie": hidden_cookie} |
| 14 |
|
| 15 |
|
| 16 |
@app.get("/hidden_header") |
| 17 |
async def hidden_header( |
| 18 |
hidden_header: str | None = Header(default=None, include_in_schema=False), |
| 19 |
): |
| 20 |
return {"hidden_header": hidden_header} |
| 21 |
|
| 22 |
|
| 23 |
@app.get("/hidden_path/{hidden_path}") |
| 24 |
async def hidden_path(hidden_path: str = Path(include_in_schema=False)): |
| 25 |
return {"hidden_path": hidden_path} |
| 26 |
|
| 27 |
|
| 28 |
@app.get("/hidden_query") |
| 29 |
async def hidden_query( |
| 30 |
hidden_query: str | None = Query(default=None, include_in_schema=False), |
| 31 |
): |
| 32 |
return {"hidden_query": hidden_query} |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.parametrize( |
| 36 |
"path,cookies,expected_status,expected_response", |
| 37 |
[ |
| 38 |
( |
| 39 |
"/hidden_cookie", |
| 40 |
{}, |
| 41 |
200, |
| 42 |
{"hidden_cookie": None}, |
| 43 |
), |
| 44 |
( |
| 45 |
"/hidden_cookie", |
| 46 |
{"hidden_cookie": "somevalue"}, |
| 47 |
200, |
| 48 |
{"hidden_cookie": "somevalue"}, |
| 49 |
), |
| 50 |
], |
| 51 |
) |
| 52 |
def test_hidden_cookie(path, cookies, expected_status, expected_response): |
| 53 |
client = TestClient(app, cookies=cookies) |
| 54 |
response = client.get(path) |
| 55 |
assert response.status_code == expected_status |
| 56 |
assert response.json() == expected_response |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.parametrize( |
| 60 |
"path,headers,expected_status,expected_response", |
| 61 |
[ |
| 62 |
( |
| 63 |
"/hidden_header", |
| 64 |
{}, |
| 65 |
200, |
| 66 |
{"hidden_header": None}, |
| 67 |
), |
| 68 |
( |
| 69 |
"/hidden_header", |
| 70 |
{"Hidden-Header": "somevalue"}, |
| 71 |
200, |
| 72 |
{"hidden_header": "somevalue"}, |
| 73 |
), |
| 74 |
], |
| 75 |
) |
| 76 |
def test_hidden_header(path, headers, expected_status, expected_response): |
| 77 |
client = TestClient(app) |
| 78 |
response = client.get(path, headers=headers) |
| 79 |
assert response.status_code == expected_status |
| 80 |
assert response.json() == expected_response |
| 81 |
|
| 82 |
|
| 83 |
def test_hidden_path(): |
| 84 |
client = TestClient(app) |
| 85 |
response = client.get("/hidden_path/hidden_path") |
| 86 |
assert response.status_code == 200 |
| 87 |
assert response.json() == {"hidden_path": "hidden_path"} |
| 88 |
|
| 89 |
|
| 90 |
@pytest.mark.parametrize( |
| 91 |
"path,expected_status,expected_response", |
| 92 |
[ |
| 93 |
( |
| 94 |
"/hidden_query", |
| 95 |
200, |
| 96 |
{"hidden_query": None}, |
| 97 |
), |
| 98 |
( |
| 99 |
"/hidden_query?hidden_query=somevalue", |
| 100 |
200, |
| 101 |
{"hidden_query": "somevalue"}, |
| 102 |
), |
| 103 |
], |
| 104 |
) |
| 105 |
def test_hidden_query(path, expected_status, expected_response): |
| 106 |
client = TestClient(app) |
| 107 |
response = client.get(path) |
| 108 |
assert response.status_code == expected_status |
| 109 |
assert response.json() == expected_response |
| 110 |
|
| 111 |
|
| 112 |
def test_openapi_schema(): |
| 113 |
client = TestClient(app) |
| 114 |
response = client.get("/openapi.json") |
| 115 |
assert response.status_code == 200 |
| 116 |
assert response.json() == snapshot( |
| 117 |
{ |
| 118 |
"openapi": "3.1.0", |
| 119 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 120 |
"paths": { |
| 121 |
"/hidden_cookie": { |
| 122 |
"get": { |
| 123 |
"summary": "Hidden Cookie", |
| 124 |
"operationId": "hidden_cookie_hidden_cookie_get", |
| 125 |
"responses": { |
| 126 |
"200": { |
| 127 |
"description": "Successful Response", |
| 128 |
"content": {"application/json": {"schema": {}}}, |
| 129 |
}, |
| 130 |
"422": { |
| 131 |
"description": "Validation Error", |
| 132 |
"content": { |
| 133 |
"application/json": { |
| 134 |
"schema": { |
| 135 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 136 |
} |
| 137 |
} |
| 138 |
}, |
| 139 |
}, |
| 140 |
}, |
| 141 |
} |
| 142 |
}, |
| 143 |
"/hidden_header": { |
| 144 |
"get": { |
| 145 |
"summary": "Hidden Header", |
| 146 |
"operationId": "hidden_header_hidden_header_get", |
| 147 |
"responses": { |
| 148 |
"200": { |
| 149 |
"description": "Successful Response", |
| 150 |
"content": {"application/json": {"schema": {}}}, |
| 151 |
}, |
| 152 |
"422": { |
| 153 |
"description": "Validation Error", |
| 154 |
"content": { |
| 155 |
"application/json": { |
| 156 |
"schema": { |
| 157 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 158 |
} |
| 159 |
} |
| 160 |
}, |
| 161 |
}, |
| 162 |
}, |
| 163 |
} |
| 164 |
}, |
| 165 |
"/hidden_path/{hidden_path}": { |
| 166 |
"get": { |
| 167 |
"summary": "Hidden Path", |
| 168 |
"operationId": "hidden_path_hidden_path__hidden_path__get", |
| 169 |
"responses": { |
| 170 |
"200": { |
| 171 |
"description": "Successful Response", |
| 172 |
"content": {"application/json": {"schema": {}}}, |
| 173 |
}, |
| 174 |
"422": { |
| 175 |
"description": "Validation Error", |
| 176 |
"content": { |
| 177 |
"application/json": { |
| 178 |
"schema": { |
| 179 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 180 |
} |
| 181 |
} |
| 182 |
}, |
| 183 |
}, |
| 184 |
}, |
| 185 |
} |
| 186 |
}, |
| 187 |
"/hidden_query": { |
| 188 |
"get": { |
| 189 |
"summary": "Hidden Query", |
| 190 |
"operationId": "hidden_query_hidden_query_get", |
| 191 |
"responses": { |
| 192 |
"200": { |
| 193 |
"description": "Successful Response", |
| 194 |
"content": {"application/json": {"schema": {}}}, |
| 195 |
}, |
| 196 |
"422": { |
| 197 |
"description": "Validation Error", |
| 198 |
"content": { |
| 199 |
"application/json": { |
| 200 |
"schema": { |
| 201 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 202 |
} |
| 203 |
} |
| 204 |
}, |
| 205 |
}, |
| 206 |
}, |
| 207 |
} |
| 208 |
}, |
| 209 |
}, |
| 210 |
"components": { |
| 211 |
"schemas": { |
| 212 |
"HTTPValidationError": { |
| 213 |
"title": "HTTPValidationError", |
| 214 |
"type": "object", |
| 215 |
"properties": { |
| 216 |
"detail": { |
| 217 |
"title": "Detail", |
| 218 |
"type": "array", |
| 219 |
"items": { |
| 220 |
"$ref": "#/components/schemas/ValidationError" |
| 221 |
}, |
| 222 |
} |
| 223 |
}, |
| 224 |
}, |
| 225 |
"ValidationError": { |
| 226 |
"title": "ValidationError", |
| 227 |
"required": ["loc", "msg", "type"], |
| 228 |
"type": "object", |
| 229 |
"properties": { |
| 230 |
"loc": { |
| 231 |
"title": "Location", |
| 232 |
"type": "array", |
| 233 |
"items": { |
| 234 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 235 |
}, |
| 236 |
}, |
| 237 |
"msg": {"title": "Message", "type": "string"}, |
| 238 |
"type": {"title": "Error Type", "type": "string"}, |
| 239 |
"input": {"title": "Input"}, |
| 240 |
"ctx": {"title": "Context", "type": "object"}, |
| 241 |
}, |
| 242 |
}, |
| 243 |
} |
| 244 |
}, |
| 245 |
} |
| 246 |
) |