Skip to content

Commit 26d6156

Browse files
committed
Add docstring and tests describing how to add body to POST request.
1 parent 7645d29 commit 26d6156

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

splunklib/binding.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,10 @@ def post(self, path_segment, owner=None, app=None, sharing=None, headers=None, *
724724
:type headers: ``list`` of 2-tuples.
725725
:param query: All other keyword arguments, which are used as query
726726
parameters.
727-
:type query: ``string``
727+
:param body: Parameters to be used in the post body. If specified,
728+
any parameters in the query will be applied to the URL instead of
729+
the body.
730+
:type body: dict
728731
:return: The response from the server.
729732
:rtype: ``dict`` with keys ``body``, ``headers``, ``reason``,
730733
and ``status``

tests/test_binding.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,47 @@ def test_connect_with_preexisting_token_sans_user_and_pass(self):
808808
socket.write("\r\n".encode('utf-8'))
809809
socket.close()
810810

811+
812+
class TestPostWithBodyParam(unittest.TestCase):
813+
814+
def test_post(self):
815+
def handler(url, message, **kwargs):
816+
assert url == "https://localhost:8089/servicesNS/testowner/testapp/foo/bar"
817+
assert message["body"]["testkey"] == "testvalue"
818+
return splunklib.data.Record({
819+
"status": 200,
820+
"headers": [],
821+
})
822+
ctx = binding.Context(handler=handler)
823+
ctx.post("foo/bar", owner="testowner", app="testapp", body={"testkey": "testvalue"})
824+
825+
def test_post_with_params_and_body(self):
826+
def handler(url, message, **kwargs):
827+
assert url == "https://localhost:8089/servicesNS/testowner/testapp/foo/bar?extrakey=extraval"
828+
assert message["body"]["testkey"] == "testvalue"
829+
return splunklib.data.Record({
830+
"status": 200,
831+
"headers": [],
832+
})
833+
ctx = binding.Context(handler=handler)
834+
ctx.post("foo/bar", extrakey="extraval", owner="testowner", app="testapp", body={"testkey": "testvalue"})
835+
836+
def test_post_with_params_and_no_body(self):
837+
def handler(url, message, **kwargs):
838+
assert url == "https://localhost:8089/servicesNS/testowner/testapp/foo/bar"
839+
assert message["body"] == "extrakey=extraval"
840+
return splunklib.data.Record({
841+
"status": 200,
842+
"headers": [],
843+
})
844+
ctx = binding.Context(handler=handler)
845+
ctx.post("foo/bar", extrakey="extraval", owner="testowner", app="testapp")
846+
847+
def test_with_body_with_full_request(self):
848+
class TestRequestHandler(BaseHTTPRequestHandler):
849+
pass
850+
851+
811852
if __name__ == "__main__":
812853
try:
813854
import unittest2 as unittest

0 commit comments

Comments
 (0)