test_postfix.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test cases for twisted.protocols.postfix module.
  5. """
  6. from twisted.trial import unittest
  7. from twisted.protocols import postfix
  8. from twisted.test.proto_helpers import StringTransport
  9. class PostfixTCPMapQuoteTests(unittest.TestCase):
  10. data = [
  11. # (raw, quoted, [aliasQuotedForms]),
  12. (b'foo', b'foo'),
  13. (b'foo bar', b'foo%20bar'),
  14. (b'foo\tbar', b'foo%09bar'),
  15. (b'foo\nbar', b'foo%0Abar', b'foo%0abar'),
  16. (b'foo\r\nbar', b'foo%0D%0Abar', b'foo%0D%0abar', b'foo%0d%0Abar', b'foo%0d%0abar'),
  17. (b'foo ', b'foo%20'),
  18. (b' foo', b'%20foo'),
  19. ]
  20. def testData(self):
  21. for entry in self.data:
  22. raw = entry[0]
  23. quoted = entry[1:]
  24. self.assertEqual(postfix.quote(raw), quoted[0])
  25. for q in quoted:
  26. self.assertEqual(postfix.unquote(q), raw)
  27. class PostfixTCPMapServerTestCase(object):
  28. data = {
  29. # 'key': 'value',
  30. }
  31. chat = [
  32. # (input, expected_output),
  33. ]
  34. def test_chat(self):
  35. """
  36. Test that I{get} and I{put} commands are responded to correctly by
  37. L{postfix.PostfixTCPMapServer} when its factory is an instance of
  38. L{postifx.PostfixTCPMapDictServerFactory}.
  39. """
  40. factory = postfix.PostfixTCPMapDictServerFactory(self.data)
  41. transport = StringTransport()
  42. protocol = postfix.PostfixTCPMapServer()
  43. protocol.service = factory
  44. protocol.factory = factory
  45. protocol.makeConnection(transport)
  46. for input, expected_output in self.chat:
  47. protocol.lineReceived(input)
  48. self.assertEqual(
  49. transport.value(), expected_output,
  50. 'For %r, expected %r but got %r' % (
  51. input, expected_output, transport.value()))
  52. transport.clear()
  53. protocol.setTimeout(None)
  54. def test_deferredChat(self):
  55. """
  56. Test that I{get} and I{put} commands are responded to correctly by
  57. L{postfix.PostfixTCPMapServer} when its factory is an instance of
  58. L{postifx.PostfixTCPMapDeferringDictServerFactory}.
  59. """
  60. factory = postfix.PostfixTCPMapDeferringDictServerFactory(self.data)
  61. transport = StringTransport()
  62. protocol = postfix.PostfixTCPMapServer()
  63. protocol.service = factory
  64. protocol.factory = factory
  65. protocol.makeConnection(transport)
  66. for input, expected_output in self.chat:
  67. protocol.lineReceived(input)
  68. self.assertEqual(
  69. transport.value(), expected_output,
  70. 'For %r, expected %r but got %r' % (
  71. input, expected_output, transport.value()))
  72. transport.clear()
  73. protocol.setTimeout(None)
  74. class ValidTests(PostfixTCPMapServerTestCase, unittest.TestCase):
  75. data = {
  76. b'foo': b'ThisIs Foo',
  77. b'bar': b' bar really is found\r\n',
  78. }
  79. chat = [
  80. (b'get', b"400 Command 'get' takes 1 parameters.\n"),
  81. (b'get foo bar', b"500 \n"),
  82. (b'put', b"400 Command 'put' takes 2 parameters.\n"),
  83. (b'put foo', b"400 Command 'put' takes 2 parameters.\n"),
  84. (b'put foo bar baz', b"500 put is not implemented yet.\n"),
  85. (b'put foo bar', b'500 put is not implemented yet.\n'),
  86. (b'get foo', b'200 ThisIs%20Foo\n'),
  87. (b'get bar', b'200 %20bar%20really%20is%20found%0D%0A\n'),
  88. (b'get baz', b'500 \n'),
  89. (b'foo', b'400 unknown command\n'),
  90. ]