/Lib/test/test_MimeWriter.py

http://unladen-swallow.googlecode.com/ · Python · 295 lines · 237 code · 39 blank · 19 comment · 8 complexity · 6d41a83be33eab13e753b9523fd3bcc6 MD5 · raw file

  1. """Test program for MimeWriter module.
  2. The test program was too big to comfortably fit in the MimeWriter
  3. class, so it's here in its own file.
  4. This should generate Barry's example, modulo some quotes and newlines.
  5. """
  6. import unittest, StringIO
  7. from test.test_support import run_unittest
  8. import warnings
  9. warnings.filterwarnings("ignore", "the MimeWriter module is deprecated.*",
  10. DeprecationWarning)
  11. from MimeWriter import MimeWriter
  12. SELLER = '''\
  13. INTERFACE Seller-1;
  14. TYPE Seller = OBJECT
  15. DOCUMENTATION "A simple Seller interface to test ILU"
  16. METHODS
  17. price():INTEGER,
  18. END;
  19. '''
  20. BUYER = '''\
  21. class Buyer:
  22. def __setup__(self, maxprice):
  23. self._maxprice = maxprice
  24. def __main__(self, kos):
  25. """Entry point upon arrival at a new KOS."""
  26. broker = kos.broker()
  27. # B4 == Barry's Big Bass Business :-)
  28. seller = broker.lookup('Seller_1.Seller', 'B4')
  29. if seller:
  30. price = seller.price()
  31. print 'Seller wants $', price, '... '
  32. if price > self._maxprice:
  33. print 'too much!'
  34. else:
  35. print "I'll take it!"
  36. else:
  37. print 'no seller found here'
  38. ''' # Don't ask why this comment is here
  39. STATE = '''\
  40. # instantiate a buyer instance and put it in a magic place for the KOS
  41. # to find.
  42. __kp__ = Buyer()
  43. __kp__.__setup__(500)
  44. '''
  45. SIMPLE_METADATA = [
  46. ("Interpreter", "python"),
  47. ("Interpreter-Version", "1.3"),
  48. ("Owner-Name", "Barry Warsaw"),
  49. ("Owner-Rendezvous", "bwarsaw@cnri.reston.va.us"),
  50. ("Home-KSS", "kss.cnri.reston.va.us"),
  51. ("Identifier", "hdl://cnri.kss/my_first_knowbot"),
  52. ("Launch-Date", "Mon Feb 12 16:39:03 EST 1996"),
  53. ]
  54. COMPLEX_METADATA = [
  55. ("Metadata-Type", "complex"),
  56. ("Metadata-Key", "connection"),
  57. ("Access", "read-only"),
  58. ("Connection-Description", "Barry's Big Bass Business"),
  59. ("Connection-Id", "B4"),
  60. ("Connection-Direction", "client"),
  61. ]
  62. EXTERNAL_METADATA = [
  63. ("Metadata-Type", "complex"),
  64. ("Metadata-Key", "generic-interface"),
  65. ("Access", "read-only"),
  66. ("Connection-Description", "Generic Interface for All Knowbots"),
  67. ("Connection-Id", "generic-kp"),
  68. ("Connection-Direction", "client"),
  69. ]
  70. OUTPUT = '''\
  71. From: bwarsaw@cnri.reston.va.us
  72. Date: Mon Feb 12 17:21:48 EST 1996
  73. To: kss-submit@cnri.reston.va.us
  74. MIME-Version: 1.0
  75. Content-Type: multipart/knowbot;
  76. boundary="801spam999";
  77. version="0.1"
  78. This is a multi-part message in MIME format.
  79. --801spam999
  80. Content-Type: multipart/knowbot-metadata;
  81. boundary="802spam999"
  82. --802spam999
  83. Content-Type: message/rfc822
  84. KP-Metadata-Type: simple
  85. KP-Access: read-only
  86. KPMD-Interpreter: python
  87. KPMD-Interpreter-Version: 1.3
  88. KPMD-Owner-Name: Barry Warsaw
  89. KPMD-Owner-Rendezvous: bwarsaw@cnri.reston.va.us
  90. KPMD-Home-KSS: kss.cnri.reston.va.us
  91. KPMD-Identifier: hdl://cnri.kss/my_first_knowbot
  92. KPMD-Launch-Date: Mon Feb 12 16:39:03 EST 1996
  93. --802spam999
  94. Content-Type: text/isl
  95. KP-Metadata-Type: complex
  96. KP-Metadata-Key: connection
  97. KP-Access: read-only
  98. KP-Connection-Description: Barry's Big Bass Business
  99. KP-Connection-Id: B4
  100. KP-Connection-Direction: client
  101. INTERFACE Seller-1;
  102. TYPE Seller = OBJECT
  103. DOCUMENTATION "A simple Seller interface to test ILU"
  104. METHODS
  105. price():INTEGER,
  106. END;
  107. --802spam999
  108. Content-Type: message/external-body;
  109. access-type="URL";
  110. URL="hdl://cnri.kss/generic-knowbot"
  111. Content-Type: text/isl
  112. KP-Metadata-Type: complex
  113. KP-Metadata-Key: generic-interface
  114. KP-Access: read-only
  115. KP-Connection-Description: Generic Interface for All Knowbots
  116. KP-Connection-Id: generic-kp
  117. KP-Connection-Direction: client
  118. --802spam999--
  119. --801spam999
  120. Content-Type: multipart/knowbot-code;
  121. boundary="803spam999"
  122. --803spam999
  123. Content-Type: text/plain
  124. KP-Module-Name: BuyerKP
  125. class Buyer:
  126. def __setup__(self, maxprice):
  127. self._maxprice = maxprice
  128. def __main__(self, kos):
  129. """Entry point upon arrival at a new KOS."""
  130. broker = kos.broker()
  131. # B4 == Barry's Big Bass Business :-)
  132. seller = broker.lookup('Seller_1.Seller', 'B4')
  133. if seller:
  134. price = seller.price()
  135. print 'Seller wants $', price, '... '
  136. if price > self._maxprice:
  137. print 'too much!'
  138. else:
  139. print "I'll take it!"
  140. else:
  141. print 'no seller found here'
  142. --803spam999--
  143. --801spam999
  144. Content-Type: multipart/knowbot-state;
  145. boundary="804spam999"
  146. KP-Main-Module: main
  147. --804spam999
  148. Content-Type: text/plain
  149. KP-Module-Name: main
  150. # instantiate a buyer instance and put it in a magic place for the KOS
  151. # to find.
  152. __kp__ = Buyer()
  153. __kp__.__setup__(500)
  154. --804spam999--
  155. --801spam999--
  156. '''
  157. class MimewriterTest(unittest.TestCase):
  158. def test(self):
  159. buf = StringIO.StringIO()
  160. # Toplevel headers
  161. toplevel = MimeWriter(buf)
  162. toplevel.addheader("From", "bwarsaw@cnri.reston.va.us")
  163. toplevel.addheader("Date", "Mon Feb 12 17:21:48 EST 1996")
  164. toplevel.addheader("To", "kss-submit@cnri.reston.va.us")
  165. toplevel.addheader("MIME-Version", "1.0")
  166. # Toplevel body parts
  167. f = toplevel.startmultipartbody("knowbot", "801spam999",
  168. [("version", "0.1")], prefix=0)
  169. f.write("This is a multi-part message in MIME format.\n")
  170. # First toplevel body part: metadata
  171. md = toplevel.nextpart()
  172. md.startmultipartbody("knowbot-metadata", "802spam999")
  173. # Metadata part 1
  174. md1 = md.nextpart()
  175. md1.addheader("KP-Metadata-Type", "simple")
  176. md1.addheader("KP-Access", "read-only")
  177. m = MimeWriter(md1.startbody("message/rfc822"))
  178. for key, value in SIMPLE_METADATA:
  179. m.addheader("KPMD-" + key, value)
  180. m.flushheaders()
  181. del md1
  182. # Metadata part 2
  183. md2 = md.nextpart()
  184. for key, value in COMPLEX_METADATA:
  185. md2.addheader("KP-" + key, value)
  186. f = md2.startbody("text/isl")
  187. f.write(SELLER)
  188. del md2
  189. # Metadata part 3
  190. md3 = md.nextpart()
  191. f = md3.startbody("message/external-body",
  192. [("access-type", "URL"),
  193. ("URL", "hdl://cnri.kss/generic-knowbot")])
  194. m = MimeWriter(f)
  195. for key, value in EXTERNAL_METADATA:
  196. md3.addheader("KP-" + key, value)
  197. md3.startbody("text/isl")
  198. # Phantom body doesn't need to be written
  199. md.lastpart()
  200. # Second toplevel body part: code
  201. code = toplevel.nextpart()
  202. code.startmultipartbody("knowbot-code", "803spam999")
  203. # Code: buyer program source
  204. buyer = code.nextpart()
  205. buyer.addheader("KP-Module-Name", "BuyerKP")
  206. f = buyer.startbody("text/plain")
  207. f.write(BUYER)
  208. code.lastpart()
  209. # Third toplevel body part: state
  210. state = toplevel.nextpart()
  211. state.addheader("KP-Main-Module", "main")
  212. state.startmultipartbody("knowbot-state", "804spam999")
  213. # State: a bunch of assignments
  214. st = state.nextpart()
  215. st.addheader("KP-Module-Name", "main")
  216. f = st.startbody("text/plain")
  217. f.write(STATE)
  218. state.lastpart()
  219. # End toplevel body parts
  220. toplevel.lastpart()
  221. self.assertEqual(buf.getvalue(), OUTPUT)
  222. def test_main():
  223. run_unittest(MimewriterTest)
  224. if __name__ == '__main__':
  225. test_main()