| 1 |
from typing import Optional |
| 2 |
|
| 3 |
import pytest |
| 4 |
from pydantic import ConfigDict |
| 5 |
|
| 6 |
from ninja import Body, Cookie, Header, Router, Schema |
| 7 |
from ninja.testing import TestClient |
| 8 |
|
| 9 |
|
| 10 |
class OptionalEmptySchema(Schema): |
| 11 |
model_config = ConfigDict(extra="forbid") |
| 12 |
name: Optional[str] = None |
| 13 |
|
| 14 |
|
| 15 |
class ExtraForbidSchema(Schema): |
| 16 |
model_config = ConfigDict(extra="forbid") |
| 17 |
name: str |
| 18 |
metadata: Optional[OptionalEmptySchema] = None |
| 19 |
|
| 20 |
|
| 21 |
router = Router() |
| 22 |
|
| 23 |
|
| 24 |
@router.get("/headers1") |
| 25 |
def headers1(request, user_agent: str = Header(...)): |
| 26 |
return user_agent |
| 27 |
|
| 28 |
|
| 29 |
@router.get("/headers2") |
| 30 |
def headers2(request, ua: str = Header(..., alias="User-Agent")): |
| 31 |
return ua |
| 32 |
|
| 33 |
|
| 34 |
@router.get("/headers3") |
| 35 |
def headers3(request, content_length: int = Header(...)): |
| 36 |
return content_length |
| 37 |
|
| 38 |
|
| 39 |
@router.get("/headers4") |
| 40 |
def headers4(request, c_len: int = Header(..., alias="Content-length")): |
| 41 |
return c_len |
| 42 |
|
| 43 |
|
| 44 |
@router.get("/headers5") |
| 45 |
def headers5(request, missing: int = Header(...)): |
| 46 |
return missing |
| 47 |
|
| 48 |
|
| 49 |
@router.get("/cookies1") |
| 50 |
def cookies1(request, weapon: str = Cookie(...)): |
| 51 |
return weapon |
| 52 |
|
| 53 |
|
| 54 |
@router.get("/cookies2") |
| 55 |
def cookies2(request, wpn: str = Cookie(..., alias="weapon")): |
| 56 |
return wpn |
| 57 |
|
| 58 |
|
| 59 |
@router.post("/test-schema") |
| 60 |
def schema(request, payload: ExtraForbidSchema = Body(...)): |
| 61 |
return "ok" |
| 62 |
|
| 63 |
|
| 64 |
client = TestClient(router) |
| 65 |
|
| 66 |
|
| 67 |
@pytest.mark.parametrize( |
| 68 |
"path,expected_status,expected_response", |
| 69 |
[ |
| 70 |
("/headers1", 200, "Ninja"), |
| 71 |
("/headers2", 200, "Ninja"), |
| 72 |
("/headers3", 200, 10), |
| 73 |
("/headers4", 200, 10), |
| 74 |
( |
| 75 |
"/headers5", |
| 76 |
422, |
| 77 |
{ |
| 78 |
"detail": [ |
| 79 |
{ |
| 80 |
"type": "missing", |
| 81 |
"loc": ["header", "missing"], |
| 82 |
"msg": "Field required", |
| 83 |
} |
| 84 |
] |
| 85 |
}, |
| 86 |
), |
| 87 |
("/cookies1", 200, "shuriken"), |
| 88 |
("/cookies2", 200, "shuriken"), |
| 89 |
], |
| 90 |
) |
| 91 |
def test_headers(path, expected_status, expected_response): |
| 92 |
response = client.get( |
| 93 |
path, |
| 94 |
headers={"User-Agent": "Ninja", "Content-Length": "10"}, |
| 95 |
COOKIES={"weapon": "shuriken"}, |
| 96 |
) |
| 97 |
assert response.status_code == expected_status, response.content |
| 98 |
print(response.json()) |
| 99 |
assert response.json() == expected_response |
| 100 |
|
| 101 |
|
| 102 |
@pytest.mark.parametrize( |
| 103 |
"path,json,expected_status,expected_response", |
| 104 |
[ |
| 105 |
( |
| 106 |
"/test-schema", |
| 107 |
{"name": "test", "extra_name": "test2"}, |
| 108 |
422, |
| 109 |
{ |
| 110 |
"detail": [ |
| 111 |
{ |
| 112 |
"type": "extra_forbidden", |
| 113 |
"loc": ["body", "payload", "extra_name"], |
| 114 |
"msg": "Extra inputs are not permitted", |
| 115 |
} |
| 116 |
] |
| 117 |
}, |
| 118 |
), |
| 119 |
( |
| 120 |
"/test-schema", |
| 121 |
{"name": "test", "metadata": {"extra_name": "xxx"}}, |
| 122 |
422, |
| 123 |
{ |
| 124 |
"detail": [ |
| 125 |
{ |
| 126 |
"loc": ["body", "payload", "metadata", "extra_name"], |
| 127 |
"msg": "Extra inputs are not permitted", |
| 128 |
"type": "extra_forbidden", |
| 129 |
} |
| 130 |
] |
| 131 |
}, |
| 132 |
), |
| 133 |
( |
| 134 |
"/test-schema", |
| 135 |
{"name": "test", "metadata": "test2"}, |
| 136 |
422, |
| 137 |
{ |
| 138 |
"detail": [ |
| 139 |
{ |
| 140 |
"type": "model_attributes_type", |
| 141 |
"loc": ["body", "payload", "metadata"], |
| 142 |
"msg": "Input should be a valid dictionary or object to extract fields from", |
| 143 |
} |
| 144 |
] |
| 145 |
}, |
| 146 |
), |
| 147 |
], |
| 148 |
) |
| 149 |
def test_pydantic_config(path, json, expected_status, expected_response): |
| 150 |
|
| 151 |
response = client.post(path, json=json) |
| 152 |
assert response.json() == expected_response |
| 153 |
assert response.status_code == expected_status |