mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
encode fix test
This commit is contained in:
parent
55397be78a
commit
09e0d4c7fc
@ -7,6 +7,7 @@ from __future__ import print_function
|
|||||||
|
|
||||||
import email.utils
|
import email.utils
|
||||||
import fcntl
|
import fcntl
|
||||||
|
import io
|
||||||
import os
|
import os
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
import random
|
import random
|
||||||
@ -506,6 +507,19 @@ def to_bytestring(value, encoding="utf8"):
|
|||||||
|
|
||||||
return value.encode(encoding)
|
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):
|
def warn(msg):
|
||||||
print("!!!", file=sys.stderr)
|
print("!!!", file=sys.stderr)
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,7 @@ from gunicorn import util
|
|||||||
from gunicorn.http.body import Body
|
from gunicorn.http.body import Body
|
||||||
from gunicorn.http.wsgi import Response
|
from gunicorn.http.wsgi import Response
|
||||||
from gunicorn.six import BytesIO
|
from gunicorn.six import BytesIO
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import unittest.mock as mock
|
import unittest.mock as mock
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@ -68,20 +69,28 @@ def test_readline_buffer_loaded_with_size():
|
|||||||
|
|
||||||
|
|
||||||
def test_http_header_encoding():
|
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 = mock.MagicMock()
|
||||||
mocked_socket.sendall = mock.MagicMock()
|
mocked_socket.sendall = mock.MagicMock()
|
||||||
|
|
||||||
mocked_request = mock.MagicMock()
|
mocked_request = mock.MagicMock()
|
||||||
response = Response(mocked_request, mocked_socket, None)
|
response = Response(mocked_request, mocked_socket, None)
|
||||||
|
|
||||||
# set umlaut header
|
# set umlaut header
|
||||||
response.headers.append(('foo', 'häder'))
|
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
|
# build our own header_str to compare against
|
||||||
tosend = response.default_headers()
|
tosend = response.default_headers()
|
||||||
tosend.extend(["%s: %s\r\n" % (k, v) for k, v in response.headers])
|
tosend.extend(["%s: %s\r\n" % (k, v) for k, v in response.headers])
|
||||||
header_str = "%s\r\n" % "".join(tosend)
|
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)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user