Skip to content

Commit 5b8788b

Browse files
author
Vladimir Kotal
committed
make call_rest_api() robust in the face of None args
add tests for HTTP header handling
1 parent 4454e30 commit 5b8788b

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
from .patterns import COMMAND_PROPERTY
2929

3030

31+
CONTENT_TYPE = 'Content-Type'
32+
APPLICATION_JSON = 'application/json' # default
33+
34+
3135
def do_api_call(command, uri, verb, headers, data):
3236
verbs = {
3337
'PUT': put,
@@ -53,19 +57,25 @@ def call_rest_api(command, pattern, name):
5357
:param name: command name
5458
:return return value from given requests method
5559
"""
60+
61+
logger = logging.getLogger(__name__)
62+
5663
command = command.get(COMMAND_PROPERTY)
57-
uri = command[0].replace(pattern, name)
64+
if pattern and name:
65+
uri = command[0].replace(pattern, name)
66+
else:
67+
uri = command[0]
68+
5869
verb = command[1]
5970
data = command[2]
71+
6072
try:
6173
headers = command[3]
6274
except IndexError:
6375
headers = {}
6476

65-
logger = logging.getLogger(__name__)
66-
67-
CONTENT_TYPE = 'Content-Type'
68-
APPLICATION_JSON = 'application/json' # default
77+
if not headers:
78+
headers = {}
6979

7080
header_names = [x.lower() for x in headers.keys()]
7181

@@ -82,7 +92,8 @@ def call_rest_api(command, pattern, name):
8292
data = json.dumps(data)
8393
break
8494

85-
data = data.replace(pattern, name)
95+
if pattern and name:
96+
data = data.replace(pattern, name)
8697
logger.debug("entity data: {}".format(data))
8798

8899
logger.debug("{} API call: {} with data '{}' and headers: {}".

opengrok-tools/src/test/python/test_restful.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
#
2525

2626
import pytest
27-
from opengrok_tools.utils.restful import call_rest_api
27+
from opengrok_tools.utils.restful import call_rest_api,\
28+
CONTENT_TYPE, APPLICATION_JSON
2829

2930

3031
def test_replacement(monkeypatch):
@@ -64,3 +65,26 @@ def test_unknown_verb():
6465
value = "BAR"
6566
with pytest.raises(Exception):
6667
call_rest_api(command, pattern, value)
68+
69+
70+
def test_headers(monkeypatch):
71+
"""
72+
Test HTTP header handling.
73+
"""
74+
for verb in ["PUT", "POST", "DELETE"]:
75+
TEXT_PLAIN = {'Content-type': 'text/plain'}
76+
for headers in [TEXT_PLAIN, None]:
77+
command = {"command": ["http://localhost:8080/source/api/v1/foo",
78+
verb, "data", headers]}
79+
80+
def mock_response(command, uri, verb, headers_arg, data):
81+
if headers:
82+
assert TEXT_PLAIN.items() <= headers_arg.items()
83+
else:
84+
assert {CONTENT_TYPE: APPLICATION_JSON}.items() \
85+
<= headers_arg.items()
86+
87+
with monkeypatch.context() as m:
88+
m.setattr("opengrok_tools.utils.restful.do_api_call",
89+
mock_response)
90+
call_rest_api(command, None, None)

0 commit comments

Comments
 (0)