Skip to content

Commit 16e733a

Browse files
committed
test api_timeout argument passing
fix pre-existing issues with do_api_call() mocking
1 parent 959b34e commit 16e733a

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

tools/src/main/python/opengrok_tools/utils/restful.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def subst(src, substitutions):
141141
return src
142142

143143

144-
def call_rest_api(command, substitutions=None, http_headers=None, timeout=None):
144+
def call_rest_api(command, substitutions=None, http_headers=None, timeout=None, api_timeout=None):
145145
"""
146146
Make RESTful API call. Occurrence of the pattern in the URI
147147
(first part of the command) or data payload will be replaced by the name.
@@ -154,6 +154,7 @@ def call_rest_api(command, substitutions=None, http_headers=None, timeout=None):
154154
data substitution
155155
:param http_headers: optional dictionary of HTTP headers to be appended
156156
:param timeout: optional timeout in seconds for API call response
157+
:param api_timeout: optional timeout in seconds for asynchronous API call
157158
:return value from given requests method
158159
"""
159160

@@ -200,4 +201,4 @@ def call_rest_api(command, substitutions=None, http_headers=None, timeout=None):
200201
data = subst(data, substitutions)
201202
logger.debug("entity data: {}".format(data))
202203

203-
return do_api_call(verb, uri, headers=headers, data=data, timeout=timeout)
204+
return do_api_call(verb, uri, headers=headers, data=data, timeout=timeout, api_timeout=api_timeout)

tools/src/test/python/test_restful.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ def __init__(self):
4949
self.status_code = okay_status
5050
self.raise_for_status = self.p
5151

52-
def mock_do_api_call(verb, uri, headers, data, timeout):
52+
def mock_do_api_call(verb, uri, **kwargs):
5353
# Spying on mocked function is maybe too much so verify
5454
# the arguments here.
5555
assert uri == "http://localhost:8080/source/api/v1/BAR"
56-
assert data == '"fooBARbar"'
56+
assert kwargs['data'] == '"fooBARbar"'
5757

5858
return MockResponse()
5959

@@ -88,7 +88,8 @@ def test_content_type(monkeypatch):
8888
command = {"command": ["http://localhost:8080/source/api/v1/foo",
8989
verb, "data", header_arg]}
9090

91-
def mock_response(uri, verb, headers, data, timeout):
91+
def mock_response(verb, uri, **kwargs):
92+
headers = kwargs['headers']
9293
if header_arg:
9394
assert text_plain_header.items() <= headers.items()
9495
else:
@@ -109,22 +110,25 @@ def test_headers_timeout(monkeypatch):
109110
"""
110111
headers = {'Tatsuo': 'Yasuko'}
111112
expected_timeout = 42
113+
expected_api_timeout = 24
112114
command = {"command": ["http://localhost:8080/source/api/v1/bar",
113115
'GET', "data", headers]}
114116
extra_headers = {'Mei': 'Totoro'}
115117

116-
def mock_do_api_call(uri, verb, headers, data, timeout):
118+
def mock_do_api_call(verb, uri, **kwargs):
117119
all_headers = headers
118120
all_headers.update(extra_headers)
119121
assert headers == all_headers
120-
assert timeout == expected_timeout
122+
assert kwargs['timeout'] == expected_timeout
123+
assert kwargs['api_timeout'] == expected_api_timeout
121124

122125
with monkeypatch.context() as m:
123126
m.setattr("opengrok_tools.utils.restful.do_api_call",
124127
mock_do_api_call)
125128
call_rest_api(command,
126129
http_headers=extra_headers,
127-
timeout=expected_timeout)
130+
timeout=expected_timeout,
131+
api_timeout=expected_api_timeout)
128132

129133

130134
def test_headers_timeout_requests():

0 commit comments

Comments
 (0)