Skip to content

Topic/support bodies with unicode#3

Merged
jerry57 merged 3 commits into
jerry57:masterfrom
obruns:topic/support-bodies-with-unicode
Dec 12, 2019
Merged

Topic/support bodies with unicode#3
jerry57 merged 3 commits into
jerry57:masterfrom
obruns:topic/support-bodies-with-unicode

Conversation

@jerry57
Copy link
Copy Markdown
Owner

@jerry57 jerry57 commented Dec 12, 2019

No description provided.

Keep the symmetry with set_request_body() which encodes the unicode
object (Robot Framework defaults to unicode for all strings) into an
instance of str by decoding the response into an instance of unicode in
get_response_body().

Failure to do so causes UnicodeDecodeErrors in all keywords that compare
the response body against a given string if any of the two contains
Unicode characters.
Robot Framework defaults to unicode objects for all strings created. As
a result, httplib.py:848 has unicode += str (msg += message_body) which
crashes with UnicodeDecodeError when the body contains Unicode
characters:

  File "/usr/lib64/python2.7/httplib.py", line 1001, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python2.7/httplib.py", line 1035, in _send_request
    self.endheaders(body)
  File "/usr/lib64/python2.7/httplib.py", line 997, in endheaders
    self._send_output(message_body)
  File "/usr/lib64/python2.7/httplib.py", line 848, in _send_output
    msg += message_body
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 32: ordinal not in range(128)

A similar stack trace has been posted as part of Python bug #11898 [1]
(duplicated by #12398 [2]) which has later been closed as a programming
error.

Both robotframework-httplibrary and webtest treat the 'Content-Type'
header special which results in surprising behavior regarding the tests:
Had the 'Accept' header been removed from the provided test case no
UnicodeDecodeError would have been thrown in httplib.py:848.

Turning unicode objects into str objects is the right thing to do
according to the HTTP standard ([3], updated by [5]) because it requires
conformity to MIME [5] (hint by [6]).

[1] http://bugs.python.org/issue11898
[2] http://bugs.python.org/issue12398
[3] https://tools.ietf.org/html/rfc2616
[4] https://tools.ietf.org/html/rfc2047
[5] https://tools.ietf.org/html/rfc7230
[6] https://stackoverflow.com/a/5426648
@jerry57 jerry57 merged commit 3a402e1 into jerry57:master Dec 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants