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