test_soap.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. #
  5. """Test SOAP support."""
  6. try:
  7. import SOAPpy
  8. except ImportError:
  9. SOAPpy = None
  10. class SOAPPublisher: pass
  11. else:
  12. from twisted.web import soap
  13. SOAPPublisher = soap.SOAPPublisher
  14. from twisted.trial import unittest
  15. from twisted.web import server, error
  16. from twisted.internet import reactor, defer
  17. class Test(SOAPPublisher):
  18. def soap_add(self, a, b):
  19. return a + b
  20. def soap_kwargs(self, a=1, b=2):
  21. return a + b
  22. soap_kwargs.useKeywords=True
  23. def soap_triple(self, string, num):
  24. return [string, num, None]
  25. def soap_struct(self):
  26. return SOAPpy.structType({"a": "c"})
  27. def soap_defer(self, x):
  28. return defer.succeed(x)
  29. def soap_deferFail(self):
  30. return defer.fail(ValueError())
  31. def soap_fail(self):
  32. raise RuntimeError
  33. def soap_deferFault(self):
  34. return defer.fail(ValueError())
  35. def soap_complex(self):
  36. return {"a": ["b", "c", 12, []], "D": "foo"}
  37. def soap_dict(self, map, key):
  38. return map[key]
  39. class SOAPTests(unittest.TestCase):
  40. def setUp(self):
  41. self.publisher = Test()
  42. self.p = reactor.listenTCP(0, server.Site(self.publisher),
  43. interface="127.0.0.1")
  44. self.port = self.p.getHost().port
  45. def tearDown(self):
  46. return self.p.stopListening()
  47. def proxy(self):
  48. return soap.Proxy("http://127.0.0.1:%d/" % self.port)
  49. def testResults(self):
  50. inputOutput = [
  51. ("add", (2, 3), 5),
  52. ("defer", ("a",), "a"),
  53. ("dict", ({"a": 1}, "a"), 1),
  54. ("triple", ("a", 1), ["a", 1, None])]
  55. dl = []
  56. for meth, args, outp in inputOutput:
  57. d = self.proxy().callRemote(meth, *args)
  58. d.addCallback(self.assertEqual, outp)
  59. dl.append(d)
  60. # SOAPpy kinda blows.
  61. d = self.proxy().callRemote('complex')
  62. d.addCallback(lambda result: result._asdict())
  63. d.addCallback(self.assertEqual, {"a": ["b", "c", 12, []], "D": "foo"})
  64. dl.append(d)
  65. # We now return to our regularly scheduled program, already in progress.
  66. return defer.DeferredList(dl, fireOnOneErrback=True)
  67. def testMethodNotFound(self):
  68. """
  69. Check that a non existing method return error 500.
  70. """
  71. d = self.proxy().callRemote('doesntexist')
  72. self.assertFailure(d, error.Error)
  73. def cb(err):
  74. self.assertEqual(int(err.status), 500)
  75. d.addCallback(cb)
  76. return d
  77. def testLookupFunction(self):
  78. """
  79. Test lookupFunction method on publisher, to see available remote
  80. methods.
  81. """
  82. self.assertTrue(self.publisher.lookupFunction("add"))
  83. self.assertTrue(self.publisher.lookupFunction("fail"))
  84. self.assertFalse(self.publisher.lookupFunction("foobar"))
  85. if not SOAPpy:
  86. SOAPTests.skip = "SOAPpy not installed"