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: files descriptors not closed in files param #378 #389

Merged
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
11 changes: 6 additions & 5 deletions src/RequestsLibrary/RequestsKeywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ def _common_request(

self.last_response = resp

# file descriptors should be closed for files parameter as well
data = kwargs.get('data', None)
if is_file_descriptor(data):
data.close()
files = kwargs.get('files', {}) or {}
data = kwargs.get('data', []) or []
files_descriptor_to_close = filter(is_file_descriptor, list(files.values()) + [data])
for file_descriptor in files_descriptor_to_close:
file_descriptor.close()

return resp

Expand All @@ -59,7 +60,7 @@ def _merge_url(session, uri):
"""
Helper method that join session url and request url.
It relies on urljoin that handles quite good join urls and multiple /
but has some counter intuitive behaviours if you join uri starting with /
but has some counterintuitive behaviours if you join uri starting with /
It handles also override in case a full url (http://etc) is passed as uri.
"""
base = ''
Expand Down
2 changes: 1 addition & 1 deletion src/RequestsLibrary/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.9.6'
VERSION = '0.9.7'
19 changes: 16 additions & 3 deletions utests/test_RequestsOnSessionKeywords.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import os

from RequestsLibrary import RequestsLibrary
from utests import mock

from utests import SCRIPT_DIR
from utests import mock


def build_mocked_session_common_request(alias='alias', url='http://mocking.rules',
Expand All @@ -23,6 +22,16 @@ def test_common_request_file_descriptor_closing():
assert f.closed is True


def test_common_request_files_descriptor_closing_when_passed_as_files_param():
session, m_common_request = build_mocked_session_common_request()
with open(os.path.join(SCRIPT_DIR, '../atests/randombytes.bin'), 'rb') as f1:
with open(os.path.join(SCRIPT_DIR, '../atests/data.json'), 'rb') as f2:
m_common_request('get', session,
'http://mocking.rules', files={'randombytes': f1, 'data': f2})
assert f1.closed is True
assert f2.closed is True


def test_common_request_verify_override_true():
session, m_common_request = build_mocked_session_common_request(verify=False)
m_common_request('get', session, '/', verify=True)
Expand Down Expand Up @@ -68,22 +77,26 @@ def test_common_request_with_cookies_default_only():
m_common_request('get', session, '/')
session.get.assert_called_with('http://mocking.rules/', timeout=None, cookies={'a': 1, 'b': 2})


def test_common_request_with_float_timeout():
session, m_common_request = build_mocked_session_common_request(timeout=123.4)
m_common_request('get', session, '/')
session.get.assert_called_with('http://mocking.rules/', timeout=123.4, cookies={})


def test_common_request_with_float_timeout_override():
session, m_common_request = build_mocked_session_common_request(timeout=None)
m_common_request('get', session, '/', timeout=123.4)
session.get.assert_called_with('http://mocking.rules/', timeout=123.4, cookies={})


def test_common_request_with_touple_timeout():
session, m_common_request = build_mocked_session_common_request(timeout=(123.4, 432.1))
m_common_request('get', session, '/')
session.get.assert_called_with('http://mocking.rules/', timeout=(123.4, 432.1), cookies={})


def test_common_request_with_touple_timeout_override():
session, m_common_request = build_mocked_session_common_request(timeout=None)
m_common_request('get', session, '/', timeout=(123.4, 432.1))
session.get.assert_called_with('http://mocking.rules/', timeout=(123.4, 432.1), cookies={})
session.get.assert_called_with('http://mocking.rules/', timeout=(123.4, 432.1), cookies={})
Loading