def test_xml_dumps(): """ Test the `ipalib.rpc.xml_dumps` function. """ f = rpc.xml_dumps params = (binary_bytes, utf8_bytes, unicode_str, None) # Test serializing an RPC request: data = f(params, API_VERSION, 'the_method') (p, m) = loads(data) assert_equal(m, u'the_method') assert type(p) is tuple assert rpc.xml_unwrap(p) == params # Test serializing an RPC response: data = f((params,), API_VERSION, methodresponse=True) (tup, m) = loads(data) assert m is None assert len(tup) == 1 assert type(tup) is tuple assert rpc.xml_unwrap(tup[0]) == params # Test serializing an RPC response containing a Fault: fault = Fault(69, unicode_str) data = f(fault, API_VERSION, methodresponse=True) e = raises(Fault, loads, data) assert e.faultCode == 69 assert_equal(e.faultString, unicode_str)
def test_i8(self): for value in self.long_data: value = (value, ) enc = xmlrpcplus.dumps(value, methodresponse=1) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None) # and as a call method = "foomethod" value = tuple(self.long_data) enc = xmlrpcplus.dumps(value, methodname=method) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, method)
def test_i8(self): for value in self.long_data: value = (value,) enc = xmlrpcplus.dumps(value, methodresponse=1) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None) # and as a call method = "foomethod" value = tuple(self.long_data) enc = xmlrpcplus.dumps(value, methodname=method) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, method)
def test_generator(self): value = (self.gendata(), ) enc = xmlrpcplus.dumps(value, methodresponse=1) params, method = xmlrpc_client.loads(enc) expect = (list(self.gendata()), ) self.assertEqual(params, expect) self.assertEqual(method, None)
def test_no_i8(self): # we shouldn't use i8 if we don't have to data = [ 23, 42, -1024, 2**31 - 1, -2**31, [2**31 - 1], { "a": -2**31, "b": 3.14 }, ] for value in data: value = (value, ) enc = xmlrpcplus.dumps(value, methodresponse=1, encoding='us-ascii') _enc = xmlrpc_client.dumps(value, methodresponse=1, allow_none=1, encoding='us-ascii') if 'i8' in enc or 'I8' in enc: raise Exception('i8 used unnecessarily') self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None)
def test_generator(self): value = (self.gendata(),) enc = xmlrpcplus.dumps(value, methodresponse=1) params, method = xmlrpc_client.loads(enc) expect = (list(self.gendata()),) self.assertEqual(params, expect) self.assertEqual(method, None)
def test_marshaller(self): value = 3.14159 value = (value,) enc = xmlrpcplus.dumps(value, methodresponse=1, marshaller=MyMarshaller) params, method = xmlrpc_client.loads(enc) # MyMarshaller rounds off floats self.assertEqual(params, (3,)) self.assertEqual(method, None)
def test_response(self): for value in self.standard_data: value = (value,) enc = xmlrpcplus.dumps(value, methodresponse=1) _enc = xmlrpc_client.dumps(value, methodresponse=1, allow_none=1) self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None)
def test_response(self): for value in self.standard_data: value = (value, ) enc = xmlrpcplus.dumps(value, methodresponse=1) _enc = xmlrpc_client.dumps(value, methodresponse=1, allow_none=1) self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None)
def test_call(self): method = 'my_rpc_method' for value in self.standard_data: value = (value, "other arg") enc = xmlrpcplus.dumps(value, methodname=method) _enc = xmlrpc_client.dumps(value, methodname=method, allow_none=1) self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, method)
def test_just_data(self): # xmlrpc_client supports this case, so I guess we should too # neither method call nor response for value in self.standard_data: value = (value, "foo", "bar") enc = xmlrpcplus.dumps(value) _enc = xmlrpc_client.dumps(value, allow_none=1) self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None)
def loads(self, str_data): try: try: # Python 3 return xmlrpc_client.loads(str_data, use_builtin_types=settings. MODERNRPC_XMLRPC_USE_BUILTIN_TYPES) except TypeError: # Python 2 return xmlrpc_client.loads( str_data, use_datetime=settings.MODERNRPC_XMLRPC_USE_BUILTIN_TYPES) except xml.parsers.expat.ExpatError as e: raise RPCParseError(e) except xmlrpc_client.ResponseError: raise RPCInvalidRequest('Bad XML-RPC payload') except Exception as e: # pragma: no cover raise RPCInvalidRequest(e)
def _marshaled_dispatch(self, request, dispatch_method=None): """Dispatches an XML-RPC method from marshalled (XML) data. XML-RPC methods are dispatched from the marshalled (XML) data using the _dispatch method and the result is returned as marshalled data. For backwards compatibility, a dispatch function can be provided as an argument (see comment in SimpleXMLRPCRequestHandler.do_POST) but overriding the existing method through subclassing is the prefered means of changing method dispatch behavior. """ data = request.body params, method = xmlrpclib.loads(data) # add request to params params = (request, ) + params # generate response try: if dispatch_method is not None: response = dispatch_method(method, params) else: response = self._dispatch(method, params) # wrap response in a singleton tuple response = (response, ) response = xmlrpclib.dumps(response, methodresponse=1, allow_none=self.allow_none, encoding=self.encoding) except xmlrpclib.Fault as fault: response = xmlrpclib.dumps(fault, allow_none=self.allow_none, encoding=self.encoding) except: # report exception back to server if settings.DEBUG: from kobo.tback import Traceback response = xmlrpclib.dumps(xmlrpclib.Fault( 1, u"%s" % Traceback().get_traceback()), allow_none=self.allow_none, encoding=self.encoding) else: exc_info = sys.exc_info()[1] exc_type = exc_info.__class__.__name__ response = xmlrpclib.dumps(xmlrpclib.Fault( 1, "%s: %s" % (exc_type, exc_info)), allow_none=self.allow_none, encoding=self.encoding) return response
def rpc_pypi(method, *args): """Call an XML-RPC method on the Pypi server.""" conn = http_client.HTTPSConnection(PYPI_HOSTNAME) headers = {'Content-Type': 'text/xml'} payload = xmlrpc_client.dumps(args, method) conn.request("POST", "/pypi", payload, headers) response = conn.getresponse() if response.status == 200: result = xmlrpc_client.loads(response.read())[0][0] return result else: raise RuntimeError("Unable to download the list of top " "packages from Pypi.")
def test_encoding(self): data = [ 45, ["hello", "world"], {"a": 5.5, "b": [None]}, ] for value in data: value = (value,) enc = xmlrpcplus.dumps(value, methodresponse=1, encoding='us-ascii') _enc = xmlrpc_client.dumps(value, methodresponse=1, allow_none=1, encoding='us-ascii') self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None)
def test_fault(self): code = 1001 msg = "some useless error" f1 = xmlrpcplus.Fault(code, msg) f2 = xmlrpc_client.Fault(code, msg) value = f1 enc = xmlrpcplus.dumps(value, methodresponse=1) _enc = xmlrpc_client.dumps(value, methodresponse=1, allow_none=1) self.assertEqual(enc, _enc) try: params, method = xmlrpc_client.loads(enc) except xmlrpc_client.Fault as e: self.assertEqual(e.faultCode, code) self.assertEqual(e.faultString, msg) else: raise Exception('Fault not raised')
def _marshaled_dispatch(self, request, dispatch_method = None): """Dispatches an XML-RPC method from marshalled (XML) data. XML-RPC methods are dispatched from the marshalled (XML) data using the _dispatch method and the result is returned as marshalled data. For backwards compatibility, a dispatch function can be provided as an argument (see comment in SimpleXMLRPCRequestHandler.do_POST) but overriding the existing method through subclassing is the prefered means of changing method dispatch behavior. """ data = request.body params, method = xmlrpclib.loads(data) # add request to params params = (request, ) + params # generate response try: if dispatch_method is not None: response = dispatch_method(method, params) else: response = self._dispatch(method, params) # wrap response in a singleton tuple response = (response,) response = xmlrpclib.dumps(response, methodresponse=1, allow_none=self.allow_none, encoding=self.encoding) except xmlrpclib.Fault as fault: response = xmlrpclib.dumps(fault, allow_none=self.allow_none, encoding=self.encoding) except: # report exception back to server if settings.DEBUG: from kobo.tback import Traceback response = xmlrpclib.dumps( xmlrpclib.Fault(1, u"%s" % Traceback().get_traceback()), allow_none=self.allow_none, encoding=self.encoding) else: response = xmlrpclib.dumps( xmlrpclib.Fault(1, "%s: %s" % (sys.exc_type.__name__, sys.exc_info()[1])), allow_none=self.allow_none, encoding=self.encoding) return response
def test_no_i8(self): # we shouldn't use i8 if we don't have to data = [ 23, 42, -1024, 2 ** 31 - 1, -2 ** 31, [2**31 -1], {"a": -2 ** 31, "b": 3.14}, ] for value in data: value = (value,) enc = xmlrpcplus.dumps(value, methodresponse=1, encoding='us-ascii') _enc = xmlrpc_client.dumps(value, methodresponse=1, allow_none=1, encoding='us-ascii') if 'i8' in enc or 'I8' in enc: raise Exception('i8 used unnecessarily') self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None)
def deserialize(self, operation, shape, body): try: args, methodname = xmlrpc_client.loads(body) except xmlrpc_client.Fault as e: return { "Error": { "Code": e.faultCode, "Message": e.faultString, } } args_iter = zip(args, filter( lambda m: m.destination == 'body', shape.iter_members(), )) result = {} for arg, member in args_iter: result[member.name] = arg return result
def _rpcpost(url, req_data, cert, root_bundle): if isinstance(config.HTTP.LOG_URLS, tuple): config.HTTP.LOG_URLS[0].log(config.HTTP.LOG_URLS[1], "POST: %s" % (url)) s = requests.Session() s.mount(url, GCU.TLSHttpAdapter()) if isinstance(config.HTTP.LOG_RAW_REQUESTS, tuple): config.HTTP.LOG_RAW_REQUESTS[0].log(config.HTTP.LOG_RAW_REQUESTS[1], req_data) resp = s.post(url, req_data, cert=cert, verify=root_bundle, headers=headers(), timeout=config.HTTP.TIMEOUT, allow_redirects=config.HTTP.ALLOW_REDIRECTS) if resp.status_code != 200: resp.raise_for_status() if isinstance(config.HTTP.LOG_RAW_RESPONSES, tuple): config.HTTP.LOG_RAW_RESPONSES[0].log(config.HTTP.LOG_RAW_RESPONSES[1], resp.content) return xmlrpclib.loads(resp.content, use_datetime=True)[0][0]
def scgi_request(url, methodname, *params, **kw): """ Send a XMLRPC request over SCGI to the given URL. @param url: Endpoint URL. @param methodname: XMLRPC method name. @param params: Tuple of simple python objects. @keyword deserialize: Parse XML result? (default is True) @return: XMLRPC response, or the equivalent Python data. """ xmlreq = xmlrpclib.dumps(params, methodname) xmlresp = SCGIRequest(url).send(xmlreq) if kw.get("deserialize", True): # This fixes a bug with the Python xmlrpclib module # (has no handler for <i8> in some versions) xmlresp = xmlresp.replace("<i8>", "<i4>").replace("</i8>", "</i4>") # Return deserialized data return xmlrpclib.loads(xmlresp)[0][0] else: # Return raw XML return xmlresp
def testBigNumbersInStats(self): host_monitor = mock.Mock() host_monitor.interrogate.return_value.statistics = [{ "ksm_run": 1, "ksm_pages": 100, "huge_number": 2**31 + 2**10 }] vm1 = mock.Mock() vm1.properties = {"name": "vm1"} vm1.statistics = [{ "free_mem": 25 * 2**30 # 25 TiB (in KiB) }] vm2 = mock.Mock() vm2.properties = {"name": "vm2"} vm2.statistics = [{ "free_mem": 30 * 2**20 # 30 GiB (in KiB) }] guest_manager = mock.Mock() guest_manager.interrogate.return_value.values.return_value = [ vm1, vm2 ] threads = { "host_monitor": host_monitor, "guest_manager": guest_manager } funcs = mom.MOMFuncs(None, threads) data = funcs.getStatistics() mom.enable_i8() packet = xmlrpc_client.dumps((data,)) (reply,), func = xmlrpc_client.loads(packet) self.assertEqual(data, reply)
def scgi_request(url, methodname, *params, **kw): """ Send a XMLRPC request over SCGI to the given URL. :param url: Endpoint URL. :param methodname: XMLRPC method name. :param params: Tuple of simple python objects. :type url: string :type methodname: string :keyword deserialize: Parse XML result? (default is True) :return: XMLRPC string response, or the equivalent Python data. """ xmlreq = xmlrpclib.dumps(params, methodname) xmlresp = SCGIRequest(url).send(xmlreq.encode()).decode() if kw.get("deserialize", True): # This fixes a bug with the Python xmlrpclib module # (has no handler for <i8> in some versions) xmlresp = xmlresp.replace("<i8>", "<i4>").replace("</i8>", "</i4>") # Return deserialized data return xmlrpclib.loads(xmlresp)[0][0] else: # Return raw XML return xmlresp
def dump_n_load(value): param, _method = loads( dumps((value,), allow_none=True) ) return param[0]
def dump_n_load(value): (param, method) = loads( dumps((value,), allow_none=True) ) return param[0]