mirror of
https://github.com/zulip/zulip.git
synced 2025-11-17 20:41:46 +00:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user