Skip to content

Commit

Permalink
Add get_request_kwargs() to check before submitting (#339)
Browse files Browse the repository at this point in the history
Previously, users had no way to check what request data is
sent to the server before submitting a form. By calling
get_request_kwargs(), the user can see the request data
without having to send to.
  • Loading branch information
kumarstack55 committed Nov 11, 2020
1 parent 5dc6008 commit e6da833
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
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):
"""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

0 comments on commit e6da833

Please sign in to comment.