File: test_wsdl_soap.py

package info (click to toggle)
python-zeep 4.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,600 kB
  • sloc: python: 15,551; makefile: 13
file content (419 lines) | stat: -rw-r--r-- 13,918 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import platform

import pytest
from lxml import etree
from pretend import stub

from tests.utils import load_xml
from zeep import Client
from zeep.exceptions import Fault, TransportError
from zeep.wsdl import bindings


def test_soap11_no_output():
    client = Client("tests/wsdl_files/soap.wsdl")
    content = """
        <soapenv:Envelope
            xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:stoc="http://example.com/stockquote.xsd">
          <soapenv:Body></soapenv:Body>
        </soapenv:Envelope>
    """.strip()
    response = stub(status_code=200, headers={}, content=content)

    operation = client.service._binding._operations["GetLastTradePriceNoOutput"]
    res = client.service._binding.process_reply(client, operation, response)
    assert res is None


def test_soap11_process_error():
    response = load_xml(
        """
        <soapenv:Envelope
            xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:stoc="http://example.com/stockquote.xsd">
          <soapenv:Body>
            <soapenv:Fault>
              <faultcode>fault-code</faultcode>
              <faultstring>fault-string</faultstring>
              <detail>
                <e:myFaultDetails xmlns:e="http://myexample.org/faults">
                  <e:message>detail-message</e:message>
                  <e:errorcode>detail-code</e:errorcode>
                </e:myFaultDetails>
              </detail>
            </soapenv:Fault>
          </soapenv:Body>
        </soapenv:Envelope>
    """
    )

    binding = bindings.Soap11Binding(
        wsdl=None, name=None, port_name=None, transport=None, default_style=None
    )
    try:
        binding.process_error(response, None)
        assert False
    except Fault as exc:
        assert exc.message == "fault-string"
        assert exc.code == "fault-code"
        assert exc.actor is None
        assert exc.subcodes is None
        assert "detail-message" in etree.tostring(exc.detail).decode("utf-8")

    responseWithNamespaceInFault = load_xml(
        """
        <soapenv:Envelope
            xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:stoc="http://example.com/stockquote.xsd">
          <soapenv:Body>
            <soapenv:Fault xmlns="http://schemas.xmlsoap.org/soap/envelope/">
              <faultcode>fault-code-withNamespace</faultcode>
              <faultstring>fault-string-withNamespace</faultstring>
              <detail>
                <e:myFaultDetails xmlns:e="http://myexample.org/faults">
                  <e:message>detail-message-withNamespace</e:message>
                  <e:errorcode>detail-code-withNamespace</e:errorcode>
                </e:myFaultDetails>
              </detail>
            </soapenv:Fault>
          </soapenv:Body>
        </soapenv:Envelope>
    """
    )

    try:
        binding.process_error(responseWithNamespaceInFault, None)
        assert False
    except Fault as exc:
        assert exc.message == "fault-string-withNamespace"
        assert exc.code == "fault-code-withNamespace"
        assert exc.actor is None
        assert exc.subcodes is None
        assert "detail-message-withNamespace" in etree.tostring(exc.detail).decode(
            "utf-8"
        )


def test_soap12_process_error():
    response = """
        <soapenv:Envelope
            xmlns="http://example.com/example1"
            xmlns:ex="http://example.com/example2"
            xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
          <soapenv:Body>
            <soapenv:Fault>
             <soapenv:Code>
               <soapenv:Value>fault-code</soapenv:Value>
               %s
             </soapenv:Code>
             <soapenv:Reason>
              <soapenv:Text xml:lang="en-US">us-error</soapenv:Text>
              <soapenv:Text xml:lang="nl-NL">nl-error</soapenv:Text>
             </soapenv:Reason>
             <soapenv:Detail>
              <e:myFaultDetails
                xmlns:e="http://myexample.org/faults" >
                <e:message>Invalid credit card details</e:message>
                <e:errorcode>999</e:errorcode>
              </e:myFaultDetails>
             </soapenv:Detail>
           </soapenv:Fault>
         </soapenv:Body>
        </soapenv:Envelope>
    """
    subcode = """
               <soapenv:Subcode>
                 <soapenv:Value>%s</soapenv:Value>
                 %s
               </soapenv:Subcode>
    """
    binding = bindings.Soap12Binding(
        wsdl=None, name=None, port_name=None, transport=None, default_style=None
    )

    try:
        binding.process_error(load_xml(response % ""), None)
        assert False
    except Fault as exc:
        assert exc.message == "us-error"
        assert exc.code == "fault-code"
        assert exc.subcodes == []

    try:
        binding.process_error(
            load_xml(response % subcode % ("fault-subcode1", "")), None
        )
        assert False
    except Fault as exc:
        assert exc.message == "us-error"
        assert exc.code == "fault-code"
        assert len(exc.subcodes) == 1
        assert exc.subcodes[0].namespace == "http://example.com/example1"
        assert exc.subcodes[0].localname == "fault-subcode1"

    try:
        binding.process_error(
            load_xml(
                response
                % subcode
                % ("fault-subcode1", subcode % ("ex:fault-subcode2", ""))
            ),
            None,
        )
        assert False
    except Fault as exc:
        assert exc.message == "us-error"
        assert exc.code == "fault-code"
        assert len(exc.subcodes) == 2
        assert exc.subcodes[0].namespace == "http://example.com/example1"
        assert exc.subcodes[0].localname == "fault-subcode1"
        assert exc.subcodes[1].namespace == "http://example.com/example2"
        assert exc.subcodes[1].localname == "fault-subcode2"


def test_no_content_type():
    data = """
        <?xml version="1.0"?>
        <soapenv:Envelope
            xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:stoc="http://example.com/stockquote.xsd">
           <soapenv:Header/>
           <soapenv:Body>
              <stoc:TradePrice>
                 <price>120.123</price>
              </stoc:TradePrice>
           </soapenv:Body>
        </soapenv:Envelope>
    """.strip()

    client = Client("tests/wsdl_files/soap.wsdl")
    binding = client.service._binding

    response = stub(status_code=200, content=data, encoding="utf-8", headers={})

    result = binding.process_reply(client, binding.get("GetLastTradePrice"), response)

    assert result == 120.123


@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="Fails on PyPy")
def test_wrong_content():
    data = """
        The request is answered something unexpected,
        like an html page or a raw internal stack trace
    """.strip()

    client = Client("tests/wsdl_files/soap.wsdl")
    binding = client.service._binding

    response = stub(status_code=200, content=data, encoding="utf-8", headers={})

    with pytest.raises(TransportError) as exc:
        binding.process_reply(client, binding.get("GetLastTradePrice"), response)
    assert 200 == exc.value.status_code
    assert data == exc.value.content


@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="Fails on PyPy")
def test_wrong_no_unicode_content():
    data = """
        The request is answered something unexpected,
        and the content charset is beyond unicode òñÇÿ
    """.strip()

    client = Client("tests/wsdl_files/soap.wsdl")
    binding = client.service._binding

    response = stub(status_code=200, content=data, encoding="utf-8", headers={})

    with pytest.raises(TransportError) as exc:
        binding.process_reply(client, binding.get("GetLastTradePrice"), response)

    assert 200 == exc.value.status_code
    assert data == exc.value.content


@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="Fails on PyPy")
def test_http_error():
    data = """
        Unauthorized!
    """.strip()

    client = Client("tests/wsdl_files/soap.wsdl")
    binding = client.service._binding

    response = stub(status_code=401, content=data, encoding="utf-8", headers={})

    with pytest.raises(TransportError) as exc:
        binding.process_reply(client, binding.get("GetLastTradePrice"), response)
    assert 401 == exc.value.status_code
    assert data == exc.value.content


def test_mime_multipart():
    data = "\r\n".join(
        line.strip()
        for line in """
        --MIME_boundary
        Content-Type: text/xml; charset=UTF-8
        Content-Transfer-Encoding: 8bit
        Content-ID: <claim061400a.xml@claiming-it.com>

        <?xml version='1.0' ?>
        <SOAP-ENV:Envelope
        xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <SOAP-ENV:Body>
        <claim:insurance_claim_auto id="insurance_claim_document_id"
        xmlns:claim="http://schemas.risky-stuff.com/Auto-Claim">
        <theSignedForm href="cid:claim061400a.tiff@claiming-it.com"/>
        <theCrashPhoto href="cid:claim061400a.jpeg@claiming-it.com"/>
        <!-- ... more claim details go here... -->
        </claim:insurance_claim_auto>
        </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>

        --MIME_boundary
        Content-Type: image/tiff
        Content-Transfer-Encoding: base64
        Content-ID: <claim061400a.tiff@claiming-it.com>

        Li4uQmFzZTY0IGVuY29kZWQgVElGRiBpbWFnZS4uLg==

        --MIME_boundary
        Content-Type: image/jpeg
        Content-Transfer-Encoding: binary
        Content-ID: <claim061400a.jpeg@claiming-it.com>

        ...Raw JPEG image..
        --MIME_boundary--
    """.splitlines()
    ).encode("utf-8")

    client = Client("tests/wsdl_files/claim.wsdl")
    binding = client.service._binding

    response = stub(
        status_code=200,
        content=data,
        encoding="utf-8",
        headers={
            "Content-Type": 'multipart/related; type="text/xml"; start="<claim061400a.xml@claiming-it.com>"; boundary="MIME_boundary"'
        },
    )

    result = binding.process_reply(client, binding.get("GetClaimDetails"), response)

    assert result.root is None
    assert len(result.attachments) == 2

    assert result.attachments[0].content == b"...Base64 encoded TIFF image..."
    assert result.attachments[1].content == b"...Raw JPEG image.."


def test_mime_multipart_no_encoding():
    data = "\r\n".join(
        line.strip()
        for line in """
        --MIME_boundary
        Content-Type: text/xml
        Content-Transfer-Encoding: 8bit
        Content-ID: <claim061400a.xml@claiming-it.com>

        <?xml version='1.0' ?>
        <SOAP-ENV:Envelope
        xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <SOAP-ENV:Body>
        <claim:insurance_claim_auto id="insurance_claim_document_id"
        xmlns:claim="http://schemas.risky-stuff.com/Auto-Claim">
        <theSignedForm href="cid:claim061400a.tiff@claiming-it.com"/>
        <theCrashPhoto href="cid:claim061400a.jpeg@claiming-it.com"/>
        <!-- ... more claim details go here... -->
        </claim:insurance_claim_auto>
        </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>

        --MIME_boundary
        Content-Type: image/tiff
        Content-Transfer-Encoding: base64
        Content-ID: <claim061400a.tiff@claiming-it.com>

        Li4uQmFzZTY0IGVuY29kZWQgVElGRiBpbWFnZS4uLg==

        --MIME_boundary
        Content-Type: text/xml
        Content-ID: <claim061400a.jpeg@claiming-it.com>

        ...Raw JPEG image..
        --MIME_boundary--
    """.splitlines()
    ).encode("utf-8")

    client = Client("tests/wsdl_files/claim.wsdl")
    binding = client.service._binding

    response = stub(
        status_code=200,
        content=data,
        encoding=None,
        headers={
            "Content-Type": 'multipart/related; type="text/xml"; start="<claim061400a.xml@claiming-it.com>"; boundary="MIME_boundary"'
        },
    )

    result = binding.process_reply(client, binding.get("GetClaimDetails"), response)

    assert result.root is None
    assert len(result.attachments) == 2

    assert result.attachments[0].content == b"...Base64 encoded TIFF image..."
    assert result.attachments[1].content == b"...Raw JPEG image.."


def test_unexpected_headers():
    data = """
        <?xml version="1.0"?>
        <soapenv:Envelope
            xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:stoc="http://example.com/stockquote.xsd">
           <soapenv:Header>
             <stoc:IamUnexpected>uhoh</stoc:IamUnexpected>
           </soapenv:Header>
           <soapenv:Body>
              <stoc:TradePrice>
                 <price>120.123</price>
              </stoc:TradePrice>
           </soapenv:Body>
        </soapenv:Envelope>
    """.strip()

    client = Client("tests/wsdl_files/soap_header.wsdl")
    binding = client.service._binding

    response = stub(status_code=200, content=data, encoding="utf-8", headers={})

    result = binding.process_reply(client, binding.get("GetLastTradePrice"), response)

    assert result.body.price == 120.123
    assert result.header.body is None
    assert len(result.header._raw_elements) == 1


def test_response_201():
    client = Client("tests/wsdl_files/soap_header.wsdl")
    binding = client.service._binding

    response = stub(status_code=201, content="", encoding="utf-8", headers={})

    result = binding.process_reply(client, binding.get("GetLastTradePrice"), response)
    assert result is None


def test_response_202():
    client = Client("tests/wsdl_files/soap_header.wsdl")
    binding = client.service._binding

    response = stub(status_code=202, content="", encoding="utf-8", headers={})

    result = binding.process_reply(client, binding.get("GetLastTradePrice"), response)
    assert result is None