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

Commit 209c7f0

Browse files
committed
For python2.7 compatibility
1 parent 89d4227 commit 209c7f0

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
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

+20-4
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

@@ -581,7 +597,7 @@ def test_recv_cb_n_times(self):
581597

582598
def consume_single_frame():
583599
mutable['counter'] += 1
584-
600+
585601
c._consume_single_frame = consume_single_frame
586602
c._recv_cb()
587603

@@ -779,7 +795,7 @@ def test_streams_can_replace_none_headers(self):
779795
(b"name", b"value"),
780796
(b"other_name", b"other_value")
781797
]
782-
798+
783799
def test_stream_opening_sends_headers(self):
784800
def data_callback(frame):
785801
assert isinstance(frame, HeadersFrame)
@@ -1548,7 +1564,7 @@ def test_connection_error_when_send_out_of_range_frame(self):
15481564
class NullEncoder(object):
15491565
@staticmethod
15501566
def encode(headers):
1551-
1567+
15521568
def to_str(v):
15531569
if is_py2:
15541570
return str(v)
@@ -1557,7 +1573,7 @@ def to_str(v):
15571573
v = str(v, 'utf-8')
15581574
return v
15591575

1560-
return '\n'.join("%s%s" % (to_str(name), to_str(val))
1576+
return '\n'.join("%s%s" % (to_str(name), to_str(val))
15611577
for name, val in headers)
15621578

15631579

0 commit comments

Comments
 (0)