1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 Test cases for twisted.protocols.postfix module.
8 from twisted.trial import unittest
9 from twisted.protocols import postfix
10 from twisted.test.proto_helpers import StringTransport
13 class PostfixTCPMapQuoteTestCase(unittest.TestCase):
15 # (raw, quoted, [aliasQuotedForms]),
17 ('foo bar', 'foo%20bar'),
18 ('foo\tbar', 'foo%09bar'),
19 ('foo\nbar', 'foo%0Abar', 'foo%0abar'),
20 ('foo\r\nbar', 'foo%0D%0Abar', 'foo%0D%0abar', 'foo%0d%0Abar', 'foo%0d%0abar'),
26 for entry in self.data:
30 self.assertEqual(postfix.quote(raw), quoted[0])
32 self.assertEqual(postfix.unquote(q), raw)
34 class PostfixTCPMapServerTestCase:
40 # (input, expected_output),
45 Test that I{get} and I{put} commands are responded to correctly by
46 L{postfix.PostfixTCPMapServer} when its factory is an instance of
47 L{postifx.PostfixTCPMapDictServerFactory}.
49 factory = postfix.PostfixTCPMapDictServerFactory(self.data)
50 transport = StringTransport()
52 protocol = postfix.PostfixTCPMapServer()
53 protocol.service = factory
54 protocol.factory = factory
55 protocol.makeConnection(transport)
57 for input, expected_output in self.chat:
58 protocol.lineReceived(input)
60 transport.value(), expected_output,
61 'For %r, expected %r but got %r' % (
62 input, expected_output, transport.value()))
64 protocol.setTimeout(None)
67 def test_deferredChat(self):
69 Test that I{get} and I{put} commands are responded to correctly by
70 L{postfix.PostfixTCPMapServer} when its factory is an instance of
71 L{postifx.PostfixTCPMapDeferringDictServerFactory}.
73 factory = postfix.PostfixTCPMapDeferringDictServerFactory(self.data)
74 transport = StringTransport()
76 protocol = postfix.PostfixTCPMapServer()
77 protocol.service = factory
78 protocol.factory = factory
79 protocol.makeConnection(transport)
81 for input, expected_output in self.chat:
82 protocol.lineReceived(input)
84 transport.value(), expected_output,
85 'For %r, expected %r but got %r' % (
86 input, expected_output, transport.value()))
88 protocol.setTimeout(None)
92 class Valid(PostfixTCPMapServerTestCase, unittest.TestCase):
95 'bar': ' bar really is found\r\n',
98 ('get', "400 Command 'get' takes 1 parameters.\n"),
99 ('get foo bar', "500 \n"),
100 ('put', "400 Command 'put' takes 2 parameters.\n"),
101 ('put foo', "400 Command 'put' takes 2 parameters.\n"),
102 ('put foo bar baz', "500 put is not implemented yet.\n"),
103 ('put foo bar', '500 put is not implemented yet.\n'),
104 ('get foo', '200 ThisIs%20Foo\n'),
105 ('get bar', '200 %20bar%20really%20is%20found%0D%0A\n'),
106 ('get baz', '500 \n'),
107 ('foo', '400 unknown command\n'),