screenshot webhooks: Add support for multipart/form-data fixtures.

Add a common function for webhooks to convert multipart strings to dict.
This facilitates loading a multipart/form-data fixture as a file string,
and converting it.

This will allow testing integrations that use multipart/form-data,
and generating their example screenshots using a script.

Note that this only supports text fields, accommodation for binary files
is not included at the moment.
This commit is contained in:
Niloth P
2024-11-26 20:25:52 +05:30
committed by Tim Abbott
parent f99e1a5dba
commit a0a1f55965
2 changed files with 36 additions and 6 deletions

View File

@@ -249,3 +249,27 @@ def unix_milliseconds_to_timestamp(milliseconds: Any, webhook: str) -> datetime:
raise JsonableError(
_("The {webhook} webhook expects time in milliseconds.").format(webhook=webhook)
)
def parse_multipart_string(body: str) -> dict[str, str]:
"""
Converts multipart/form-data string (fixture) to dict
"""
boundary = body.split("\n")[0][2:]
parts = body.split(f"--{boundary}")
data = {}
for part in parts:
if part.strip() in ["", "--"]:
continue
headers, body = part.split("\n\n", 1)
body = body.removesuffix("\n--")
content_disposition = next(
(line for line in headers.splitlines() if "Content-Disposition" in line), ""
)
field_name = content_disposition.split('name="')[1].split('"')[0]
data[field_name] = body
return data