jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1339803?usp=email )
Change subject: http: Avoid mutating request inputs
......................................................................
http: Avoid mutating request inputs
Build request-local header and callback containers so fetch does not add
configured headers or error handling callbacks to caller-owned objects.
Change-Id: I32886fab6d124dd502b2fae828d94ee317908194
---
M pywikibot/comms/http.py
M tests/http_tests.py
2 files changed, 34 insertions(+), 4 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index 7dea9a5..7df0643 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -456,8 +456,7 @@
"""
# Change user agent depending on fake UA settings.
# Set header to new UA if needed.
- headers = headers or {}
- headers.update(config.extra_headers)
+ headers = (headers or {}) | config.extra_headers
def assign_fake_user_agent(use_fake_user_agent, uri):
uri_domain = urlparse(uri).netloc
@@ -488,7 +487,7 @@
else:
headers['user-agent'] = assign_user_agent(headers.get('user-agent'))
- callbacks = kwargs.pop('callbacks', [])
+ callbacks = list(kwargs.pop('callbacks', []))
# error_handling_callback will be executed first.
if default_error_handling:
callbacks.insert(0, error_handling_callback)
diff --git a/tests/http_tests.py b/tests/http_tests.py
index 8d821ce..e6db15d 100755
--- a/tests/http_tests.py
+++ b/tests/http_tests.py
@@ -13,7 +13,7 @@
from contextlib import suppress
from http import HTTPStatus
from platform import python_implementation
-from unittest.mock import patch
+from unittest.mock import Mock, patch
import requests
@@ -232,6 +232,37 @@
self.assertIn('Python/' + str(PYTHON_VERSION[0]), ua)
+class FetchInputTestCase(TestCase):
+
+ """Test input handling by :func:`http.fetch`."""
+
+ net = False
+
+ def test_inputs_are_not_modified(self) -> None:
+ """Test that headers and callbacks remain unchanged."""
+ headers = {'X-Caller': 'value'}
+ callback = Mock()
+ callbacks = [callback]
+ response = requests.Response()
+ response.status_code = HTTPStatus.OK
+
+ with (
+ patch.object(config, 'extra_headers', {'X-Extra': 'configured'}),
+ patch.object(http, '_decide_encoding', return_value='utf-8'),
+ patch.object(http.session, 'request', return_value=response)
+ as request,
+ ):
+ http.fetch('https://example.test', headers=headers,
+ callbacks=callbacks)
+
+ self.assertEqual(headers, {'X-Caller': 'value'})
+ self.assertEqual(callbacks, [callback])
+ request_headers = request.call_args.kwargs['headers']
+ self.assertEqual(request_headers['X-Caller'], 'value')
+ self.assertEqual(request_headers['X-Extra'], 'configured')
+ callback.assert_called_once_with(response)
+
+
@require_modules('fake_useragent')
class LiveFakeUserAgentTestCase(HttpbinTestCase):
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1339803?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I32886fab6d124dd502b2fae828d94ee317908194
Gerrit-Change-Number: 1339803
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]