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