Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / test / test_postfix.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Test cases for twisted.protocols.postfix module.
6 """
7
8 from twisted.trial import unittest
9 from twisted.protocols import postfix
10 from twisted.test.proto_helpers import StringTransport
11
12
13 class PostfixTCPMapQuoteTestCase(unittest.TestCase):
14     data = [
15         # (raw, quoted, [aliasQuotedForms]),
16         ('foo', 'foo'),
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'),
21         ('foo ', 'foo%20'),
22         (' foo', '%20foo'),
23         ]
24
25     def testData(self):
26         for entry in self.data:
27             raw = entry[0]
28             quoted = entry[1:]
29
30             self.assertEqual(postfix.quote(raw), quoted[0])
31             for q in quoted:
32                 self.assertEqual(postfix.unquote(q), raw)
33
34 class PostfixTCPMapServerTestCase:
35     data = {
36         # 'key': 'value',
37         }
38
39     chat = [
40         # (input, expected_output),
41         ]
42
43     def test_chat(self):
44         """
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}.
48         """
49         factory = postfix.PostfixTCPMapDictServerFactory(self.data)
50         transport = StringTransport()
51
52         protocol = postfix.PostfixTCPMapServer()
53         protocol.service = factory
54         protocol.factory = factory
55         protocol.makeConnection(transport)
56
57         for input, expected_output in self.chat:
58             protocol.lineReceived(input)
59             self.assertEqual(
60                 transport.value(), expected_output,
61                 'For %r, expected %r but got %r' % (
62                     input, expected_output, transport.value()))
63             transport.clear()
64         protocol.setTimeout(None)
65
66
67     def test_deferredChat(self):
68         """
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}.
72         """
73         factory = postfix.PostfixTCPMapDeferringDictServerFactory(self.data)
74         transport = StringTransport()
75
76         protocol = postfix.PostfixTCPMapServer()
77         protocol.service = factory
78         protocol.factory = factory
79         protocol.makeConnection(transport)
80
81         for input, expected_output in self.chat:
82             protocol.lineReceived(input)
83             self.assertEqual(
84                 transport.value(), expected_output,
85                 'For %r, expected %r but got %r' % (
86                     input, expected_output, transport.value()))
87             transport.clear()
88         protocol.setTimeout(None)
89
90
91
92 class Valid(PostfixTCPMapServerTestCase, unittest.TestCase):
93     data = {
94         'foo': 'ThisIs Foo',
95         'bar': ' bar really is found\r\n',
96         }
97     chat = [
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'),
108         ]