blob: 6a32cb6b625eb7b8ca8b4c68240d68ef700f490a [file] [log] [blame]
Guido van Rossum2ad816f1998-04-23 13:33:56 +00001"""Test program for MimeWriter module.
2
3The test program was too big to comfortably fit in the MimeWriter
4class, so it's here in its own file.
5
6This should generate Barry's example, modulo some quotes and newlines.
7
8"""
9
Christian Heimesc5f05e42008-02-23 17:40:11 +000010import unittest, StringIO
Georg Brandle8328ba2006-10-29 20:28:26 +000011from test.test_support import run_unittest
Guido van Rossum2ad816f1998-04-23 13:33:56 +000012
Brett Cannon90134c92007-05-30 20:46:26 +000013import warnings
14warnings.filterwarnings("ignore", "the MimeWriter module is deprecated.*",
15 DeprecationWarning)
16
Guido van Rossum2ad816f1998-04-23 13:33:56 +000017from MimeWriter import MimeWriter
18
19SELLER = '''\
20INTERFACE Seller-1;
21
22TYPE Seller = OBJECT
23 DOCUMENTATION "A simple Seller interface to test ILU"
24 METHODS
Guido van Rossum068ad971998-06-09 19:19:40 +000025 price():INTEGER,
Guido van Rossum2ad816f1998-04-23 13:33:56 +000026 END;
27'''
28
29BUYER = '''\
30class Buyer:
31 def __setup__(self, maxprice):
Guido van Rossum068ad971998-06-09 19:19:40 +000032 self._maxprice = maxprice
Guido van Rossum2ad816f1998-04-23 13:33:56 +000033
34 def __main__(self, kos):
Guido van Rossum068ad971998-06-09 19:19:40 +000035 """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
Guido van Rossum2ad816f1998-04-23 13:33:56 +000049
50STATE = '''\
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'''
56
57SIMPLE_METADATA = [
Guido van Rossum068ad971998-06-09 19:19:40 +000058 ("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"),
65 ]
Guido van Rossum2ad816f1998-04-23 13:33:56 +000066
67COMPLEX_METADATA = [
Guido van Rossum068ad971998-06-09 19:19:40 +000068 ("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"),
74 ]
Guido van Rossum2ad816f1998-04-23 13:33:56 +000075
76EXTERNAL_METADATA = [
Guido van Rossum068ad971998-06-09 19:19:40 +000077 ("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"),
83 ]
Guido van Rossum2ad816f1998-04-23 13:33:56 +000084
85
Georg Brandle8328ba2006-10-29 20:28:26 +000086OUTPUT = '''\
87From: bwarsaw@cnri.reston.va.us
88Date: Mon Feb 12 17:21:48 EST 1996
89To: kss-submit@cnri.reston.va.us
90MIME-Version: 1.0
91Content-Type: multipart/knowbot;
92 boundary="801spam999";
93 version="0.1"
Guido van Rossum2ad816f1998-04-23 13:33:56 +000094
Georg Brandle8328ba2006-10-29 20:28:26 +000095This is a multi-part message in MIME format.
Fred Drake004d5e62000-10-23 17:22:08 +000096
Georg Brandle8328ba2006-10-29 20:28:26 +000097--801spam999
98Content-Type: multipart/knowbot-metadata;
99 boundary="802spam999"
Guido van Rossum2ad816f1998-04-23 13:33:56 +0000100
101
Georg Brandle8328ba2006-10-29 20:28:26 +0000102--802spam999
103Content-Type: message/rfc822
104KP-Metadata-Type: simple
105KP-Access: read-only
106
107KPMD-Interpreter: python
108KPMD-Interpreter-Version: 1.3
109KPMD-Owner-Name: Barry Warsaw
110KPMD-Owner-Rendezvous: bwarsaw@cnri.reston.va.us
111KPMD-Home-KSS: kss.cnri.reston.va.us
112KPMD-Identifier: hdl://cnri.kss/my_first_knowbot
113KPMD-Launch-Date: Mon Feb 12 16:39:03 EST 1996
114
115--802spam999
116Content-Type: text/isl
117KP-Metadata-Type: complex
118KP-Metadata-Key: connection
119KP-Access: read-only
120KP-Connection-Description: Barry's Big Bass Business
121KP-Connection-Id: B4
122KP-Connection-Direction: client
123
124INTERFACE Seller-1;
125
126TYPE Seller = OBJECT
127 DOCUMENTATION "A simple Seller interface to test ILU"
128 METHODS
129 price():INTEGER,
130 END;
131
132--802spam999
133Content-Type: message/external-body;
134 access-type="URL";
135 URL="hdl://cnri.kss/generic-knowbot"
136
137Content-Type: text/isl
138KP-Metadata-Type: complex
139KP-Metadata-Key: generic-interface
140KP-Access: read-only
141KP-Connection-Description: Generic Interface for All Knowbots
142KP-Connection-Id: generic-kp
143KP-Connection-Direction: client
144
145
146--802spam999--
147
148--801spam999
149Content-Type: multipart/knowbot-code;
150 boundary="803spam999"
151
152
153--803spam999
154Content-Type: text/plain
155KP-Module-Name: BuyerKP
156
157class Buyer:
158 def __setup__(self, maxprice):
159 self._maxprice = maxprice
160
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'
175
176--803spam999--
177
178--801spam999
179Content-Type: multipart/knowbot-state;
180 boundary="804spam999"
181KP-Main-Module: main
182
183
184--804spam999
185Content-Type: text/plain
186KP-Module-Name: main
187
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)
192
193--804spam999--
194
195--801spam999--
196'''
197
198class MimewriterTest(unittest.TestCase):
Tim Petersabd8a332006-11-03 02:32:46 +0000199
Georg Brandle8328ba2006-10-29 20:28:26 +0000200 def test(self):
201 buf = StringIO.StringIO()
202
203 # Toplevel headers
204
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")
210
211 # Toplevel body parts
212
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")
216
217 # First toplevel body part: metadata
218
219 md = toplevel.nextpart()
220 md.startmultipartbody("knowbot-metadata", "802spam999")
221
222 # Metadata part 1
223
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
232
233 # Metadata part 2
234
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
241
242 # Metadata part 3
243
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
253
254 md.lastpart()
255
256 # Second toplevel body part: code
257
258 code = toplevel.nextpart()
259 code.startmultipartbody("knowbot-code", "803spam999")
260
261 # Code: buyer program source
262
263 buyer = code.nextpart()
264 buyer.addheader("KP-Module-Name", "BuyerKP")
265 f = buyer.startbody("text/plain")
266 f.write(BUYER)
267
268 code.lastpart()
269
270 # Third toplevel body part: state
271
272 state = toplevel.nextpart()
273 state.addheader("KP-Main-Module", "main")
274 state.startmultipartbody("knowbot-state", "804spam999")
275
276 # State: a bunch of assignments
277
278 st = state.nextpart()
279 st.addheader("KP-Module-Name", "main")
280 f = st.startbody("text/plain")
281 f.write(STATE)
282
283 state.lastpart()
284
285 # End toplevel body parts
286
287 toplevel.lastpart()
288
289 self.assertEqual(buf.getvalue(), OUTPUT)
290
291def test_main():
292 run_unittest(MimewriterTest)
293
294if __name__ == '__main__':
295 test_main()