def test_xmlrpc_marshal(self): from supervisor import xmlrpc data = xmlrpc.xmlrpc_marshal(1) self.assertEqual(data, xmlrpclib.dumps((1,), methodresponse=True)) fault = xmlrpclib.Fault(1, 'foo') data = xmlrpc.xmlrpc_marshal(fault) self.assertEqual(data, xmlrpclib.dumps(fault))
def xmlrpc_marshal(value): ismethodresponse = not isinstance(value, xmlrpclib.Fault) if ismethodresponse: if not isinstance(value, tuple): value = (value, ) body = xmlrpclib.dumps(value, methodresponse=ismethodresponse) else: body = xmlrpclib.dumps(value) return body
def xmlrpc_marshal(value): ismethodresponse = not isinstance(value, xmlrpclib.Fault) if ismethodresponse: if not isinstance(value, tuple): value = (value,) body = xmlrpclib.dumps(value, methodresponse=ismethodresponse) else: body = xmlrpclib.dumps(value) return body
def test_continue_request_methodsuccess(self): supervisor = DummySupervisor() subinterfaces = [('supervisor', DummySupervisorRPCNamespace())] handler = self._makeOne(supervisor, subinterfaces) data = xmlrpclib.dumps((), 'supervisor.getAPIVersion') request = DummyRequest('/what/ever', None, None, None) handler.continue_request(data, request) logdata = supervisor.options.logger.data from supervisor.xmlrpc import loads if loads: expected = 2 else: expected = 3 self.assertEqual(len(logdata), expected) self.assertEqual(logdata[-2], 'XML-RPC method called: supervisor.getAPIVersion()') self.assertEqual( logdata[-1], 'XML-RPC method supervisor.getAPIVersion() returned successfully') self.assertEqual(len(request.producers), 1) xml_response = request.producers[0] response = xmlrpclib.loads(xml_response) from supervisor.rpcinterface import API_VERSION self.assertEqual(response[0][0], API_VERSION) self.assertEqual(request._done, True) self.assertEqual(request.headers['Content-Type'], 'text/xml') self.assertEqual(request.headers['Content-Length'], len(xml_response))
def test_continue_request_methodsuccess(self): supervisor = DummySupervisor() subinterfaces = [('supervisor', DummySupervisorRPCNamespace())] handler = self._makeOne(supervisor, subinterfaces) data = xmlrpclib.dumps((), 'supervisor.getAPIVersion') request = DummyRequest('/what/ever', None, None, None) handler.continue_request(data, request) logdata = supervisor.options.logger.data from supervisor.xmlrpc import loads if loads: expected = 2 else: expected = 3 self.assertEqual(len(logdata), expected) self.assertEqual(logdata[-2], 'XML-RPC method called: supervisor.getAPIVersion()') self.assertEqual(logdata[-1], 'XML-RPC method supervisor.getAPIVersion() returned successfully') self.assertEqual(len(request.producers), 1) xml_response = request.producers[0] response = xmlrpclib.loads(xml_response) from supervisor.rpcinterface import API_VERSION self.assertEqual(response[0][0], API_VERSION) self.assertEqual(request._done, True) self.assertEqual(request.headers['Content-Type'], 'text/xml') self.assertEqual(request.headers['Content-Length'], len(xml_response))
def test_continue_request_500(self): supervisor = DummySupervisor() subinterfaces = [('supervisor', DummySupervisorRPCNamespace())] handler = self._makeOne(supervisor, subinterfaces) data = xmlrpclib.dumps((), 'supervisor.raiseError') request = DummyRequest('/what/ever', None, None, None) handler.continue_request(data, request) logdata = supervisor.options.logger.data self.assertEqual(len(logdata), 2) self.assertEqual(logdata[-2], 'XML-RPC method called: supervisor.raiseError()') self.assertTrue(logdata[-1].startswith('Traceback')) self.assertTrue(logdata[-1].endswith('ValueError: error\n')) self.assertEqual(len(request.producers), 0) self.assertEqual(request._error, 500)
def test_continue_request_500_if_rpcinterface_method_call_raises(self): supervisor = DummySupervisor() subinterfaces = [('supervisor', DummySupervisorRPCNamespace())] handler = self._makeOne(supervisor, subinterfaces) data = xmlrpclib.dumps((), 'supervisor.raiseError') request = DummyRequest('/what/ever', None, None, None) handler.continue_request(data, request) logdata = supervisor.options.logger.data self.assertEqual(len(logdata), 2) self.assertEqual(logdata[0], 'XML-RPC method called: supervisor.raiseError()') self.assertTrue("unexpected exception" in logdata[1]) self.assertTrue(repr(data) in logdata[1]) self.assertTrue("Traceback" in logdata[1]) self.assertTrue("ValueError: error" in logdata[1]) self.assertEqual(request._error, 500)
def test_continue_request_nosuchmethod(self): supervisor = DummySupervisor() subinterfaces = [('supervisor', DummySupervisorRPCNamespace())] handler = self._makeOne(supervisor, subinterfaces) data = xmlrpclib.dumps(('a', 'b'), 'supervisor.noSuchMethod') request = DummyRequest('/what/ever', None, None, None) handler.continue_request(data, request) logdata = supervisor.options.logger.data self.assertEqual(len(logdata), 2) self.assertEqual(logdata[-2], 'XML-RPC method called: supervisor.noSuchMethod()') self.assertEqual(logdata[-1], ('XML-RPC method supervisor.noSuchMethod() returned fault: ' '[1] UNKNOWN_METHOD')) self.assertEqual(len(request.producers), 1) xml_response = request.producers[0] self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, xml_response)
def test_continue_request_500_if_xmlrpc_dumps_raises(self): supervisor = DummySupervisor() subinterfaces = [('supervisor', DummySupervisorRPCNamespace())] handler = self._makeOne(supervisor, subinterfaces) data = xmlrpclib.dumps((), 'supervisor.getXmlRpcUnmarshallable') request = DummyRequest('/what/ever', None, None, None) handler.continue_request(data, request) logdata = supervisor.options.logger.data self.assertEqual(len(logdata), 3) self.assertEqual(logdata[0], 'XML-RPC method called: supervisor.getXmlRpcUnmarshallable()') self.assertEqual(logdata[1], 'XML-RPC method supervisor.getXmlRpcUnmarshallable() ' 'returned successfully') self.assertTrue("unexpected exception" in logdata[2]) self.assertTrue(repr(data) in logdata[2]) self.assertTrue("Traceback" in logdata[2]) self.assertTrue("TypeError: cannot marshal" in logdata[2]) self.assertEqual(request._error, 500)
def test_continue_request_value_is_function(self): class DummyRPCNamespace(object): def foo(self): def inner(self): return 1 inner.delay = .05 return inner supervisor = DummySupervisor() subinterfaces = [('supervisor', DummySupervisorRPCNamespace()), ('ns1', DummyRPCNamespace())] handler = self._makeOne(supervisor, subinterfaces) data = xmlrpclib.dumps((), 'ns1.foo') request = DummyRequest('/what/ever', None, None, None) handler.continue_request(data, request) logdata = supervisor.options.logger.data self.assertEqual(len(logdata), 2) self.assertEqual(logdata[-2], 'XML-RPC method called: ns1.foo()') self.assertEqual(logdata[-1], 'XML-RPC method ns1.foo() returned successfully') self.assertEqual(len(request.producers), 0) self.assertEqual(request._done, False)