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.
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
20 DOCUMENTATION "A simple Seller interface to test ILU"
28 def __setup__(self, maxprice):
29 self._maxprice = maxprice
31 def __main__(self, kos):
32 """Entry point upon arrival at a new KOS."""
34 # B4 == Barry's Big Bass Business :-)
35 seller = broker.lookup('Seller_1.Seller', 'B4')
37 price = seller.price()
38 print 'Seller wants $', price, '... '
39 if price > self._maxprice:
44 print 'no seller found here'
45 ''' # Don't ask why this comment is here
48 # instantiate a buyer instance and put it in a magic place for the KOS
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"),
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"),
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"),
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
88 Content-Type: multipart/knowbot;
89 boundary="801spam999";
92 This is a multi-part message in MIME format.
95 Content-Type: multipart/knowbot-metadata;
100 Content-Type: message/rfc822
101 KP-Metadata-Type: simple
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
113 Content-Type: text/isl
114 KP-Metadata-Type: complex
115 KP-Metadata-Key: connection
117 KP-Connection-Description: Barry's Big Bass Business
119 KP-Connection-Direction: client
124 DOCUMENTATION "A simple Seller interface to test ILU"
130 Content-Type: message/external-body;
132 URL="hdl://cnri.kss/generic-knowbot"
134 Content-Type: text/isl
135 KP-Metadata-Type: complex
136 KP-Metadata-Key: generic-interface
138 KP-Connection-Description: Generic Interface for All Knowbots
139 KP-Connection-Id: generic-kp
140 KP-Connection-Direction: client
146 Content-Type: multipart/knowbot-code;
147 boundary="803spam999"
151 Content-Type: text/plain
152 KP-Module-Name: BuyerKP
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')
164 price = seller.price()
165 print 'Seller wants $', price, '... '
166 if price > self._maxprice:
169 print "I'll take it!"
171 print 'no seller found here'
176 Content-Type: multipart/knowbot-state;
177 boundary="804spam999"
182 Content-Type: text/plain
185 # instantiate a buyer instance and put it in a magic place for the KOS
188 __kp__.__setup__(500)
195 class MimewriterTest(unittest
.TestCase
):
198 buf
= StringIO
.StringIO()
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")
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
)
233 for key
, value
in COMPLEX_METADATA
:
234 md2
.addheader("KP-" + key
, value
)
235 f
= md2
.startbody("text/isl")
242 f
= md3
.startbody("message/external-body",
243 [("access-type", "URL"),
244 ("URL", "hdl://cnri.kss/generic-knowbot")])
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
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")
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")
282 # End toplevel body parts
286 self
.assertEqual(buf
.getvalue(), OUTPUT
)
289 run_unittest(MimewriterTest
)
291 if __name__
== '__main__':