Skip to content

Commit

Permalink
Complete test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio committed Jun 13, 2024
1 parent 6be6568 commit 94edaed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
20 changes: 20 additions & 0 deletions tests/test_page_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,26 @@ def test_http_request_from_form_select_no_selected() -> None:
assert request.body == b"query=&bar=code&baz=ooka"


def test_http_request_from_form_select_no_options() -> None:
url = "https://example.com"
response = HttpResponse(
url,
b"""
<!doctype html>
<title>a</title>
<form id="search-form" accept-charset="utf-8" action="/search" method="POST">
<input type="text" value="" name="query">
<select name="bar"></select>
<input type="submit">
<input type="hidden" name="baz" value="ooka">
</form>
""",
)
form_selector = response.css("#search-form")[0]
request = HttpRequest.from_form(form_selector.root)
assert request.body == b"query=&baz=ooka"


def test_http_request_from_form_no_method() -> None:
url = "https://example.com"
response = HttpResponse(
Expand Down
18 changes: 8 additions & 10 deletions web_poet/page_inputs/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,21 @@ def _is_listlike(x: Any) -> bool:


def _value(
form: Union[InputElement, SelectElement, TextareaElement]
element: Union[InputElement, SelectElement, TextareaElement]
) -> Tuple[Optional[str], Union[None, str, MultipleSelectOptions]]:
if form.tag == "select":
return _select_value(cast(SelectElement, form), form.name, form.value)
return form.name, form.value
if element.tag == "select":
return _select_value(cast(SelectElement, element), element.name, element.value)
return element.name, element.value


def _select_value(
form: SelectElement,
element: SelectElement,
name: Optional[str],
value: Union[None, str, MultipleSelectOptions],
) -> Tuple[Optional[str], Union[None, str, MultipleSelectOptions]]:
if value is None and not form.multiple:
# Match browser behavior on simple select tag without options selected
# And for select tags without options
options = form.value_options
return (name, options[0]) if options else (None, None)
if value is None and not element.multiple:
# Match browser behavior on select tags without options
return (None, None)
return name, value


Expand Down

0 comments on commit 94edaed

Please sign in to comment.