From 76774ea1358a0363af3d41eda1d8915f2683c67b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Chaves?= Date: Fri, 12 Jul 2024 13:27:32 +0200 Subject: [PATCH] Unset select name if there are no options --- form2request/_base.py | 10 ++++++++- tests/test_main.py | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/form2request/_base.py b/form2request/_base.py index ceeb8f7..ed685b2 100644 --- a/form2request/_base.py +++ b/form2request/_base.py @@ -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 diff --git a/tests/test_main.py b/tests/test_main.py index 82c9788..3e6797d 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -590,6 +590,30 @@ b"", ), ), + # Single-choice select with no options. + ( + "https://example.com", + b"""
""", + {}, + Request( + "https://example.com", + "GET", + [], + b"", + ), + ), + # Single-choice select with no options but with a value. + ( + "https://example.com", + b"""
""", + {}, + Request( + "https://example.com", + "GET", + [], + b"", + ), + ), # Single-choice select with multiple options. The first one is # selected. ( @@ -659,6 +683,30 @@ b"", ), ), + # Multiple-choice select without options. + ( + "https://example.com", + b"""
""", + {}, + Request( + "https://example.com", + "GET", + [], + b"", + ), + ), + # Multiple-choice select without options but with a value. + ( + "https://example.com", + b"""
""", + {}, + Request( + "https://example.com", + "GET", + [], + b"", + ), + ), # Values are URL-encoded, with plus signs instead of spaces. ( "https://example.com",