diff --git a/CHANGELOG.md b/CHANGELOG.md
index 45f88f519..6934cee08 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,4 @@
-# Splunk SDK for Python Changelog
+# Splunk Enterprise SDK for Python Changelog
## Version 1.6.14
@@ -51,9 +51,9 @@
### Changes
-* Updated the Splunk SDK for Python to work with the Python 3 version of Splunk Enterprise on Windows
+* Updated the Splunk Enterprise SDK for Python to work with the Python 3 version of Splunk Enterprise on Windows
* Improved the performance of deleting/updating an input
-* Added logging to custom search commands app to showcase how to do logging in custom search commands by using the Splunk SDK for Python
+* Added logging to custom search commands app to showcase how to do logging in custom search commands by using the Splunk Enterprise SDK for Python
## Version 1.6.6
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 36511355a..6de8fccd8 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -2,19 +2,19 @@
## How to contribute
-If you would like to contribute to this project, see [Contributions to Splunk][indivcontrib] for more information.
+If you would like to contribute to this project, see [Contributions to Splunk](https://www.splunk.com/en_us/form/contributions.html) for more information.
-## Issues & Bug Reports
+## Issues and bug reports
-If you're seeing some unexpected behavior with this project, please create an [issue on GitHub][issues] with the following information:
+If you're seeing some unexpected behavior with this project, please create an [issue](https://github.com/splunk/splunk-sdk-python/issues) on GitHub with the following information:
1. Version of this project you're using (ex: 1.5.0)
-1. Platform version (ex: Windows Server 2012 R2)
-1. Framework version (ex: Python 2.7.9)
-1. Splunk version (ex: 6.3.0)
-1. Other relevant information (ex: local/remote environment, Splunk network configuration)
+2. Platform version (ex: Windows Server 2012 R2)
+3. Framework version (ex: Python 3.7)
+4. Splunk Enterprise version (ex: 8.0)
+5. Other relevant information (ex: local/remote environment, Splunk network configuration, standalone or distributed deployment, are load balancers used)
-Alternatively, if you have a Splunk question please ask on [Splunk Answers][answers]
+Alternatively, if you have a Splunk question please ask on [Splunk Answers](https://community.splunk.com/t5/Splunk-Development/ct-p/developer-tools).
## Pull requests
@@ -22,22 +22,14 @@ We love to see pull requests!
To create a pull request:
-1. Fill out the [Individual Contributor Agreement][indivcontrib].
-1. Fork [the repository][repo].
-1. Make changes to the **`develop`** branch, preferably with tests.
-1. Create a [pull request][pulls] against the **`develop`** branch.
+1. Fill out the [Individual Contributor Agreement](https://www.splunk.com/en_us/form/contributions.html).
+2. Fork the [repository](https://github.com/splunk/splunk-sdk-python).
+3. Make changes to the **develop** branch, preferably with tests.
+4. Create a [pull request](https://github.com/splunk/splunk-sdk-python/pulls) against the **develop** branch.
## Contact us
-You can [contact support][contact] if you have Splunk related questions.
+If you have a paid Splunk Enterprise or Splunk Cloud license, you can contact [Support](https://www.splunk.com/en_us/support-and-services.html) with questions.
-You can reach the Developer Platform team at _devinfo@splunk.com_.
+You can reach the Splunk Developer Platform team at _devinfo@splunk.com_.
-[contributions]: http://dev.splunk.com/view/opensource/SP-CAAAEDM
-[indivcontrib]: http://dev.splunk.com/goto/individualcontributions
-[companycontrib]: http://dev.splunk.com/view/companycontributions/SP-CAAAEDR
-[answers]: http://answers.splunk.com/
-[repo]: https://github.com/splunk/splunk-sdk-python
-[issues]: https://github.com/splunk/splunk-sdk-python/issues
-[pulls]: https://github.com/splunk/splunk-sdk-python/pulls
-[contact]: https://www.splunk.com/en_us/support-and-services.html
diff --git a/README.md b/README.md
index 22a06ed4e..34c5e5fe8 100644
--- a/README.md
+++ b/README.md
@@ -1,80 +1,62 @@
[](https://travis-ci.org/splunk/splunk-sdk-python)
[](https://splunk-python-sdk.readthedocs.io/en/latest/?badge=latest)
-# The Splunk Software Development Kit for Python
+# The Splunk Enterprise Software Development Kit for Python
#### Version 1.6.14
-The Splunk Software Development Kit (SDK) for Python contains library code and
-examples designed to enable developers to build applications using Splunk.
+The Splunk Enterprise Software Development Kit (SDK) for Python contains library code and examples designed to enable developers to build applications using the Splunk platform.
-Splunk is a search engine and analytic environment that uses a distributed
-map-reduce architecture to efficiently index, search and process large
-time-varying data sets.
+The Splunk platform is a search engine and analytic environment that uses a distributed map-reduce architecture to efficiently index, search, and process large time-varying data sets.
-The Splunk product is popular with system administrators for aggregation and
-monitoring of IT machine data, security, compliance and a wide variety of other
-scenarios that share a requirement to efficiently index, search, analyze and
-generate real-time notifications from large volumes of time series data.
+The Splunk platform is popular with system administrators for aggregation and monitoring of IT machine data, security, compliance, and a wide variety of other scenarios that share a requirement to efficiently index, search, analyze, and generate real-time notifications from large volumes of time-series data.
-The Splunk developer platform enables developers to take advantage of the same
-technology used by the Splunk product to build exciting new applications that
-are enabled by Splunk's unique capabilities.
+The Splunk developer platform enables developers to take advantage of the same technology used by the Splunk platform to build exciting new applications.
## Getting started with the Splunk SDK for Python
-The Splunk SDK for Python contains library code and examples that show how to
-programmatically interact with Splunk for a variety of scenarios including
-searching, saved searches, data inputs, and many more, along with building
-complete applications.
-The information in this Readme provides steps to get going quickly, but for more
-in-depth information be sure to visit the
-[Splunk Developer Portal](http://dev.splunk.com/view/SP-CAAAEBB).
-### Requirements
+## Get started with the Splunk Enterprise SDK for Python
+
+The Splunk Enterprise SDK for Python contains library code and examples that show how to programmatically interact with the Splunk platform for a variety of scenarios including searching, saved searches, data inputs, and many more, along with building complete applications.
-Here's what you need to get going with the Splunk SDK for Python.
+### Requirements
-#### Python
+Here's what you need to get going with the Splunk Enterprise SDK for Python.
-The Splunk SDK for Python requires Python 2.7+, including Python 3. The Splunk SDK for Python has been tested with Python v2.7 and v3.5.
+* Python 2.7+ or Python 3.7.
+
+ The Splunk Enterprise SDK for Python has been tested with Python v2.7 and v3.7.
-#### Splunk
+* Splunk Enterprise
-If you haven't already installed Splunk, download it
-[here](http://www.splunk.com/download). For more about installing and running
-Splunk and system requirements, see
-[Installing & Running Splunk](http://dev.splunk.com/view/SP-CAAADRV). The Splunk SDK for Python has been tested with Splunk Enterprise 7.3 and 8.0.
+ If you haven't already installed Splunk Enterprise, download it [here](http://www.splunk.com/download).
+ For more information, see the Splunk Enterprise [_Installation Manual_](https://docs.splunk.com/Documentation/Splunk/latest/Installation).
-You can also run `SPLUNK_VERSION=(VERSION) make up` to run a version of [Splunk from DockerHub](https://hub.docker.com/r/splunk/splunk/).
+* Splunk Enterprise SDK for Python
-#### Splunk SDK for Python
-Get the Splunk SDK for Python; [download the SDK as a ZIP](http://dev.splunk.com/view/SP-CAAAEBB)
-and extract the files. Or, if you want to contribute to the SDK, clone the
-repository from [GitHub](https://github.com/splunk/splunk-sdk-python).
+ Get the Splunk Enterprise SDK for Python from [PyPI](https://pypi.org/project/splunk-sdk/). If you want to contribute to the SDK, clone the repository from [GitHub](https://github.com/splunk/splunk-sdk-python).
+### Install the SDK
-### Installing the SDK
+Use the following commands to install the Splunk Enterprise SDK for Python libraries in different ways. However, it's not necessary to install the libraries to run the examples and unit tests from the SDK.
-You can install the Splunk SDK for Python libraries by using `easy_install` or `pip`:
+Use `easy_install`:
[sudo] easy_install splunk-sdk
-Or
+Use `pip`:
[sudo] pip install splunk-sdk
-Or to install the Python egg
+Install the Python egg:
[sudo] pip install --egg splunk-sdk
-Alternatively, you can use **setup.py** on the sources you cloned from GitHub:
+Install the sources you cloned from GitHub:
[sudo] python setup.py install
-However, it's not necessary to install the libraries to run the
-examples and unit tests from the SDK.
-
## Testing Quickstart
You'll need `docker` and `docker-compose` to get up and running using this method.
@@ -87,43 +69,32 @@ make test
make down
```
-### Running the examples and unit tests
-
-To run the examples and unit tests, you must put the root of
-the SDK on your PYTHONPATH. For example, if you have downloaded the SDK to your
-home folder and are running OS X or Linux, add the following line to your
-**.bash_profile**:
+To run the examples and unit tests, you must put the root of the SDK on your PYTHONPATH. For example, if you downloaded the SDK to your home folder and are running OS X or Linux, add the following line to your **.bash_profile** file:
export PYTHONPATH=~/splunk-sdk-python
-The SDK command-line examples require a common set of arguments
-that specify things like the Splunk host, port, and login credentials. For a
-full list of command-line arguments, include `--help` as an argument to any of
-the examples.
+The SDK command-line examples require a common set of arguments that specify the host, port, and login credentials for Splunk Enterprise. For a full list of command-line arguments, include `--help` as an argument to any of the examples.
+
+#### Create a .splunkrc convenience file
-#### .splunkrc
+To connect to Splunk Enterprise, many of the SDK examples and unit tests take command-line arguments that specify values for the host, port, and login credentials for Splunk Enterprise. For convenience during development, you can store these arguments as key-value pairs in a text file named **.splunkrc**. Then, the SDK examples and unit tests use the values from the **.splunkrc** file when you don't specify them.
-To connect to Splunk, many of the SDK examples and unit tests take command-line
-arguments that specify values for the host, port, and login credentials for
-Splunk. For convenience during development, you can store these arguments as
-key-value pairs in a text file named **.splunkrc**. Then, the SDK examples and
-unit tests use the values from the **.splunkrc** file when you don't specify
-them.
+>**Note**: Storing login credentials in the **.splunkrc** file is only for convenience during development. This file isn't part of the Splunk platform and shouldn't be used for storing user credentials for production. And, if you're at all concerned about the security of your credentials, enter them at the command line rather than saving them in this file.
To use this convenience file, create a text file with the following format:
- # Splunk host (default: localhost)
+ # Splunk Enterprise host (default: localhost)
host=localhost
- # Splunk admin port (default: 8089)
+ # Splunk Enterprise admin port (default: 8089)
port=8089
- # Splunk username
+ # Splunk Enterprise username
username=admin
- # Splunk password
+ # Splunk Enterprise password
password=changeme
# Access scheme (default: https)
scheme=https
- # Your version of Splunk (default: 5.0)
- version=5.0
+ # Your version of Splunk Enterprise
+ version=8.0
Save the file as **.splunkrc** in the current user's home directory.
@@ -135,32 +106,19 @@ Save the file as **.splunkrc** in the current user's home directory.
C:\Users\currentusername\.splunkrc
- You might get errors in Windows when you try to name the file because
- ".splunkrc" looks like a nameless file with an extension. You can use
- the command line to create this file—go to the
- **C:\Users\currentusername** directory and enter the following command:
+ You might get errors in Windows when you try to name the file because ".splunkrc" appears to be a nameless file with an extension. You can use the command line to create this file by going to the **C:\Users\\<currentusername>** directory and entering the following command:
Notepad.exe .splunkrc
Click **Yes**, then continue creating the file.
-**Note**: Storing login credentials in the **.splunkrc** file is only for
-convenience during development. This file isn't part of the Splunk platform and
-shouldn't be used for storing user credentials for production. And, if you're
-at all concerned about the security of your credentials, just enter them at
-the command line rather than saving them in this file.
-
-
-#### Examples
+#### Run the examples
-Examples are located in the **/splunk-sdk-python/examples** directory. To run
-the examples at the command line, use the Python interpreter and include any
-arguments that are required by the example:
+Examples are located in the **/splunk-sdk-python/examples** directory. To run the examples at the command line, use the Python interpreter and include any arguments that are required by the example:
python examplename.py --username="admin" --password="changeme"
-If you saved your login credentials in the **.splunkrc** file, you can omit
-those arguments:
+If you saved your login credentials in the **.splunkrc** file, you can omit those arguments:
python examplename.py
@@ -168,174 +126,86 @@ To get help for an example, use the `--help` argument with an example:
python examplename.py --help
-#### Unit tests
+#### Run the unit tests
-The Splunk SDK for Python contains a collection of unit tests. To run them, open a
-command prompt in the **/splunk-sdk-python** directory and enter:
+The Splunk Enterprise SDK for Python contains a collection of unit tests. To run them, open a command prompt in the **/splunk-sdk-python** directory and enter:
make
-You can also run individual test files, which are located in
-**/splunk-sdk-python/tests**. The following command explains how to run
-a specific test:
+You can also run individual test files, which are located in **/splunk-sdk-python/tests**. To run a specific test, enter:
- make test_specific
+ make specific_test_name
-The test suite uses Python's standard library, the built-in `unittest`
-library, `pytest`, and `tox`.
+The test suite uses Python's standard library, the built-in `unittest` library, `pytest`, and `tox`.
-**Important Notes:**
-
-The test run will fail unless the
-[SDK App Collection](https://github.com/splunk/sdk-app-collection) is installed.
-
-You can exclude app-specific tests with the following command:
-
- make test_no_app
-
-You can read more about our testing framework on
-[GitHub](https://github.com/splunk/splunk-sdk-python/tree/master/tests).
-
-In addition, the test run requires the searchcommands app to be built. The `make`
-command runs the tasks to do this, but more complex testing may require you to
-rebuild using `make build_app`.
+>**Notes:**
+>* The test run fails unless the [SDK App Collection](https://github.com/splunk/sdk-app-collection) app is installed.
+>* To exclude app-specific tests, use the `make test_no_app` command.
+>* To learn about our testing framework, see [Splunk Test Suite](https://github.com/splunk/splunk-sdk-python/tree/master/tests) on GitHub.
+> In addition, the test run requires you to build the searchcommands app. The `make` command runs the tasks to do this, but more complex testing may require you to rebuild using the `make build_app` command.
## Repository
-
-
-
-/docs |
-Source for Sphinx-based docs and build |
-
-
-
-/examples |
-Examples demonstrating various SDK features |
-
-
-
-/splunklib |
-Source for the Splunk library modules |
-
-
-
-/tests |
-Source for unit tests |
-
-
-
-/utils |
-Source for utilities shared by the examples and unit tests |
-
-
-
+| Directory | Description |
+|:--------- |:---------------------------------------------------------- |
+|/docs | Source for Sphinx-based docs and build |
+|/examples | Examples demonstrating various SDK features |
+|/splunklib | Source for the Splunk library modules |
+|/tests | Source for unit tests |
+|/utils | Source for utilities shared by the examples and unit tests |
### Changelog
-The **CHANGELOG.md** file in the root of the repository contains a description
-of changes for each version of the SDK. You can also find it online at
-[https://github.com/splunk/splunk-sdk-python/blob/master/CHANGELOG.md](https://github.com/splunk/splunk-sdk-python/blob/master/CHANGELOG.md).
+The [CHANGELOG](CHANGELOG.md) contains a description of changes for each version of the SDK. For the latest version, see the [CHANGELOG.md](https://github.com/splunk/splunk-sdk-python/blob/master/CHANGELOG.md) on GitHub.
### Branches
-The **master** branch always represents a stable and released version of the SDK.
-You can read more about our branching model on our Wiki at
-[https://github.com/splunk/splunk-sdk-python/wiki/Branching-Model](https://github.com/splunk/splunk-sdk-python/wiki/Branching-Model).
+The **master** branch represents a stable and released version of the SDK.
+To learn about our branching model, see [Branching Model](https://github.com/splunk/splunk-sdk-python/wiki/Branching-Model) on GitHub.
## Documentation and resources
-If you need to know more:
-
-* For all things developer with Splunk, your main resource is the
- [Splunk Developer Portal](http://dev.splunk.com).
-* For conceptual and how-to documentation, see the
- [Overview of the Splunk SDK for Python](http://dev.splunk.com/view/SP-CAAAEBB).
+| Resource | Description |
+|:----------------------- |:----------- |
+| [Splunk Developer Portal](http://dev.splunk.com) | General developer documentation, tools, and examples |
+| [Integrate the Splunk platform using development tools for Python](https://dev.splunk.com/enterprise/docs/devtools/python)| Documentation for Python development |
+| [Splunk Enterprise SDK for Python Reference](http://docs.splunk.com/Documentation/PythonSDK) | SDK API reference documentation |
+| [REST API Reference Manual](https://docs.splunk.com/Documentation/Splunk/latest/RESTREF/RESTprolog) | Splunk REST API reference documentation |
+| [Splunk>Docs](https://docs.splunk.com/Documentation) | General documentation for the Splunk platform |
+| [GitHub Wiki](https://github.com/splunk/splunk-sdk-python/wiki/) | Documentation for this SDK's repository on GitHub |
-* For API reference documentation, see the
- [Splunk SDK for Python Reference](http://docs.splunk.com/Documentation/PythonSDK).
-
-* For more about the Splunk REST API, see the
- [REST API Reference](http://docs.splunk.com/Documentation/Splunk/latest/RESTAPI).
-
-* For more about about Splunk in general, see
- [Splunk>Docs](http://docs.splunk.com/Documentation/Splunk).
-
-* For more about this SDK's repository, see our
- [GitHub Wiki](https://github.com/splunk/splunk-sdk-python/wiki/).
## Community
-Stay connected with other developers building on Splunk.
-
-
+Stay connected with other developers building on the Splunk platform.
-
-Email |
-devinfo@splunk.com |
-
+* [Email](mailto:devinfo@splunk.com)
+* [Issues and pull requests](https://github.com/splunk/splunk-sdk-python/issues/)
+* [Community Slack](https://splunk-usergroups.slack.com/app_redirect?channel=appdev)
+* [Splunk Answers](https://community.splunk.com/t5/Splunk-Development/ct-p/developer-tools)
+* [Splunk Blogs](https://www.splunk.com/blog)
+* [Twitter](https://twitter.com/splunkdev)
-
-Issues
- | https://github.com/splunk/splunk-sdk-python/issues/ |
-
+### Contributions
-
-Answers
- | http://splunk-base.splunk.com/tags/python/ |
-
+If you would like to contribute to the SDK, see [Contributing to Splunk](https://www.splunk.com/en_us/form/contributions.html). For additional guidelines, see [CONTRIBUTING](CONTRIBUTING.md).
-
-Blog
- | http://blogs.splunk.com/dev/ |
-
-
-
-Twitter
- | @splunkdev |
-
-
-
-
-### How to contribute
-
-If you would like to contribute to the SDK, go here for more information:
+### Support
-* [Splunk and open source](http://dev.splunk.com/view/opensource/SP-CAAAEDM)
+* You will be granted support if you or your company are already covered under an existing maintenance/support agreement. Submit a new case in the [Support Portal](https://www.splunk.com/en_us/support-and-services.html) and include "Splunk Enterprise SDK for Python" in the subject line.
-* [Individual contributions](http://dev.splunk.com/goto/individualcontributions)
+ If you are not covered under an existing maintenance/support agreement, you can find help through the broader community at [Splunk Answers](https://community.splunk.com/t5/Splunk-Development/ct-p/developer-tools).
-* [Company contributions](http://dev.splunk.com/view/companycontributions/SP-CAAAEDR)
+* Splunk will NOT provide support for SDKs if the core library (the code in the /splunklib directory) has been modified. If you modify an SDK and want support, you can find help through the broader community and [Splunk Answers](https://community.splunk.com/t5/Splunk-Development/ct-p/developer-tools).
-### Support
+ We would also like to know why you modified the core library, so please send feedback to _devinfo@splunk.com_.
-1. You will be granted support if you or your company are already covered
- under an existing maintenance/support agreement.
- Submit a new case in the [Support Portal][contact] and include "Splunk SDK for Python"
- in the subject line.
-2. If you are not covered under an existing maintenance/support agreement, you
- can find help through the broader community at:
-
- - Splunk Answers (use
- the sdk, java, python, and javascript tags to
- identify your questions)
-
-3. Splunk will NOT provide support for SDKs if the core library (the
- code in the /splunklib directory) has been modified. If you modify an
- SDK and want support, you can find help through the broader community and
- Splunk answers (see above). We would also like to know why you modified the
- core library—please send feedback to devinfo@splunk.com.
-4. File any issues on [GitHub](https://github.com/splunk/splunk-sdk-python/issues).
+* File any issues on [GitHub](https://github.com/splunk/splunk-sdk-python/issues).
### Contact Us
-You can [contact support][contact] if you have Splunk related questions.
-
-You can reach the Developer Platform team at _devinfo@splunk.com_.
+You can reach the Splunk Developer Platform team at _devinfo@splunk.com_.
## License
-The Splunk Software Development Kit for Python is licensed under the Apache
-License 2.0. Details can be found in the file LICENSE.
-
-[contact]: https://www.splunk.com/en_us/support-and-services.html
+The Splunk Enterprise Software Development Kit for Python is licensed under the Apache License 2.0. See [LICENSE](LICENSE) for details.
diff --git a/splunklib/binding.py b/splunklib/binding.py
index e7336f2a1..e21da6e52 100644
--- a/splunklib/binding.py
+++ b/splunklib/binding.py
@@ -724,7 +724,12 @@ def post(self, path_segment, owner=None, app=None, sharing=None, headers=None, *
:type headers: ``list`` of 2-tuples.
:param query: All other keyword arguments, which are used as query
parameters.
- :type query: ``string``
+ :param body: Parameters to be used in the post body. If specified,
+ any parameters in the query will be applied to the URL instead of
+ the body. If a dict is supplied, the key-value pairs will be form
+ encoded. If a string is supplied, the body will be passed through
+ in the request unchanged.
+ :type body: ``dict`` or ``str``
:return: The response from the server.
:rtype: ``dict`` with keys ``body``, ``headers``, ``reason``,
and ``status``
@@ -1223,6 +1228,8 @@ def post(self, url, headers=None, **kwargs):
headers.append(("Content-Type", "application/x-www-form-urlencoded"))
body = kwargs.pop('body')
+ if isinstance(body, dict):
+ body = _encode(**body).encode('utf-8')
if len(kwargs) > 0:
url = url + UrlEncoded('?' + _encode(**kwargs), skip_encode=True)
else:
diff --git a/splunkrc.spec b/splunkrc.spec
index 52c953cb0..cd158fcfd 100644
--- a/splunkrc.spec
+++ b/splunkrc.spec
@@ -1,12 +1,12 @@
-# Splunk host (default: localhost)
+# Splunk Enterprise host (default: localhost)
host=localhost
-# Splunk admin port (default: 8089)
+# Splunk Enterprise admin port (default: 8089)
port=8089
-# Splunk username
+# Splunk Enterprise username
username=admin
-# Splunk password
+# Splunk Enterprise password
password=changeme
# Access scheme (default: https)
scheme=https
-# Your version of Splunk (default: 6.3)
-version=6.3
\ No newline at end of file
+# Your version of Splunk Enterprise
+version=8.0
\ No newline at end of file
diff --git a/tests/test_binding.py b/tests/test_binding.py
index c636656fe..2d3107507 100755
--- a/tests/test_binding.py
+++ b/tests/test_binding.py
@@ -16,14 +16,16 @@
from __future__ import absolute_import
-import time
from io import BytesIO
+from threading import Thread
+from splunklib.six.moves import BaseHTTPServer
from splunklib.six.moves.urllib.request import Request, urlopen
from splunklib.six.moves.urllib.error import HTTPError
-from splunklib.six import StringIO
+import splunklib.six as six
from xml.etree.ElementTree import XML
+import json
import logging
from tests import testlib
import unittest
@@ -104,7 +106,7 @@ def test_read_partial(self):
def test_readable(self):
txt = "abcd"
- response = binding.ResponseReader(StringIO(txt))
+ response = binding.ResponseReader(six.StringIO(txt))
self.assertTrue(response.readable())
def test_readinto_bytearray(self):
@@ -808,6 +810,119 @@ def test_connect_with_preexisting_token_sans_user_and_pass(self):
socket.write("\r\n".encode('utf-8'))
socket.close()
+
+class TestPostWithBodyParam(unittest.TestCase):
+
+ def test_post(self):
+ def handler(url, message, **kwargs):
+ assert six.ensure_str(url) == "https://localhost:8089/servicesNS/testowner/testapp/foo/bar"
+ assert six.ensure_str(message["body"]) == "testkey=testvalue"
+ return splunklib.data.Record({
+ "status": 200,
+ "headers": [],
+ })
+ ctx = binding.Context(handler=handler)
+ ctx.post("foo/bar", owner="testowner", app="testapp", body={"testkey": "testvalue"})
+
+ def test_post_with_params_and_body(self):
+ def handler(url, message, **kwargs):
+ assert url == "https://localhost:8089/servicesNS/testowner/testapp/foo/bar?extrakey=extraval"
+ assert six.ensure_str(message["body"]) == "testkey=testvalue"
+ return splunklib.data.Record({
+ "status": 200,
+ "headers": [],
+ })
+ ctx = binding.Context(handler=handler)
+ ctx.post("foo/bar", extrakey="extraval", owner="testowner", app="testapp", body={"testkey": "testvalue"})
+
+ def test_post_with_params_and_no_body(self):
+ def handler(url, message, **kwargs):
+ assert url == "https://localhost:8089/servicesNS/testowner/testapp/foo/bar"
+ assert six.ensure_str(message["body"]) == "extrakey=extraval"
+ return splunklib.data.Record({
+ "status": 200,
+ "headers": [],
+ })
+ ctx = binding.Context(handler=handler)
+ ctx.post("foo/bar", extrakey="extraval", owner="testowner", app="testapp")
+
+
+def _wrap_handler(func, response_code=200, body=""):
+ def wrapped(handler_self):
+ result = func(handler_self)
+ if result is None:
+ handler_self.send_response(response_code)
+ handler_self.end_headers()
+ handler_self.wfile.write(body)
+ return wrapped
+
+
+class MockServer(object):
+ def __init__(self, port=9093, **handlers):
+ methods = {"do_" + k: _wrap_handler(v) for (k, v) in handlers.items()}
+
+ def init(handler_self, socket, address, server):
+ BaseHTTPServer.BaseHTTPRequestHandler.__init__(handler_self, socket, address, server)
+
+ def log(*args): # To silence server access logs
+ pass
+
+ methods["__init__"] = init
+ methods["log_message"] = log
+ Handler = type("Handler",
+ (BaseHTTPServer.BaseHTTPRequestHandler, object),
+ methods)
+ self._svr = BaseHTTPServer.HTTPServer(("localhost", port), Handler)
+
+ def run():
+ self._svr.handle_request()
+ self._thread = Thread(target=run)
+ self._thread.daemon = True
+
+ def __enter__(self):
+ self._thread.start()
+ return self._svr
+
+ def __exit__(self, typ, value, traceback):
+ self._thread.join(10)
+ self._svr.server_close()
+
+
+class TestFullPost(unittest.TestCase):
+
+ def test_post_with_body_urlencoded(self):
+ def check_response(handler):
+ length = int(handler.headers.get('content-length', 0))
+ body = handler.rfile.read(length)
+ assert six.ensure_str(body) == "foo=bar"
+
+ with MockServer(POST=check_response):
+ ctx = binding.connect(port=9093, scheme='http', token="waffle")
+ ctx.post("/", foo="bar")
+
+ def test_post_with_body_string(self):
+ def check_response(handler):
+ length = int(handler.headers.get('content-length', 0))
+ body = handler.rfile.read(length)
+ assert six.ensure_str(handler.headers['content-type']) == 'application/json'
+ assert json.loads(body)["baz"] == "baf"
+
+ with MockServer(POST=check_response):
+ ctx = binding.connect(port=9093, scheme='http', token="waffle", headers=[("Content-Type", "application/json")])
+ ctx.post("/", foo="bar", body='{"baz": "baf"}')
+
+ def test_post_with_body_dict(self):
+ def check_response(handler):
+ length = int(handler.headers.get('content-length', 0))
+ body = handler.rfile.read(length)
+ assert six.ensure_str(handler.headers['content-type']) == 'application/x-www-form-urlencoded'
+ assert six.ensure_str(body) == 'baz=baf&hep=cat' or six.ensure_str(body) == 'hep=cat&baz=baf'
+
+ with MockServer(POST=check_response):
+ ctx = binding.connect(port=9093, scheme='http', token="waffle")
+ ctx.post("/", foo="bar", body={"baz": "baf", "hep": "cat"})
+
+
if __name__ == "__main__":
try:
import unittest2 as unittest