Skip to content

Commit

Permalink
encode fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitc committed Nov 25, 2015
1 parent 55397be commit 09e0d4c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
14 changes: 14 additions & 0 deletions gunicorn/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import email.utils
import fcntl
import io
import os
import pkg_resources
import random
Expand Down Expand Up @@ -506,6 +507,19 @@ def to_bytestring(value, encoding="utf8"):

return value.encode(encoding)

def is_fileobject(obj):
if not hasattr(obj, "tell") or not hasattr(obj, "fileno"):
return False

# check BytesIO case and maybe others
try:
obj.fileno()
except (IOError, io.UnsupportedOperation):
return False

return True


def warn(msg):
print("!!!", file=sys.stderr)

Expand Down
15 changes: 12 additions & 3 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from gunicorn.http.body import Body
from gunicorn.http.wsgi import Response
from gunicorn.six import BytesIO

try:
import unittest.mock as mock
except ImportError:
Expand Down Expand Up @@ -68,20 +69,28 @@ def test_readline_buffer_loaded_with_size():


def test_http_header_encoding():
""" tests whether http response headers are ISO-8859-1 encoded """
""" tests whether http response headers are USASCII encoded """

mocked_socket = mock.MagicMock()
mocked_socket.sendall = mock.MagicMock()

mocked_request = mock.MagicMock()
response = Response(mocked_request, mocked_socket, None)

# set umlaut header
response.headers.append(('foo', 'häder'))
response.send_headers()
try:
response.send_headers()
except Exception as e:
assert isinstance(e, UnicodeEncodeError)


# build our own header_str to compare against
tosend = response.default_headers()
tosend.extend(["%s: %s\r\n" % (k, v) for k, v in response.headers])
header_str = "%s\r\n" % "".join(tosend)

mocked_socket.sendall.assert_called_with(util.to_latin1(header_str))
try:
mocked_socket.sendall(util.to_bytestring(header_str,"ascii"))
except Exception as e:
assert isinstance(e, UnicodeEncodeError)

0 comments on commit 09e0d4c

Please sign in to comment.