コード例 #1
0
    def test_preexisting_token_sans_splunk(self):
        token = self.context.token
        if token.startswith('Splunk '):
            token = token.split(' ', 1)[1]
            self.assertFalse(token.startswith('Splunk '))
        else:
            self.fail('Token did not start with "Splunk ".')
        opts = self.opts.kwargs.copy()
        opts["token"] = token
        opts["username"] = "******"
        opts["password"] = "******"

        newContext = binding.Context(**opts)
        response = newContext.get("/services")
        self.assertEqual(response.status, 200)

        socket = newContext.connect()
        socket.write(("POST %s HTTP/1.1\r\n" %\
                    self.context._abspath("some/path/to/post/to")).encode('utf-8'))
        socket.write(("Host: %s:%s\r\n" %\
                     (self.context.host, self.context.port)).encode('utf-8'))
        socket.write("Accept-Encoding: identity\r\n".encode('utf-8'))
        socket.write(("Authorization: %s\r\n" %\
                     self.context.token).encode('utf-8'))
        socket.write(("X-Splunk-Input-Mode: Streaming\r\n").encode('utf-8'))
        socket.write(("\r\n").encode('utf-8'))
        socket.close()
コード例 #2
0
    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")
コード例 #3
0
    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"})
コード例 #4
0
    def test_preexisting_token(self):
        token = self.context.token
        opts = self.opts.kwargs.copy()
        opts["token"] = token
        opts["username"] = "******"
        opts["password"] = "******"

        newContext = binding.Context(**opts)
        response = newContext.get("/services")
        self.assertEqual(response.status, 200)

        socket = newContext.connect()
        socket.write("POST %s HTTP/1.1\r\n" % \
                         self.context._abspath("some/path/to/post/to"))
        socket.write("Host: %s:%s\r\n" % \
                         (self.context.host, self.context.port))
        socket.write("Accept-Encoding: identity\r\n")
        socket.write("Authorization: %s\r\n" % \
                         self.context.token)
        socket.write("X-Splunk-Input-Mode: Streaming\r\n")
        socket.write("\r\n")
        socket.close()
コード例 #5
0
    def test_login_with_multiple_cookies(self):
        # We should get an error if using a bad cookie
        new_context = binding.Context()
        new_context.get_cookies().update({"bad": "cookie"})
        try:
            new_context = new_context.login()
            self.fail()
        except AuthenticationError as ae:
            self.assertEqual(str(ae), "Login failed.")
            # Bring in a valid cookie now
            for key, value in self.context.get_cookies().items():
                new_context.get_cookies()[key] = value

            self.assertEqual(len(new_context.get_cookies()), 2)
            self.assertTrue('bad' in list(new_context.get_cookies().keys()))
            self.assertTrue(
                'cookie' in list(new_context.get_cookies().values()))

            for k, v in self.context.get_cookies().items():
                self.assertEqual(new_context.get_cookies()[k], v)

            self.assertEqual(new_context.get("apps/local").status, 200)