Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit 6be7831

Browse files
committed
Merge pull request #201 from irachex/py27-compat
For python2.7 compatibility
2 parents b428195 + d964df0 commit 6be7831

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

hyper/http20/connection.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ..common.exceptions import ConnectionResetError
1010
from ..common.bufsocket import BufferedSocket
1111
from ..common.headers import HTTPHeaderMap
12-
from ..common.util import to_host_port_tuple, to_native_string
12+
from ..common.util import to_host_port_tuple, to_native_string, to_bytestring
1313
from ..packages.hyperframe.frame import (
1414
FRAMES, DataFrame, HeadersFrame, PushPromiseFrame, RstStreamFrame,
1515
SettingsFrame, Frame, WindowUpdateFrame, GoAwayFrame, PingFrame,
@@ -179,8 +179,8 @@ def request(self, method, url, body=None, headers={}):
179179
self.putheader(name, value, stream_id, replace=is_default)
180180

181181
# Convert the body to bytes if needed.
182-
if isinstance(body, str):
183-
body = body.encode('utf-8')
182+
if body:
183+
body = to_bytestring(body)
184184

185185
self.endheaders(message_body=body, final=True, stream_id=stream_id)
186186

test/test_hyper.py

+16
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,22 @@ def test_putrequest_sends_data(self):
221221
assert len(sock.queue) == 2
222222
assert c._out_flow_control_window == 65535 - len(b'hello')
223223

224+
def test_request_with_utf8_bytes_body(self):
225+
c = HTTP20Connection('www.google.com')
226+
c._sock = DummySocket()
227+
body = '你好' if is_py2 else '你好'.encode('utf-8')
228+
c.request('GET', '/', body=body)
229+
230+
assert c._out_flow_control_window == 65535 - len(body)
231+
232+
def test_request_with_unicode_body(self):
233+
c = HTTP20Connection('www.google.com')
234+
c._sock = DummySocket()
235+
body = '你好'.decode('unicode-escape') if is_py2 else '你好'
236+
c.request('GET', '/', body=body)
237+
238+
assert c._out_flow_control_window == 65535 - len(body.encode('utf-8'))
239+
224240
def test_different_request_headers(self):
225241
sock = DummySocket()
226242

0 commit comments

Comments
 (0)