Skip to content

Commit

Permalink
Fix handling of HTTP body in .xmlrpc.testing. (#12)
Browse files Browse the repository at this point in the history
This enables to use characters beyond ASCII.
  • Loading branch information
Michael Howitz committed Jun 8, 2020
1 parent 880b1b6 commit 5818bde
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Expand Up @@ -11,6 +11,10 @@
4.3.0 (2020-05-12)
==================

- Fix handling of HTTP body in ``.xmlrpc.testing`` to enable characters
beyond ASCII.
(`#11 <https://github.com/zopefoundation/zope.app.publisher/issues/11>`_)

- Support options *use_datetime* (Python >= 2.7) and *use_builtin_types*
(`Python >= 3.5`) in ``.xmlrpc.testing.ServerProxy``.

Expand Down
18 changes: 14 additions & 4 deletions src/zope/app/publisher/xmlrpc/README.rst
Expand Up @@ -244,7 +244,7 @@ If you need to raise an error, the prefered way to do it is via an
... self.request = request
...
... def your_fault(self):
... return xmlrpclib.Fault(42, "It's your fault!")
... return xmlrpclib.Fault(42, u"It's your fault \N{SNOWMAN}!")

Now we'll register it as a view:

Expand All @@ -269,10 +269,20 @@ Now we'll register it as a view:

Now, when we call it, we get a proper XML-RPC fault:

>>> try:
... from xmlrpc.client import Fault
... expected = '<Fault 42: "It\'s your fault \N{SNOWMAN}!">'
... except ImportError: # PY2
... from xmlrpclib import Fault
... expected = '<Fault 42: u"It\'s your fault \u2603!">'
>>> proxy = ServerProxy(wsgi_app, "http://mgr:mgrpw@localhost/")
>>> proxy.your_fault()
Traceback (most recent call last):
xmlrpc.client.Fault: <Fault 42: "It's your fault!">
>>> # When dropping PY2 we can come back here to asserting the text of the
>>> # exception:
>>> try:
... proxy.your_fault()
... except Fault as e:
... str(e) == expected
True


DateTime values
Expand Down
12 changes: 6 additions & 6 deletions src/zope/app/publisher/xmlrpc/testing.py
Expand Up @@ -21,8 +21,7 @@ def __init__(self, data):
def makefile(self, mode, bufsize=None):
assert 'b' in mode
data = self.data
if not isinstance(data, bytes):
data = data.encode('iso-8859-1')
assert isinstance(data, bytes)
return BytesIO(data)


Expand Down Expand Up @@ -76,10 +75,11 @@ def request(self, host, handler, request_body, verbose=0):
headers)

body = response.getBody()
if not isinstance(body, str):
# Python 3
body = body.decode("utf-8")
content = 'HTTP/1.0 ' + errmsg + '\n\n' + body
body = body if isinstance(body, bytes) else body.encode('latin-1')
errmsg = (errmsg
if isinstance(errmsg, bytes)
else errmsg.encode('ascii')) # HTTP response lines are ASCII
content = b'HTTP/1.0 ' + errmsg + b'\n\n' + body

res = httplib.HTTPResponse(FakeSocket(content))
res.begin()
Expand Down

0 comments on commit 5818bde

Please sign in to comment.