libxml2dom

tests/xmlrpc_test.py

335:ffaf027ed471
2008-08-28 Paul Boddie Improved error reporting, adding messages for non-validation errors.
     1 #!/usr/bin/env python     2      3 import libxml2dom.xmlrpc     4      5 # Some examples from the specification.     6      7 request = """<?xml version="1.0"?>     8 <methodCall>     9    <methodName>examples.getStateName</methodName>    10    <params>    11       <param>    12          <value><i4>41</i4></value>    13          </param>    14       </params>    15    </methodCall>"""    16     17 req = libxml2dom.xmlrpc.parseString(request)    18 assert req.method.methodName == "examples.getStateName"    19 assert req.method.parameterValues == [41]    20 assert req.fault is None    21 print "Method name:", req.method.methodName    22 print "Parameter values:", req.method.parameterValues    23 print "Fault:", req.fault    24     25 response = """<?xml version="1.0"?>    26 <methodResponse>    27    <params>    28       <param>    29          <value><string>South Dakota</string></value>    30          </param>    31       </params>    32    </methodResponse>"""    33     34 resp = libxml2dom.xmlrpc.parseString(response)    35 assert resp.method.methodName is None    36 assert resp.method.parameterValues == ["South Dakota"]    37 assert resp.fault is None    38 print "Method name:", resp.method.methodName    39 print "Parameter values:", resp.method.parameterValues    40 print "Fault:", resp.fault    41     42 failed = """<?xml version="1.0"?>    43 <methodResponse>    44    <fault>    45       <value>    46          <struct>    47             <member>    48                <name>faultCode</name>    49                <value><int>4</int></value>    50                </member>    51             <member>    52                <name>faultString</name>    53                <value><string>Too many parameters.</string></value>    54                </member>    55             </struct>    56          </value>    57       </fault>    58    </methodResponse>"""    59     60 f = libxml2dom.xmlrpc.parseString(failed)    61 assert f.method.methodName is None    62 assert f.method.parameterValues == []    63 assert f.fault.code == "4"    64 assert f.fault.reason == "Too many parameters."    65 print "Method name:", f.method.methodName    66 print "Parameter values:", f.method.parameterValues    67 print "Fault code:", f.fault.code    68     69 # Python Package Index examples.    70     71 search = """<?xml version="1.0"?>    72 <methodCall>    73   <methodName>search</methodName>    74   <params>    75     <param>    76       <value>    77         <struct>    78           <member>    79             <name>name</name>    80             <value><string>libxml2dom</string></value>    81           </member>    82           <member>    83             <name>description</name>    84             <value>XML</value>    85           </member>    86         </struct>    87       </value>    88     </param>    89     <param>    90       <value>    91         <string>and</string>    92       </value>    93     </param>    94   </params>    95 </methodCall>"""    96     97 s = libxml2dom.xmlrpc.parseString(search)    98 assert s.method.methodName == "search"    99 assert s.method.parameterValues == [   100     [   101         ("name", "libxml2dom"),   102         ("description", "XML")   103         ],   104     "and"   105     ]   106 assert s.fault is None   107 print "Method name:", s.method.methodName   108 print "Parameter values:", s.method.parameterValues   109 print "Fault:", s.fault   110    111 # Nested structure examples.   112    113 search2 = """<?xml version="1.0"?>   114 <methodCall>   115   <methodName>search</methodName>   116   <params>   117     <param>   118       <value>   119         <struct>   120           <member>   121             <name>names</name>   122             <value>   123               <struct>   124                 <member>   125                   <name>name</name>   126                   <value><string>libxml2dom</string></value>   127                 </member>   128                 <member>   129                   <name>description</name>   130                   <value>XML</value>   131                 </member>   132               </struct>   133             </value>   134           </member>   135         </struct>   136       </value>   137     </param>   138     <param>   139       <value>   140         <string>and</string>   141       </value>   142     </param>   143   </params>   144 </methodCall>"""   145    146 s2 = libxml2dom.xmlrpc.parseString(search2)   147 assert s2.method.methodName == "search"   148 assert s2.method.parameterValues == [   149     [   150         ("names", [   151             ("name", "libxml2dom"),   152             ("description", "XML")   153             ])   154         ],   155     "and"   156     ]   157 assert s2.fault is None   158 print "Method name:", s2.method.methodName   159 print "Parameter values:", s2.method.parameterValues   160 print "Fault:", s2.fault   161    162 arrays = """<?xml version="1.0"?>   163     <methodResponse>   164       <params>   165         <param>   166           <value>   167             <array>   168               <data>   169                 <value>   170                   <string>libxml2dom</string>   171                 </value>   172                 <value>   173                   <string>XSLTools</string>   174                 </value>   175               </data>   176             </array>   177           </value>   178         </param>   179       </params>   180     </methodResponse>"""   181    182 a = libxml2dom.xmlrpc.parseString(arrays)   183 assert a.method.methodName is None   184 assert a.method.parameterValues == [["libxml2dom", "XSLTools"]]   185 assert a.fault is None   186 print "Method name:", a.method.methodName   187 print "Parameter values:", a.method.parameterValues   188 print "Fault:", a.fault   189    190 # vim: tabstop=4 expandtab shiftwidth=4