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

fix: remove reserved keywords from kwargs before passing it to requests #117

Merged
merged 4 commits into from
Jul 8, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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