blob: feca1631f2b34f0a870c77f22c94ad647f3ba2ab [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
Georg Brandle8328ba2006-10-29 20:28:26 +000010import unittest, sys, StringIO
11from test.test_support import run_unittest
Guido van Rossum2ad816f1998-04-23 13:33:56 +000012
13from MimeWriter import MimeWriter
14
15SELLER = '''\
16INTERFACE Seller-1;
17
18TYPE Seller = OBJECT
19 DOCUMENTATION "A simple Seller interface to test ILU"
20 METHODS
Guido van Rossum068ad971998-06-09 19:19:40 +000021 price():INTEGER,
Guido van Rossum2ad816f1998-04-23 13:33:56 +000022 END;
23'''
24
25BUYER = '''\
26class Buyer:
27 def __setup__(self, maxprice):
Guido van Rossum068ad971998-06-09 19:19:40 +000028 self._maxprice = maxprice
Guido van Rossum2ad816f1998-04-23 13:33:56 +000029
30 def __main__(self, kos):
Guido van Rossum068ad971998-06-09 19:19:40 +000031 """Entry point upon arrival at a new KOS."""
32 broker = kos.broker()
33 # B4 == Barry's Big Bass Business :-)
34 seller = broker.lookup('Seller_1.Seller', 'B4')
35 if seller:
36 price = seller.price()
37 print 'Seller wants $', price, '... '
38 if price > self._maxprice:
39 print 'too much!'
40 else:
41 print "I'll take it!"
42 else:
43 print 'no seller found here'
44''' # Don't ask why this comment is here
Guido van Rossum2ad816f1998-04-23 13:33:56 +000045
46STATE = '''\
47# instantiate a buyer instance and put it in a magic place for the KOS
48# to find.
49__kp__ = Buyer()
50__kp__.__setup__(500)
51'''
52
53SIMPLE_METADATA = [
Guido van Rossum068ad971998-06-09 19:19:40 +000054 ("Interpreter", "python"),
55 ("Interpreter-Version", "1.3"),
56 ("Owner-Name", "Barry Warsaw"),
57 ("Owner-Rendezvous", "bwarsaw@cnri.reston.va.us"),
58 ("Home-KSS", "kss.cnri.reston.va.us"),
59 ("Identifier", "hdl://cnri.kss/my_first_knowbot"),
60 ("Launch-Date", "Mon Feb 12 16:39:03 EST 1996"),
61 ]
Guido van Rossum2ad816f1998-04-23 13:33:56 +000062
63COMPLEX_METADATA = [
Guido van Rossum068ad971998-06-09 19:19:40 +000064 ("Metadata-Type", "complex"),
65 ("Metadata-Key", "connection"),
66 ("Access", "read-only"),
67 ("Connection-Description", "Barry's Big Bass Business"),
68 ("Connection-Id", "B4"),
69 ("Connection-Direction", "client"),
70 ]
Guido van Rossum2ad816f1998-04-23 13:33:56 +000071
72EXTERNAL_METADATA = [
Guido van Rossum068ad971998-06-09 19:19:40 +000073 ("Metadata-Type", "complex"),
74 ("Metadata-Key", "generic-interface"),
75 ("Access", "read-only"),
76 ("Connection-Description", "Generic Interface for All Knowbots"),
77 ("Connection-Id", "generic-kp"),
78 ("Connection-Direction", "client"),
79 ]
Guido van Rossum2ad816f1998-04-23 13:33:56 +000080
81
Georg Brandle8328ba2006-10-29 20:28:26 +000082OUTPUT = '''\
83From: bwarsaw@cnri.reston.va.us
84Date: Mon Feb 12 17:21:48 EST 1996
85To: kss-submit@cnri.reston.va.us
86MIME-Version: 1.0
87Content-Type: multipart/knowbot;
88 boundary="801spam999";
89 version="0.1"
Guido van Rossum2ad816f1998-04-23 13:33:56 +000090
Georg Brandle8328ba2006-10-29 20:28:26 +000091This is a multi-part message in MIME format.
Fred Drake004d5e62000-10-23 17:22:08 +000092
Georg Brandle8328ba2006-10-29 20:28:26 +000093--801spam999
94Content-Type: multipart/knowbot-metadata;
95 boundary="802spam999"
Guido van Rossum2ad816f1998-04-23 13:33:56 +000096
97
Georg Brandle8328ba2006-10-29 20:28:26 +000098--802spam999
99Content-Type: message/rfc822
100KP-Metadata-Type: simple
101KP-Access: read-only
102
103KPMD-Interpreter: python
104KPMD-Interpreter-Version: 1.3
105KPMD-Owner-Name: Barry Warsaw
106KPMD-Owner-Rendezvous: bwarsaw@cnri.reston.va.us
107KPMD-Home-KSS: kss.cnri.reston.va.us
108KPMD-Identifier: hdl://cnri.kss/my_first_knowbot
109KPMD-Launch-Date: Mon Feb 12 16:39:03 EST 1996
110
111--802spam999
112Content-Type: text/isl
113KP-Metadata-Type: complex
114KP-Metadata-Key: connection
115KP-Access: read-only
116KP-Connection-Description: Barry's Big Bass Business
117KP-Connection-Id: B4
118KP-Connection-Direction: client
119
120INTERFACE Seller-1;
121
122TYPE Seller = OBJECT
123 DOCUMENTATION "A simple Seller interface to test ILU"
124 METHODS
125 price():INTEGER,
126 END;
127
128--802spam999
129Content-Type: message/external-body;
130 access-type="URL";
131 URL="hdl://cnri.kss/generic-knowbot"
132
133Content-Type: text/isl
134KP-Metadata-Type: complex
135KP-Metadata-Key: generic-interface
136KP-Access: read-only
137KP-Connection-Description: Generic Interface for All Knowbots
138KP-Connection-Id: generic-kp
139KP-Connection-Direction: client
140
141
142--802spam999--
143
144--801spam999
145Content-Type: multipart/knowbot-code;
146 boundary="803spam999"
147
148
149--803spam999
150Content-Type: text/plain
151KP-Module-Name: BuyerKP
152
153class Buyer:
154 def __setup__(self, maxprice):
155 self._maxprice = maxprice
156
157 def __main__(self, kos):
158 """Entry point upon arrival at a new KOS."""
159 broker = kos.broker()
160 # B4 == Barry's Big Bass Business :-)
161 seller = broker.lookup('Seller_1.Seller', 'B4')
162 if seller:
163 price = seller.price()
164 print 'Seller wants $', price, '... '
165 if price > self._maxprice:
166 print 'too much!'
167 else:
168 print "I'll take it!"
169 else:
170 print 'no seller found here'
171
172--803spam999--
173
174--801spam999
175Content-Type: multipart/knowbot-state;
176 boundary="804spam999"
177KP-Main-Module: main
178
179
180--804spam999
181Content-Type: text/plain
182KP-Module-Name: main
183
184# instantiate a buyer instance and put it in a magic place for the KOS
185# to find.
186__kp__ = Buyer()
187__kp__.__setup__(500)
188
189--804spam999--
190
191--801spam999--
192'''
193
194class MimewriterTest(unittest.TestCase):
Tim Petersabd8a332006-11-03 02:32:46 +0000195
Georg Brandle8328ba2006-10-29 20:28:26 +0000196 def test(self):
197 buf = StringIO.StringIO()
198
199 # Toplevel headers
200
201 toplevel = MimeWriter(buf)
202 toplevel.addheader("From", "bwarsaw@cnri.reston.va.us")
203 toplevel.addheader("Date", "Mon Feb 12 17:21:48 EST 1996")
204 toplevel.addheader("To", "kss-submit@cnri.reston.va.us")
205 toplevel.addheader("MIME-Version", "1.0")
206
207 # Toplevel body parts
208
209 f = toplevel.startmultipartbody("knowbot", "801spam999",
210 [("version", "0.1")], prefix=0)
211 f.write("This is a multi-part message in MIME format.\n")
212
213 # First toplevel body part: metadata
214
215 md = toplevel.nextpart()
216 md.startmultipartbody("knowbot-metadata", "802spam999")
217
218 # Metadata part 1
219
220 md1 = md.nextpart()
221 md1.addheader("KP-Metadata-Type", "simple")
222 md1.addheader("KP-Access", "read-only")
223 m = MimeWriter(md1.startbody("message/rfc822"))
224 for key, value in SIMPLE_METADATA:
225 m.addheader("KPMD-" + key, value)
226 m.flushheaders()
227 del md1
228
229 # Metadata part 2
230
231 md2 = md.nextpart()
232 for key, value in COMPLEX_METADATA:
233 md2.addheader("KP-" + key, value)
234 f = md2.startbody("text/isl")
235 f.write(SELLER)
236 del md2
237
238 # Metadata part 3
239
240 md3 = md.nextpart()
241 f = md3.startbody("message/external-body",
242 [("access-type", "URL"),
243 ("URL", "hdl://cnri.kss/generic-knowbot")])
244 m = MimeWriter(f)
245 for key, value in EXTERNAL_METADATA:
246 md3.addheader("KP-" + key, value)
247 md3.startbody("text/isl")
248 # Phantom body doesn't need to be written
249
250 md.lastpart()
251
252 # Second toplevel body part: code
253
254 code = toplevel.nextpart()
255 code.startmultipartbody("knowbot-code", "803spam999")
256
257 # Code: buyer program source
258
259 buyer = code.nextpart()
260 buyer.addheader("KP-Module-Name", "BuyerKP")
261 f = buyer.startbody("text/plain")
262 f.write(BUYER)
263
264 code.lastpart()
265
266 # Third toplevel body part: state
267
268 state = toplevel.nextpart()
269 state.addheader("KP-Main-Module", "main")
270 state.startmultipartbody("knowbot-state", "804spam999")
271
272 # State: a bunch of assignments
273
274 st = state.nextpart()
275 st.addheader("KP-Module-Name", "main")
276 f = st.startbody("text/plain")
277 f.write(STATE)
278
279 state.lastpart()
280
281 # End toplevel body parts
282
283 toplevel.lastpart()
284
285 self.assertEqual(buf.getvalue(), OUTPUT)
286
287def test_main():
288 run_unittest(MimewriterTest)
289
290if __name__ == '__main__':
291 test_main()