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