1 #!/usr/bin/env python 2 # -*- coding: iso-8859-15 -*- 3 4 import libxml2dom.soap 5 from libxml2dom.rpc import ParameterName, ParameterValue 6 7 request = """<?xml version='1.0' encoding='iso-8859-1'?> 8 <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" > 9 <env:Header> 10 <t:transaction 11 xmlns:t="http://thirdparty.example.org/transaction" 12 env:encodingStyle="http://example.com/encoding" 13 env:mustUnderstand="true" >5</t:transaction> 14 </env:Header> 15 <env:Body> 16 <m:chargeReservation 17 env:encodingStyle="http://www.w3.org/2003/05/soap-encoding" 18 xmlns:m="http://travelcompany.example.org/"> 19 <m:reservation xmlns:m="http://travelcompany.example.org/reservation"> 20 <m:code>FT35ZBQ</m:code> 21 </m:reservation> 22 <o:creditCard xmlns:o="http://mycompany.example.com/financial"> 23 <n:name xmlns:n="http://mycompany.example.com/employees"> 24 ?ke J?gvan ?yvind 25 </n:name> 26 <o:number>123456789099999</o:number> 27 <o:expiration>2005-02</o:expiration> 28 </o:creditCard> 29 </m:chargeReservation> 30 </env:Body> 31 </env:Envelope>""" 32 33 req = libxml2dom.soap.parseString(request) 34 assert req.method.methodName == "chargeReservation" 35 assert req.method.parameterValues == [ 36 ParameterValue(ParameterName("http://travelcompany.example.org/reservation", "reservation"), [ 37 ParameterValue(ParameterName("http://travelcompany.example.org/reservation", "code"), "FT35ZBQ") 38 ]), 39 ParameterValue(ParameterName("http://mycompany.example.com/financial", "creditCard"), [ 40 ParameterValue(ParameterName("http://mycompany.example.com/employees", "name"), u"?ke J?gvan ?yvind"), 41 ParameterValue(ParameterName("http://mycompany.example.com/financial", "number"), "123456789099999"), 42 ParameterValue(ParameterName("http://mycompany.example.com/financial", "expiration"), "2005-02") 43 ]) 44 ] 45 assert req.fault is None 46 print "Method name:", req.method.methodName 47 print "Parameter values:", req.method.parameterValues 48 print "Fault:", req.fault 49 50 response = """<?xml version='1.0' encoding='iso-8859-1'?> 51 <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" > 52 <env:Header> 53 <t:transaction 54 xmlns:t="http://thirdparty.example.org/transaction" 55 env:encodingStyle="http://example.com/encoding" 56 env:mustUnderstand="true">5</t:transaction> 57 </env:Header> 58 <env:Body> 59 <m:chargeReservationResponse 60 env:encodingStyle="http://www.w3.org/2003/05/soap-encoding" 61 xmlns:m="http://travelcompany.example.org/"> 62 <m:code>FT35ZBQ</m:code> 63 <m:viewAt> 64 http://travelcompany.example.org/reservations?code=FT35ZBQ 65 </m:viewAt> 66 </m:chargeReservationResponse> 67 </env:Body> 68 </env:Envelope>""" 69 70 resp = libxml2dom.soap.parseString(response) 71 assert resp.method.methodName == "chargeReservationResponse" 72 assert resp.method.parameterValues == [ 73 ParameterValue(ParameterName("http://travelcompany.example.org/", "code"), "FT35ZBQ"), 74 ParameterValue(ParameterName("http://travelcompany.example.org/", "viewAt"), 75 "http://travelcompany.example.org/reservations?code=FT35ZBQ") 76 ] 77 assert resp.fault is None 78 print "Method name:", resp.method.methodName 79 print "Parameter values:", resp.method.parameterValues 80 print "Fault:", resp.fault 81 82 response2 = """<?xml version='1.0' encoding='iso-8859-1'?> 83 <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" > 84 <env:Header> 85 <t:transaction 86 xmlns:t="http://thirdparty.example.org/transaction" 87 env:encodingStyle="http://example.com/encoding" 88 env:mustUnderstand="true">5</t:transaction> 89 </env:Header> 90 <env:Body> 91 <m:chargeReservationResponse 92 env:encodingStyle="http://www.w3.org/2003/05/soap-encoding" 93 xmlns:rpc="http://www.w3.org/2003/05/soap-rpc" 94 xmlns:m="http://travelcompany.example.org/"> 95 <rpc:result>m:status</rpc:result> 96 <m:status>confirmed</m:status> 97 <m:code>FT35ZBQ</m:code> 98 <m:viewAt> 99 http://travelcompany.example.org/reservations?code=FT35ZBQ 100 </m:viewAt> 101 </m:chargeReservationResponse> 102 </env:Body> 103 </env:Envelope>""" 104 105 resp2 = libxml2dom.soap.parseString(response2) 106 assert resp2.method.methodName == "chargeReservationResponse" 107 assert resp2.method.parameterValues == [ 108 ParameterValue(ParameterName("http://www.w3.org/2003/05/soap-rpc", "result"), "m:status"), 109 ParameterValue(ParameterName("http://travelcompany.example.org/", "status"), "confirmed"), 110 ParameterValue(ParameterName("http://travelcompany.example.org/", "code"), "FT35ZBQ"), 111 ParameterValue(ParameterName("http://travelcompany.example.org/", "viewAt"), 112 "http://travelcompany.example.org/reservations?code=FT35ZBQ") 113 ] 114 assert resp2.fault is None 115 print "Method name:", resp2.method.methodName 116 print "Parameter values:", resp2.method.parameterValues 117 print "Fault:", resp2.fault 118 119 failed = """<?xml version='1.0' encoding='iso-8859-1'?> 120 <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" 121 xmlns:rpc='http://www.w3.org/2003/05/soap-rpc'> 122 <env:Body> 123 <env:Fault> 124 <env:Code> 125 <env:Value>env:Sender</env:Value> 126 <env:Subcode> 127 <env:Value>rpc:BadArguments</env:Value> 128 </env:Subcode> 129 </env:Code> 130 <env:Reason> 131 <env:Text xml:lang="en-US">Processing error</env:Text> 132 <env:Text xml:lang="cs">Chyba zpracov?n?</env:Text> 133 </env:Reason> 134 <env:Detail> 135 <e:myFaultDetails 136 xmlns:e="http://travelcompany.example.org/faults"> 137 <e:message>Name does not match card number</e:message> 138 <e:errorcode>999</e:errorcode> 139 </e:myFaultDetails> 140 </env:Detail> 141 </env:Fault> 142 </env:Body> 143 </env:Envelope>""" 144 145 f = libxml2dom.soap.parseString(failed) 146 assert f.method is None 147 assert f.fault.code == "env:Sender" 148 assert f.fault.subcode == "rpc:BadArguments" 149 print "Fault code:", f.fault.code 150 151 # vim: tabstop=4 expandtab shiftwidth=4