move sections
[python/dscho.git] / Lib / test / test_MimeWriter.py
blob2f6636dfdf13a84b994efc64bbf2549d0989442a
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, import_module
13 import_module("MimeWriter", deprecated=True)
14 from MimeWriter import MimeWriter
16 SELLER = '''\
17 INTERFACE Seller-1;
19 TYPE Seller = OBJECT
20 DOCUMENTATION "A simple Seller interface to test ILU"
21 METHODS
22 price():INTEGER,
23 END;
24 '''
26 BUYER = '''\
27 class Buyer:
28 def __setup__(self, maxprice):
29 self._maxprice = maxprice
31 def __main__(self, kos):
32 """Entry point upon arrival at a new KOS."""
33 broker = kos.broker()
34 # B4 == Barry's Big Bass Business :-)
35 seller = broker.lookup('Seller_1.Seller', 'B4')
36 if seller:
37 price = seller.price()
38 print 'Seller wants $', price, '... '
39 if price > self._maxprice:
40 print 'too much!'
41 else:
42 print "I'll take it!"
43 else:
44 print 'no seller found here'
45 ''' # Don't ask why this comment is here
47 STATE = '''\
48 # instantiate a buyer instance and put it in a magic place for the KOS
49 # to find.
50 __kp__ = Buyer()
51 __kp__.__setup__(500)
52 '''
54 SIMPLE_METADATA = [
55 ("Interpreter", "python"),
56 ("Interpreter-Version", "1.3"),
57 ("Owner-Name", "Barry Warsaw"),
58 ("Owner-Rendezvous", "bwarsaw@cnri.reston.va.us"),
59 ("Home-KSS", "kss.cnri.reston.va.us"),
60 ("Identifier", "hdl://cnri.kss/my_first_knowbot"),
61 ("Launch-Date", "Mon Feb 12 16:39:03 EST 1996"),
64 COMPLEX_METADATA = [
65 ("Metadata-Type", "complex"),
66 ("Metadata-Key", "connection"),
67 ("Access", "read-only"),
68 ("Connection-Description", "Barry's Big Bass Business"),
69 ("Connection-Id", "B4"),
70 ("Connection-Direction", "client"),
73 EXTERNAL_METADATA = [
74 ("Metadata-Type", "complex"),
75 ("Metadata-Key", "generic-interface"),
76 ("Access", "read-only"),
77 ("Connection-Description", "Generic Interface for All Knowbots"),
78 ("Connection-Id", "generic-kp"),
79 ("Connection-Direction", "client"),
83 OUTPUT = '''\
84 From: bwarsaw@cnri.reston.va.us
85 Date: Mon Feb 12 17:21:48 EST 1996
86 To: kss-submit@cnri.reston.va.us
87 MIME-Version: 1.0
88 Content-Type: multipart/knowbot;
89 boundary="801spam999";
90 version="0.1"
92 This is a multi-part message in MIME format.
94 --801spam999
95 Content-Type: multipart/knowbot-metadata;
96 boundary="802spam999"
99 --802spam999
100 Content-Type: message/rfc822
101 KP-Metadata-Type: simple
102 KP-Access: read-only
104 KPMD-Interpreter: python
105 KPMD-Interpreter-Version: 1.3
106 KPMD-Owner-Name: Barry Warsaw
107 KPMD-Owner-Rendezvous: bwarsaw@cnri.reston.va.us
108 KPMD-Home-KSS: kss.cnri.reston.va.us
109 KPMD-Identifier: hdl://cnri.kss/my_first_knowbot
110 KPMD-Launch-Date: Mon Feb 12 16:39:03 EST 1996
112 --802spam999
113 Content-Type: text/isl
114 KP-Metadata-Type: complex
115 KP-Metadata-Key: connection
116 KP-Access: read-only
117 KP-Connection-Description: Barry's Big Bass Business
118 KP-Connection-Id: B4
119 KP-Connection-Direction: client
121 INTERFACE Seller-1;
123 TYPE Seller = OBJECT
124 DOCUMENTATION "A simple Seller interface to test ILU"
125 METHODS
126 price():INTEGER,
127 END;
129 --802spam999
130 Content-Type: message/external-body;
131 access-type="URL";
132 URL="hdl://cnri.kss/generic-knowbot"
134 Content-Type: text/isl
135 KP-Metadata-Type: complex
136 KP-Metadata-Key: generic-interface
137 KP-Access: read-only
138 KP-Connection-Description: Generic Interface for All Knowbots
139 KP-Connection-Id: generic-kp
140 KP-Connection-Direction: client
143 --802spam999--
145 --801spam999
146 Content-Type: multipart/knowbot-code;
147 boundary="803spam999"
150 --803spam999
151 Content-Type: text/plain
152 KP-Module-Name: BuyerKP
154 class Buyer:
155 def __setup__(self, maxprice):
156 self._maxprice = maxprice
158 def __main__(self, kos):
159 """Entry point upon arrival at a new KOS."""
160 broker = kos.broker()
161 # B4 == Barry's Big Bass Business :-)
162 seller = broker.lookup('Seller_1.Seller', 'B4')
163 if seller:
164 price = seller.price()
165 print 'Seller wants $', price, '... '
166 if price > self._maxprice:
167 print 'too much!'
168 else:
169 print "I'll take it!"
170 else:
171 print 'no seller found here'
173 --803spam999--
175 --801spam999
176 Content-Type: multipart/knowbot-state;
177 boundary="804spam999"
178 KP-Main-Module: main
181 --804spam999
182 Content-Type: text/plain
183 KP-Module-Name: main
185 # instantiate a buyer instance and put it in a magic place for the KOS
186 # to find.
187 __kp__ = Buyer()
188 __kp__.__setup__(500)
190 --804spam999--
192 --801spam999--
195 class MimewriterTest(unittest.TestCase):
197 def test(self):
198 buf = StringIO.StringIO()
200 # Toplevel headers
202 toplevel = MimeWriter(buf)
203 toplevel.addheader("From", "bwarsaw@cnri.reston.va.us")
204 toplevel.addheader("Date", "Mon Feb 12 17:21:48 EST 1996")
205 toplevel.addheader("To", "kss-submit@cnri.reston.va.us")
206 toplevel.addheader("MIME-Version", "1.0")
208 # Toplevel body parts
210 f = toplevel.startmultipartbody("knowbot", "801spam999",
211 [("version", "0.1")], prefix=0)
212 f.write("This is a multi-part message in MIME format.\n")
214 # First toplevel body part: metadata
216 md = toplevel.nextpart()
217 md.startmultipartbody("knowbot-metadata", "802spam999")
219 # Metadata part 1
221 md1 = md.nextpart()
222 md1.addheader("KP-Metadata-Type", "simple")
223 md1.addheader("KP-Access", "read-only")
224 m = MimeWriter(md1.startbody("message/rfc822"))
225 for key, value in SIMPLE_METADATA:
226 m.addheader("KPMD-" + key, value)
227 m.flushheaders()
228 del md1
230 # Metadata part 2
232 md2 = md.nextpart()
233 for key, value in COMPLEX_METADATA:
234 md2.addheader("KP-" + key, value)
235 f = md2.startbody("text/isl")
236 f.write(SELLER)
237 del md2
239 # Metadata part 3
241 md3 = md.nextpart()
242 f = md3.startbody("message/external-body",
243 [("access-type", "URL"),
244 ("URL", "hdl://cnri.kss/generic-knowbot")])
245 m = MimeWriter(f)
246 for key, value in EXTERNAL_METADATA:
247 md3.addheader("KP-" + key, value)
248 md3.startbody("text/isl")
249 # Phantom body doesn't need to be written
251 md.lastpart()
253 # Second toplevel body part: code
255 code = toplevel.nextpart()
256 code.startmultipartbody("knowbot-code", "803spam999")
258 # Code: buyer program source
260 buyer = code.nextpart()
261 buyer.addheader("KP-Module-Name", "BuyerKP")
262 f = buyer.startbody("text/plain")
263 f.write(BUYER)
265 code.lastpart()
267 # Third toplevel body part: state
269 state = toplevel.nextpart()
270 state.addheader("KP-Main-Module", "main")
271 state.startmultipartbody("knowbot-state", "804spam999")
273 # State: a bunch of assignments
275 st = state.nextpart()
276 st.addheader("KP-Module-Name", "main")
277 f = st.startbody("text/plain")
278 f.write(STATE)
280 state.lastpart()
282 # End toplevel body parts
284 toplevel.lastpart()
286 self.assertEqual(buf.getvalue(), OUTPUT)
288 def test_main():
289 run_unittest(MimewriterTest)
291 if __name__ == '__main__':
292 test_main()