Skip to content

Commit d5e287f

Browse files
authored
Merge pull request #389 from MarketSquare/378-file-descriptor-not-closed-for-file-parameter
fix: files descriptors not closed in files param #378
2 parents 01cab99 + fc6bc90 commit d5e287f

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/RequestsLibrary/RequestsKeywords.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ def _common_request(
4747

4848
self.last_response = resp
4949

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

5556
return resp
5657

@@ -59,7 +60,7 @@ def _merge_url(session, uri):
5960
"""
6061
Helper method that join session url and request url.
6162
It relies on urljoin that handles quite good join urls and multiple /
62-
but has some counter intuitive behaviours if you join uri starting with /
63+
but has some counterintuitive behaviours if you join uri starting with /
6364
It handles also override in case a full url (http://etc) is passed as uri.
6465
"""
6566
base = ''

src/RequestsLibrary/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = '0.9.6'
1+
VERSION = '0.9.7'

utests/test_RequestsOnSessionKeywords.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import os
22

33
from RequestsLibrary import RequestsLibrary
4-
from utests import mock
5-
64
from utests import SCRIPT_DIR
5+
from utests import mock
76

87

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

2524

25+
def test_common_request_files_descriptor_closing_when_passed_as_files_param():
26+
session, m_common_request = build_mocked_session_common_request()
27+
with open(os.path.join(SCRIPT_DIR, '../atests/randombytes.bin'), 'rb') as f1:
28+
with open(os.path.join(SCRIPT_DIR, '../atests/data.json'), 'rb') as f2:
29+
m_common_request('get', session,
30+
'http://mocking.rules', files={'randombytes': f1, 'data': f2})
31+
assert f1.closed is True
32+
assert f2.closed is True
33+
34+
2635
def test_common_request_verify_override_true():
2736
session, m_common_request = build_mocked_session_common_request(verify=False)
2837
m_common_request('get', session, '/', verify=True)
@@ -68,22 +77,26 @@ def test_common_request_with_cookies_default_only():
6877
m_common_request('get', session, '/')
6978
session.get.assert_called_with('http://mocking.rules/', timeout=None, cookies={'a': 1, 'b': 2})
7079

80+
7181
def test_common_request_with_float_timeout():
7282
session, m_common_request = build_mocked_session_common_request(timeout=123.4)
7383
m_common_request('get', session, '/')
7484
session.get.assert_called_with('http://mocking.rules/', timeout=123.4, cookies={})
7585

86+
7687
def test_common_request_with_float_timeout_override():
7788
session, m_common_request = build_mocked_session_common_request(timeout=None)
7889
m_common_request('get', session, '/', timeout=123.4)
7990
session.get.assert_called_with('http://mocking.rules/', timeout=123.4, cookies={})
8091

92+
8193
def test_common_request_with_touple_timeout():
8294
session, m_common_request = build_mocked_session_common_request(timeout=(123.4, 432.1))
8395
m_common_request('get', session, '/')
8496
session.get.assert_called_with('http://mocking.rules/', timeout=(123.4, 432.1), cookies={})
8597

98+
8699
def test_common_request_with_touple_timeout_override():
87100
session, m_common_request = build_mocked_session_common_request(timeout=None)
88101
m_common_request('get', session, '/', timeout=(123.4, 432.1))
89-
session.get.assert_called_with('http://mocking.rules/', timeout=(123.4, 432.1), cookies={})
102+
session.get.assert_called_with('http://mocking.rules/', timeout=(123.4, 432.1), cookies={})

0 commit comments

Comments
 (0)