Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/return client errors #693

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions spyne/protocol/soap/soap11.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,23 @@ def _from_soap(in_envelope_xml, xmlids=None, **kwargs):

def _parse_xml_string(xml_string, parser, charset=None):
xml_string = iter(xml_string)
chunk = next(xml_string)
try:
chunk = next(xml_string)
except StopIteration:
logger_invalid.error("missing body")
raise Fault('Client.XMLSyntaxError', 'Missing body')

if isinstance(chunk, six.binary_type):
string = b''.join(chain( (chunk,), xml_string ))
else:
string = ''.join(chain( (chunk,), xml_string ))

if charset:
string = string.decode(charset)
try:
string = string.decode(charset)
except UnicodeDecodeError as e:
logger_invalid.error("%r in string %r", e, string)
raise Fault('Client.XMLSyntaxError', str(e))

try:
try:
Expand Down
35 changes: 35 additions & 0 deletions spyne/test/protocol/test_soap11.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,41 @@ def test_href(self):
# quick and dirty test href reconstruction
self.assertEqual(len(payload[0]), 2)

def test_empty_body(self):
# If the soap request has no body, then you get an empty iterable
# as the envelope string.
# This should be treated as a client error.
envelope_string = iter([])

with self.assertRaises(Fault) as cm:
_parse_xml_string(envelope_string,
etree.XMLParser(), 'utf8')
self.assertEqual(cm.exception.faultcode, 'Client.XMLSyntaxError')
self.assertEqual(cm.exception.faultstring, 'Missing body')

def test_bad_encoding(self):
# Encode string with non-ascii characters as Latin-1, so that later
# when decoding as utf-8 you will get an decode error.
# This should result in a client error.
envelope_string = ['''
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tns="http://tempuri.org/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:myResponse xsi:type="tns:myResponse">
<myResult href="bad-encoding-é#id1" />
</tns:myResponse>
</soap:Body>
</soap:Envelope>
'''.encode('latin-1')]

with self.assertRaises(Fault) as cm:
_parse_xml_string(envelope_string,
etree.XMLParser(), 'utf8')
self.assertEqual(cm.exception.faultcode, 'Client.XMLSyntaxError')
self.assertIn("'utf-8' codec can't decode byte",
cm.exception.faultstring)

def test_namespaces(self):
m = ComplexModel.produce(
namespace="some_namespace",
Expand Down