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