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