1 changed files with 38 additions and 18 deletions
@ -1,44 +1,64 @@ |
|||||
import json |
import json |
||||
from typing import Annotated |
from typing import Annotated |
||||
|
|
||||
from fastapi import FastAPI, Form |
from fastapi import Cookie, FastAPI, Form, Header, Query |
||||
from fastapi.testclient import TestClient |
from fastapi.testclient import TestClient |
||||
from pydantic import BaseModel, Json |
from pydantic import Json |
||||
|
|
||||
app = FastAPI() |
app = FastAPI() |
||||
|
|
||||
|
|
||||
class JsonListModel(BaseModel): |
@app.post("/form-json-list") |
||||
json_list: Json[list[str]] |
def form_json_list(items: Annotated[Json[list[str]], Form()]) -> list[str]: |
||||
|
return items |
||||
|
|
||||
|
|
||||
@app.post("/form-str") |
@app.get("/query-json-list") |
||||
def form_str(json_list: Annotated[str, Form()]) -> list[str]: |
def query_json_list(items: Annotated[Json[list[str]], Query()]) -> list[str]: |
||||
model = JsonListModel(json_list=json_list) # type: ignore[arg-type] |
return items |
||||
return model.json_list |
|
||||
|
|
||||
|
|
||||
@app.post("/form-json-list") |
@app.get("/header-json-list") |
||||
def form_json_list(json_list: Annotated[Json[list[str]], Form()]) -> list[str]: |
def header_json_list(x_items: Annotated[Json[list[str]], Header()]) -> list[str]: |
||||
return json_list |
return x_items |
||||
|
|
||||
|
|
||||
|
@app.get("/cookie-json-dict") |
||||
|
def cookie_json_dict( |
||||
|
session: Annotated[Json[dict[str, str]], Cookie()], |
||||
|
) -> dict[str, str]: |
||||
|
return session |
||||
|
|
||||
|
|
||||
client = TestClient(app) |
client = TestClient(app) |
||||
|
|
||||
|
|
||||
def test_form_str(): |
def test_form_json_list(): |
||||
response = client.post( |
response = client.post( |
||||
"/form-str", |
"/form-json-list", data={"items": json.dumps(["abc", "def"])} |
||||
data={"json_list": json.dumps(["abc", "def"])}, |
|
||||
) |
) |
||||
assert response.status_code == 200, response.text |
assert response.status_code == 200, response.text |
||||
assert response.json() == ["abc", "def"] |
assert response.json() == ["abc", "def"] |
||||
|
|
||||
|
|
||||
def test_form_json_list(): |
def test_query_json_list(): |
||||
response = client.post( |
response = client.get( |
||||
"/form-json-list", |
"/query-json-list", params={"items": json.dumps(["abc", "def"])} |
||||
data={"json_list": json.dumps(["abc", "def"])}, |
|
||||
) |
) |
||||
assert response.status_code == 200, response.text |
assert response.status_code == 200, response.text |
||||
assert response.json() == ["abc", "def"] |
assert response.json() == ["abc", "def"] |
||||
|
|
||||
|
|
||||
|
def test_header_json_list(): |
||||
|
response = client.get( |
||||
|
"/header-json-list", headers={"x-items": json.dumps(["abc", "def"])} |
||||
|
) |
||||
|
assert response.status_code == 200, response.text |
||||
|
assert response.json() == ["abc", "def"] |
||||
|
|
||||
|
|
||||
|
def test_cookie_json_dict(): |
||||
|
client.cookies.set("session", json.dumps({"user": "test1", "role": "admin"})) |
||||
|
response = client.get("/cookie-json-dict") |
||||
|
assert response.status_code == 200, response.text |
||||
|
assert response.json() == {"user": "test1", "role": "admin"} |
||||
|
|||||
Loading…
Reference in new issue