Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_postfix.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test cases for twisted.protocols.postfix module.
  5. """
  6. from typing import Dict, List, Tuple
  7. from twisted.protocols import postfix
  8. from twisted.test.proto_helpers import StringTransport
  9. from twisted.trial import unittest
  10. class PostfixTCPMapQuoteTests(unittest.TestCase):
  11. data = [
  12. # (raw, quoted, [aliasQuotedForms]),
  13. (b"foo", b"foo"),
  14. (b"foo bar", b"foo%20bar"),
  15. (b"foo\tbar", b"foo%09bar"),
  16. (b"foo\nbar", b"foo%0Abar", b"foo%0abar"),
  17. (
  18. b"foo\r\nbar",
  19. b"foo%0D%0Abar",
  20. b"foo%0D%0abar",
  21. b"foo%0d%0Abar",
  22. b"foo%0d%0abar",
  23. ),
  24. (b"foo ", b"foo%20"),
  25. (b" foo", b"%20foo"),
  26. ]
  27. def testData(self):
  28. for entry in self.data:
  29. raw = entry[0]
  30. quoted = entry[1:]
  31. self.assertEqual(postfix.quote(raw), quoted[0])
  32. for q in quoted:
  33. self.assertEqual(postfix.unquote(q), raw)
  34. class PostfixTCPMapServerTestCase:
  35. data: Dict[bytes, bytes] = {
  36. # 'key': 'value',
  37. }
  38. chat: List[Tuple[bytes, bytes]] = [
  39. # (input, expected_output),
  40. ]
  41. def test_chat(self):
  42. """
  43. Test that I{get} and I{put} commands are responded to correctly by
  44. L{postfix.PostfixTCPMapServer} when its factory is an instance of
  45. L{postifx.PostfixTCPMapDictServerFactory}.
  46. """
  47. factory = postfix.PostfixTCPMapDictServerFactory(self.data)
  48. transport = StringTransport()
  49. protocol = postfix.PostfixTCPMapServer()
  50. protocol.service = factory
  51. protocol.factory = factory
  52. protocol.makeConnection(transport)
  53. for input, expected_output in self.chat:
  54. protocol.lineReceived(input)
  55. self.assertEqual(
  56. transport.value(),
  57. expected_output,
  58. "For %r, expected %r but got %r"
  59. % (input, expected_output, transport.value()),
  60. )
  61. transport.clear()
  62. protocol.setTimeout(None)
  63. def test_deferredChat(self):
  64. """
  65. Test that I{get} and I{put} commands are responded to correctly by
  66. L{postfix.PostfixTCPMapServer} when its factory is an instance of
  67. L{postifx.PostfixTCPMapDeferringDictServerFactory}.
  68. """
  69. factory = postfix.PostfixTCPMapDeferringDictServerFactory(self.data)
  70. transport = StringTransport()
  71. protocol = postfix.PostfixTCPMapServer()
  72. protocol.service = factory
  73. protocol.factory = factory
  74. protocol.makeConnection(transport)
  75. for input, expected_output in self.chat:
  76. protocol.lineReceived(input)
  77. self.assertEqual(
  78. transport.value(),
  79. expected_output,
  80. "For {!r}, expected {!r} but got {!r}".format(
  81. input, expected_output, transport.value()
  82. ),
  83. )
  84. transport.clear()
  85. protocol.setTimeout(None)
  86. def test_getException(self):
  87. """
  88. If the factory throws an exception,
  89. error code 400 must be returned.
  90. """
  91. class ErrorFactory:
  92. """
  93. Factory that raises an error on key lookup.
  94. """
  95. def get(self, key):
  96. raise Exception("This is a test error")
  97. server = postfix.PostfixTCPMapServer()
  98. server.factory = ErrorFactory()
  99. server.transport = StringTransport()
  100. server.lineReceived(b"get example")
  101. self.assertEqual(server.transport.value(), b"400 This is a test error\n")
  102. class ValidTests(PostfixTCPMapServerTestCase, unittest.TestCase):
  103. data = {
  104. b"foo": b"ThisIs Foo",
  105. b"bar": b" bar really is found\r\n",
  106. }
  107. chat = [
  108. (b"get", b"400 Command 'get' takes 1 parameters.\n"),
  109. (b"get foo bar", b"500 \n"),
  110. (b"put", b"400 Command 'put' takes 2 parameters.\n"),
  111. (b"put foo", b"400 Command 'put' takes 2 parameters.\n"),
  112. (b"put foo bar baz", b"500 put is not implemented yet.\n"),
  113. (b"put foo bar", b"500 put is not implemented yet.\n"),
  114. (b"get foo", b"200 ThisIs%20Foo\n"),
  115. (b"get bar", b"200 %20bar%20really%20is%20found%0D%0A\n"),
  116. (b"get baz", b"500 \n"),
  117. (b"foo", b"400 unknown command\n"),
  118. ]