Skip to content

Commit

Permalink
Unset select name if there are no options
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio committed Jul 12, 2024
1 parent 5a210a1 commit 76774ea
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
10 changes: 9 additions & 1 deletion form2request/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,15 @@ def _data(
)
values: list[FormdataKVType] = [
(k, "" if v is None else v)
for k, v in ((e.name, e.value) for e in inputs)
for k, v in (
(
# Unset name for selects without options.
(None, None)
if e.tag == "select" and not e.value_options
else (e.name, e.value)
)
for e in inputs
)
if k and k not in keys
]
items = data.items() if isinstance(data, dict) else data
Expand Down
48 changes: 48 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,30 @@
b"",
),
),
# Single-choice select with no options.
(
"https://example.com",
b"""<form><select name="a"></select></form>""",
{},
Request(
"https://example.com",
"GET",
[],
b"",
),
),
# Single-choice select with no options but with a value.
(
"https://example.com",
b"""<form><select name="a" value="b"></select></form>""",
{},
Request(
"https://example.com",
"GET",
[],
b"",
),
),
# Single-choice select with multiple options. The first one is
# selected.
(
Expand Down Expand Up @@ -659,6 +683,30 @@
b"",
),
),
# Multiple-choice select without options.
(
"https://example.com",
b"""<form><select multiple name="a"></select></form>""",
{},
Request(
"https://example.com",
"GET",
[],
b"",
),
),
# Multiple-choice select without options but with a value.
(
"https://example.com",
b"""<form><select multiple name="a" value="b"></select></form>""",
{},
Request(
"https://example.com",
"GET",
[],
b"",
),
),
# Values are URL-encoded, with plus signs instead of spaces.
(
"https://example.com",
Expand Down

0 comments on commit 76774ea

Please sign in to comment.