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