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