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