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