From a27ff2223187f70a63ec75b7cd4f2e752aa63122 Mon Sep 17 00:00:00 2001 From: Thierry Date: Sun, 14 Mar 2021 22:47:10 +0100 Subject: [PATCH] Add a wrapper to PUT (#359) Co-authored-by: Thierry GUIBERT --- mechanicalsoup/browser.py | 12 ++++++++++++ tests/test_browser.py | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/mechanicalsoup/browser.py b/mechanicalsoup/browser.py index 255dd6d..3407dfa 100644 --- a/mechanicalsoup/browser.py +++ b/mechanicalsoup/browser.py @@ -160,6 +160,18 @@ def post(self, *args, **kwargs): Browser.add_soup(response, self.soup_config) return response + def put(self, *args, **kwargs): + """Straightforward wrapper around `requests.Session.put + `__. + + :return: `requests.Response + `__ + object with a *soup*-attribute added by :func:`add_soup`. + """ + response = self.session.put(*args, **kwargs) + Browser.add_soup(response, self.soup_config) + return response + @staticmethod def _get_request_kwargs(method, url, **kwargs): """This method exists to raise a TypeError when a method or url is diff --git a/tests/test_browser.py b/tests/test_browser.py index 293dbf9..bb17929 100644 --- a/tests/test_browser.py +++ b/tests/test_browser.py @@ -299,6 +299,13 @@ def test_post(httpbin): assert resp.status_code == 200 and resp.json()['form'] == data +def test_put(httpbin): + browser = mechanicalsoup.Browser() + data = {'color': 'blue', 'colorblind': 'True'} + resp = browser.put(httpbin + "/put", data) + assert resp.status_code == 200 and resp.json()['form'] == data + + @pytest.mark.parametrize("http_html_expected_encoding", [ pytest.param((None, 'utf-8', 'utf-8')), pytest.param(('utf-8', 'utf-8', 'utf-8')),