Skip to content

Commit

Permalink
add json/data merging
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien Coelho committed Aug 10, 2024
1 parent 0c69384 commit 548e3ab
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
12 changes: 7 additions & 5 deletions FlaskTester.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,13 @@ def request(self, method: str, path: str, status: int|None = None, content: str|
cookies.update(kwargs["cookies"])
del kwargs["cookies"]

# FIXME allow or forbid?
# if "json" in kwargs and "data" in kwargs:
# log.warning("mix of json and data parameters in request")
# this is forbidden by Flask client
if "json" in kwargs and "data" in kwargs:
# merge into data to possibly keep uploads
kwargs["data"].update(kwargs["json"])
del kwargs["json"]

# convert json parameters
# convert json parameters to json
if "json" in kwargs:
json_param = kwargs["json"]
assert isinstance(json_param, dict)
Expand All @@ -368,7 +370,7 @@ def request(self, method: str, path: str, status: int|None = None, content: str|
else: # pydantic or standard dataclasses?
json_param[name] = dataclasses.asdict(val)

# convert data parameters
# convert data parameters to simple strings
if "data" in kwargs:
data_param = kwargs["data"]
assert isinstance(data_param, dict)
Expand Down
1 change: 1 addition & 0 deletions docs/versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Improve comments.
Activate _Python 3.13_ and _Pypy 3.10_ in GitHub CI.
Restrict CI to _main_ branch.
Add explicit `bcrypt` dependency for tests.
Allow mixing `json` and `data` parameters by merging into `data`.

## 4.2 on 2024-07-28

Expand Down
6 changes: 5 additions & 1 deletion tests/app2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def create_app():
app.get_user_pass(PASSDB.get)
app.group_check("admin", ADMIN.__contains__)

# 3 routes
# 4 routes
@app.get("/open", authorize="OPEN")
def get_no_auth(lang: fsa.Cookie = "de"):
return fsa.jsonify(HELLO.get(lang, "Hey"))
Expand All @@ -29,4 +29,8 @@ def get_authenticated(user: fsa.CurrentUser, lang: fsa.Cookie = "de"):
def get_only_admin():
return fsa.jsonify("Salut administrateur !")

@app.get("/add", authorize="OPEN")
def get_add(i: int, j: int):
return {"sum": i + j}

return app
9 changes: 9 additions & 0 deletions tests/test_app2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ def test_something(app):
app.get("/open", 200, "Hello", login="calvin", auth="none")
app.get("/open", 200, "Bonjour", login="hobbes", auth="none")
app.get("/open", 200, "Guten Tag", login=None)

def test_params(app):
res = app.get("/add", 200, data={"i": 39, "j": 3}, login=None)
assert res.is_json and res.json["sum"] == 42
res = app.get("/add", 200, json={"i": 35, "j": 7}, login=None)
assert res.is_json and res.json["sum"] == 42
# mixing data/json is okay for FlaskTester
res = app.get("/add", 200, data={"i": 30}, json={"j": 12}, login=None)
assert res.is_json and res.json["sum"] == 42

0 comments on commit 548e3ab

Please sign in to comment.