|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright © 2011-2025 Splunk, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +import json |
| 18 | +import pytest |
| 19 | + |
| 20 | +from tests import testlib |
| 21 | +from splunklib import results |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.smoke |
| 25 | +class TestJSONCustomRestEndpointsApp(testlib.SDKTestCase): |
| 26 | + app_name = "cre_app" |
| 27 | + |
| 28 | + def test_GET(self): |
| 29 | + resp = self.service.get( |
| 30 | + app=self.app_name, |
| 31 | + path_segment="execute", |
| 32 | + headers=[("x-bar", "baz")], |
| 33 | + ) |
| 34 | + self.assertIn(("x-foo", "bar"), resp.headers) |
| 35 | + self.assertEqual(resp.status, 200) |
| 36 | + self.assertEqual( |
| 37 | + json.loads(str(resp.body)), |
| 38 | + { |
| 39 | + "headers": {"x-bar": "baz"}, |
| 40 | + "method": "GET", |
| 41 | + }, |
| 42 | + ) |
| 43 | + |
| 44 | + def test_POST(self): |
| 45 | + body = json.dumps({"foo": "bar"}) |
| 46 | + resp = self.service.post( |
| 47 | + app=self.app_name, |
| 48 | + path_segment="execute", |
| 49 | + body=body, |
| 50 | + headers=[("x-bar", "baz")], |
| 51 | + ) |
| 52 | + self.assertIn(("x-foo", "bar"), resp.headers) |
| 53 | + self.assertEqual(resp.status, 200) |
| 54 | + self.assertEqual( |
| 55 | + json.loads(str(resp.body)), |
| 56 | + { |
| 57 | + "payload": '{"foo": "bar"}', |
| 58 | + "headers": {"x-bar": "baz"}, |
| 59 | + "method": "POST", |
| 60 | + }, |
| 61 | + ) |
| 62 | + |
| 63 | + def test_DELETE(self): |
| 64 | + # delete does allow specifying body and custom headers. |
| 65 | + resp = self.service.delete( |
| 66 | + app=self.app_name, |
| 67 | + path_segment="execute", |
| 68 | + ) |
| 69 | + self.assertIn(("x-foo", "bar"), resp.headers) |
| 70 | + self.assertEqual(resp.status, 200) |
| 71 | + self.assertEqual( |
| 72 | + json.loads(str(resp.body)), |
| 73 | + { |
| 74 | + "payload": "", |
| 75 | + "headers": {}, |
| 76 | + "method": "DELETE", |
| 77 | + }, |
| 78 | + ) |
0 commit comments