Skip to content

Commit

Permalink
fix: remove reserved keywords from kwargs before passing it to reques…
Browse files Browse the repository at this point in the history
…ts (#117)
  • Loading branch information
pyrooka authored Jul 8, 2021
1 parent 6614a63 commit 6191978
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ disable=
duplicate-code,
missing-module-docstring,
too-many-arguments,
unnecessary-pass
unnecessary-pass,
no-member,

[TYPECHECK]
ignored-classes= responses
Expand Down
6 changes: 6 additions & 0 deletions ibm_cloud_sdk_core/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ def send(self, request: requests.Request, **kwargs) -> DetailedResponse:
# Check to see if the caller specified the 'stream' argument.
stream_response = kwargs.get('stream') or False

# Remove the keys we set manually, don't let the user to overwrite these.
reserved_keys = ['method', 'url', 'headers', 'params', 'cookies']
for key in reserved_keys:
if key in kwargs:
del kwargs[key]
logging.warning('"%s" has been removed from the request', key)
try:
response = self.http_client.request(**request,
cookies=self.jar,
Expand Down
24 changes: 24 additions & 0 deletions test/test_base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,30 @@ def test_user_agent_header():
assert response.get_result().request.headers.__getitem__(
'user-agent') == user_agent_header['User-Agent']

@responses.activate
def test_reserved_keys(caplog):
service = AnyServiceV1('2021-07-02', authenticator=NoAuthAuthenticator())
responses.add(
responses.GET,
'https://gateway.watsonplatform.net/test/api',
status=200,
body='some text')
prepped = service.prepare_request('GET', url='', headers={'key': 'OK'})
response = service.send(
prepped,
headers={'key': 'bad'},
method='POST',
url='localhost',
cookies=None,
hooks={'response': []})
assert response.get_result().request.headers.__getitem__('key') == 'OK'
assert response.get_result().request.url == 'https://gateway.watsonplatform.net/test/api'
assert response.get_result().request.method == 'GET'
assert response.get_result().request.hooks == {'response': []}
assert caplog.record_tuples[0][2] == '"method" has been removed from the request'
assert caplog.record_tuples[1][2] == '"url" has been removed from the request'
assert caplog.record_tuples[2][2] == '"headers" has been removed from the request'
assert caplog.record_tuples[3][2] == '"cookies" has been removed from the request'

def test_files_dict():
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
Expand Down

0 comments on commit 6191978

Please sign in to comment.