Skip to content

Commit

Permalink
Update exception messages and test expectations after adding multipar…
Browse files Browse the repository at this point in the history
…t/form-data support for the GET method
  • Loading branch information
Gallaecio committed Jul 3, 2024
1 parent 6974d95 commit ba5da92
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
11 changes: 8 additions & 3 deletions form2request/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,25 @@ def _enctype(
enctype = enctype.lower()
if enctype not in {"application/x-www-form-urlencoded", "text/plain"}:
raise ValueError(
f"The specified form enctype ({enctype!r}) is not supported."
f"The specified form enctype ({enctype!r}) is not supported "
f"for forms with the POST method."
)
elif click_element is not None and (
enctype := (click_element.get("formenctype") or "").lower()
):
if enctype == "multipart/form-data":
raise NotImplementedError(
f"{click_element} has formenctype set to {enctype!r}, which "
f"form2request does not currently support."
f"form2request does not currently support for forms with the "
f"POST method."
)
elif (
enctype := (form.get("enctype") or "").lower()
) and enctype == "multipart/form-data":
raise NotImplementedError(f"Found unsupported form enctype {enctype!r}.")
raise NotImplementedError(
f"{form} has enctype set to {enctype!r}, which form2request does "
f"not currently support for forms with the POST method."
)
return enctype


Expand Down
55 changes: 33 additions & 22 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,60 +226,71 @@
"foo",
)
),
# multipart/form-data raises a NotImplementedError exception.
*(
(
"https://example.com",
f"""<form enctype="{enctype}"></form>""".encode(),
{},
NotImplementedError,
)
for enctype in ("multipart/form-data",)
# multipart/form-data raises a NotImplementedError exception when the
# method is POST.
(
"https://example.com",
b"""<form enctype="multipart/form-data" method="post"></form>""",
{},
NotImplementedError,
),
# multipart/form-data does work when method is GET (default).
(
"https://example.com",
b"""<form enctype="multipart/form-data">
<input name="a" value="b" /></form>""",
{},
Request(
"https://example.com?a=b",
"GET",
[],
b"",
),
),
# The formenctype from the submit button is taken into account, even if
# it has an unknown value.
(
"https://example.com",
b"""<form enctype="multipart/form-data"><input type="submit"
b"""<form enctype="multipart/form-data" method="post"><input type="submit"
formenctype="application/x-www-form-urlencoded" /></form>""",
{},
Request(
"https://example.com",
"GET",
[],
"POST",
[("Content-Type", "application/x-www-form-urlencoded")],
b"",
),
),
(
"https://example.com",
b"""<form enctype="multipart/form-data"><input type="submit"
b"""<form enctype="multipart/form-data" method="post"><input type="submit"
formenctype="foo" /></form>""",
{},
Request(
"https://example.com",
"GET",
[],
"POST",
[("Content-Type", "application/x-www-form-urlencoded")],
b"",
),
),
(
"https://example.com",
b"""<form enctype="application/x-www-form-urlencoded"><input type="submit"
formenctype="multipart/form-data" /></form>""",
b"""<form enctype="application/x-www-form-urlencoded" method="post">
<input type="submit" formenctype="multipart/form-data" /></form>""",
{},
NotImplementedError,
),
# enctype may be overridden, in which case it raises ValueError for
# both unknown and unsupported values.
# both unknown and unsupported values when method is POST.
(
"https://example.com",
b"""<form></form>""",
b"""<form method="post"></form>""",
{"enctype": "multipart/form-data"},
ValueError,
),
(
"https://example.com",
b"""<form></form>""",
b"""<form method="post"></form>""",
{"enctype": "a"},
ValueError,
),
Expand Down Expand Up @@ -709,8 +720,8 @@
),
(
"https://example.com",
b"""<form enctype="application/x-www-form-urlencoded"><input type="submit"
formenctype="MuLtIpArT/fOrM-dAtA" /></form>""",
b"""<form enctype="application/x-www-form-urlencoded" method="post">
<input type="submit" formenctype="MuLtIpArT/fOrM-dAtA" /></form>""",
{},
NotImplementedError,
),
Expand Down

0 comments on commit ba5da92

Please sign in to comment.