Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_request_kwargs to check before requesting #339

Merged
merged 1 commit into from Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions mechanicalsoup/browser.py
Expand Up @@ -139,8 +139,18 @@ def post(self, *args, **kwargs):
Browser.add_soup(response, self.soup_config)
return response

def _request(self, form, url=None, **kwargs):
"""Extract input data from the form to pass to a Requests session."""
@staticmethod
def _get_request_kwargs(method, url, **kwargs):
"""This method exists to raise a TypeError when a method or url is
specified in the kwargs.
"""
request_kwargs = {"method": method, "url": url}
request_kwargs.update(kwargs)
return request_kwargs

@classmethod
def get_request_kwargs(cls, form, url=None, **kwargs):
hemberger marked this conversation as resolved.
Show resolved Hide resolved
"""Extract input data from the form."""
method = str(form.get("method", "get"))
action = form.get("action")
url = urllib.parse.urljoin(url, action)
Expand Down Expand Up @@ -240,7 +250,12 @@ def __bool__(self):

files = DictThatReturnsTrue()

return self.session.request(method, url, files=files, **kwargs)
return cls._get_request_kwargs(method, url, files=files, **kwargs)

def _request(self, form, url=None, **kwargs):
"""Extract input data from the form to pass to a Requests session."""
request_kwargs = Browser.get_request_kwargs(form, url, **kwargs)
return self.session.request(**request_kwargs)

def submit(self, form, url=None, **kwargs):
"""Prepares and sends a form request.
Expand Down
34 changes: 34 additions & 0 deletions tests/test_browser.py
Expand Up @@ -38,6 +38,40 @@ def test_submit_online(httpbin):
assert 'MechanicalSoup' in json["headers"]["User-Agent"]


def test_get_request_kwargs(httpbin):
"""Return kwargs without a submit"""
browser = mechanicalsoup.Browser()
page = browser.get(httpbin + "/forms/post")
form = page.soup.form
form.find("input", {"name": "custname"})["value"] = "Philip J. Fry"
request_kwargs = browser.get_request_kwargs(form, page.url)
assert "method" in request_kwargs
assert "url" in request_kwargs
assert "data" in request_kwargs
assert ("custname", "Philip J. Fry") in request_kwargs["data"]


def test_get_request_kwargs_when_method_is_in_kwargs(httpbin):
"""Raise TypeError exception"""
browser = mechanicalsoup.Browser()
page = browser.get(httpbin + "/forms/post")
form = page.soup.form
kwargs = {"method": "post"}
with pytest.raises(TypeError):
browser.get_request_kwargs(form, page.url, **kwargs)


def test_get_request_kwargs_when_url_is_in_kwargs(httpbin):
"""Raise TypeError exception"""
browser = mechanicalsoup.Browser()
page = browser.get(httpbin + "/forms/post")
form = page.soup.form
kwargs = {"url": httpbin + "/forms/post"}
with pytest.raises(TypeError):
# pylint: disable=redundant-keyword-arg
browser.get_request_kwargs(form, page.url, **kwargs)


def test__request(httpbin):
form_html = """
<form method="post" action="{}/post">
Expand Down