Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` method returning
[python.git] / Lib / test / test_MimeWriter.py
blob6a32cb6b625eb7b8ca8b4c68240d68ef700f490a
1 """Test program for MimeWriter module.
3 The test program was too big to comfortably fit in the MimeWriter
4 class, so it's here in its own file.
6 This should generate Barry's example, modulo some quotes and newlines.
8 """
10 import unittest, StringIO
11 from test.test_support import run_unittest
13 import warnings
14 warnings.filterwarnings("ignore", "the MimeWriter module is deprecated.*",
15 DeprecationWarning)
17 from MimeWriter import MimeWriter
19 SELLER = '''\
20 INTERFACE Seller-1;
22 TYPE Seller = OBJECT
23 DOCUMENTATION "A simple Seller interface to test ILU"
24 METHODS
25 price():INTEGER,
26 END;
27 '''
29 BUYER = '''\
30 class Buyer:
31 def __setup__(self, maxprice):
32 self._maxprice = maxprice
34 def __main__(self, kos):
35 """Entry point upon arrival at a new KOS."""
36 broker = kos.broker()
37 # B4 == Barry's Big Bass Business :-)
38 seller = broker.lookup('Seller_1.Seller', 'B4')
39 if seller:
40 price = seller.price()
41 print 'Seller wants $', price, '... '
42 if price > self._maxprice:
43 print 'too much!'
44 else:
45 print "I'll take it!"
46 else:
47 print 'no seller found here'
48 ''' # Don't ask why this comment is here
50 STATE = '''\
51 # instantiate a buyer instance and put it in a magic place for the KOS
52 # to find.
53 __kp__ = Buyer()
54 __kp__.__setup__(500)
55 '''
57 SIMPLE_METADATA = [
58 ("Interpreter", "python"),
59 ("Interpreter-Version", "1.3"),
60 ("Owner-Name", "Barry Warsaw"),
61 ("Owner-Rendezvous", "bwarsaw@cnri.reston.va.us"),
62 ("Home-KSS", "kss.cnri.reston.va.us"),
63 ("Identifier", "hdl://cnri.kss/my_first_knowbot"),
64 ("Launch-Date", "Mon Feb 12 16:39:03 EST 1996"),
67 COMPLEX_METADATA = [
68 ("Metadata-Type", "complex"),
69 ("Metadata-Key", "connection"),
70 ("Access", "read-only"),
71 ("Connection-Description", "Barry's Big Bass Business"),
72 ("Connection-Id", "B4"),
73 ("Connection-Direction", "client"),
76 EXTERNAL_METADATA = [
77 ("Metadata-Type", "complex"),
78 ("Metadata-Key", "generic-interface"),
79 ("Access", "read-only"),
80 ("Connection-Description", "Generic Interface for All Knowbots"),
81 ("Connection-Id", "generic-kp"),
82 ("Connection-Direction", "client"),
86 OUTPUT = '''\
87 From: bwarsaw@cnri.reston.va.us
88 Date: Mon Feb 12 17:21:48 EST 1996
89 To: kss-submit@cnri.reston.va.us
90 MIME-Version: 1.0
91 Content-Type: multipart/knowbot;
92 boundary="801spam999";
93 version="0.1"
95 This is a multi-part message in MIME format.
97 --801spam999
98 Content-Type: multipart/knowbot-metadata;
99 boundary="802spam999"
102 --802spam999
103 Content-Type: message/rfc822
104 KP-Metadata-Type: simple
105 KP-Access: read-only
107 KPMD-Interpreter: python
108 KPMD-Interpreter-Version: 1.3
109 KPMD-Owner-Name: Barry Warsaw
110 KPMD-Owner-Rendezvous: bwarsaw@cnri.reston.va.us
111 KPMD-Home-KSS: kss.cnri.reston.va.us
112 KPMD-Identifier: hdl://cnri.kss/my_first_knowbot
113 KPMD-Launch-Date: Mon Feb 12 16:39:03 EST 1996
115 --802spam999
116 Content-Type: text/isl
117 KP-Metadata-Type: complex
118 KP-Metadata-Key: connection
119 KP-Access: read-only
120 KP-Connection-Description: Barry's Big Bass Business
121 KP-Connection-Id: B4
122 KP-Connection-Direction: client
124 INTERFACE Seller-1;
126 TYPE Seller = OBJECT
127 DOCUMENTATION "A simple Seller interface to test ILU"
128 METHODS
129 price():INTEGER,
130 END;
132 --802spam999
133 Content-Type: message/external-body;
134 access-type="URL";
135 URL="hdl://cnri.kss/generic-knowbot"
137 Content-Type: text/isl
138 KP-Metadata-Type: complex
139 KP-Metadata-Key: generic-interface
140 KP-Access: read-only
141 KP-Connection-Description: Generic Interface for All Knowbots
142 KP-Connection-Id: generic-kp
143 KP-Connection-Direction: client
146 --802spam999--
148 --801spam999
149 Content-Type: multipart/knowbot-code;
150 boundary="803spam999"
153 --803spam999
154 Content-Type: text/plain
155 KP-Module-Name: BuyerKP
157 class Buyer:
158 def __setup__(self, maxprice):
159 self._maxprice = maxprice
161 def __main__(self, kos):
162 """Entry point upon arrival at a new KOS."""
163 broker = kos.broker()
164 # B4 == Barry's Big Bass Business :-)
165 seller = broker.lookup('Seller_1.Seller', 'B4')
166 if seller:
167 price = seller.price()
168 print 'Seller wants $', price, '... '
169 if price > self._maxprice:
170 print 'too much!'
171 else:
172 print "I'll take it!"
173 else:
174 print 'no seller found here'
176 --803spam999--
178 --801spam999
179 Content-Type: multipart/knowbot-state;
180 boundary="804spam999"
181 KP-Main-Module: main
184 --804spam999
185 Content-Type: text/plain
186 KP-Module-Name: main
188 # instantiate a buyer instance and put it in a magic place for the KOS
189 # to find.
190 __kp__ = Buyer()
191 __kp__.__setup__(500)
193 --804spam999--
195 --801spam999--
198 class MimewriterTest(unittest.TestCase):
200 def test(self):
201 buf = StringIO.StringIO()
203 # Toplevel headers
205 toplevel = MimeWriter(buf)
206 toplevel.addheader("From", "bwarsaw@cnri.reston.va.us")
207 toplevel.addheader("Date", "Mon Feb 12 17:21:48 EST 1996")
208 toplevel.addheader("To", "kss-submit@cnri.reston.va.us")
209 toplevel.addheader("MIME-Version", "1.0")
211 # Toplevel body parts
213 f = toplevel.startmultipartbody("knowbot", "801spam999",
214 [("version", "0.1")], prefix=0)
215 f.write("This is a multi-part message in MIME format.\n")
217 # First toplevel body part: metadata
219 md = toplevel.nextpart()
220 md.startmultipartbody("knowbot-metadata", "802spam999")
222 # Metadata part 1
224 md1 = md.nextpart()
225 md1.addheader("KP-Metadata-Type", "simple")
226 md1.addheader("KP-Access", "read-only")
227 m = MimeWriter(md1.startbody("message/rfc822"))
228 for key, value in SIMPLE_METADATA:
229 m.addheader("KPMD-" + key, value)
230 m.flushheaders()
231 del md1
233 # Metadata part 2
235 md2 = md.nextpart()
236 for key, value in COMPLEX_METADATA:
237 md2.addheader("KP-" + key, value)
238 f = md2.startbody("text/isl")
239 f.write(SELLER)
240 del md2
242 # Metadata part 3
244 md3 = md.nextpart()
245 f = md3.startbody("message/external-body",
246 [("access-type", "URL"),
247 ("URL", "hdl://cnri.kss/generic-knowbot")])
248 m = MimeWriter(f)
249 for key, value in EXTERNAL_METADATA:
250 md3.addheader("KP-" + key, value)
251 md3.startbody("text/isl")
252 # Phantom body doesn't need to be written
254 md.lastpart()
256 # Second toplevel body part: code
258 code = toplevel.nextpart()
259 code.startmultipartbody("knowbot-code", "803spam999")
261 # Code: buyer program source
263 buyer = code.nextpart()
264 buyer.addheader("KP-Module-Name", "BuyerKP")
265 f = buyer.startbody("text/plain")
266 f.write(BUYER)
268 code.lastpart()
270 # Third toplevel body part: state
272 state = toplevel.nextpart()
273 state.addheader("KP-Main-Module", "main")
274 state.startmultipartbody("knowbot-state", "804spam999")
276 # State: a bunch of assignments
278 st = state.nextpart()
279 st.addheader("KP-Module-Name", "main")
280 f = st.startbody("text/plain")
281 f.write(STATE)
283 state.lastpart()
285 # End toplevel body parts
287 toplevel.lastpart()
289 self.assertEqual(buf.getvalue(), OUTPUT)
291 def test_main():
292 run_unittest(MimewriterTest)
294 if __name__ == '__main__':
295 test_main()