PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/edk2/AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_MimeWriter.py

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