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

For python2.7 compatibility #201

Merged
merged 1 commit into from
Feb 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions hyper/http20/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..common.exceptions import ConnectionResetError
from ..common.bufsocket import BufferedSocket
from ..common.headers import HTTPHeaderMap
from ..common.util import to_host_port_tuple, to_native_string
from ..common.util import to_host_port_tuple, to_native_string, to_bytestring
from ..packages.hyperframe.frame import (
FRAMES, DataFrame, HeadersFrame, PushPromiseFrame, RstStreamFrame,
SettingsFrame, Frame, WindowUpdateFrame, GoAwayFrame, PingFrame,
Expand Down Expand Up @@ -179,8 +179,8 @@ def request(self, method, url, body=None, headers={}):
self.putheader(name, value, stream_id, replace=is_default)

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

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

Expand Down
24 changes: 20 additions & 4 deletions test/test_hyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,22 @@ def test_putrequest_sends_data(self):
assert len(sock.queue) == 2
assert c._out_flow_control_window == 65535 - len(b'hello')

def test_request_with_utf8_bytes_body(self):
c = HTTP20Connection('www.google.com')
c._sock = DummySocket()
body = '你好' if is_py2 else '你好'.encode('utf-8')
c.request('GET', '/', body=body)

assert c._out_flow_control_window == 65535 - len(body)

def test_request_with_unicode_body(self):
c = HTTP20Connection('www.google.com')
c._sock = DummySocket()
body = '你好'.decode('unicode-escape') if is_py2 else '你好'
c.request('GET', '/', body=body)

assert c._out_flow_control_window == 65535 - len(body.encode('utf-8'))

def test_different_request_headers(self):
sock = DummySocket()

Expand Down Expand Up @@ -581,7 +597,7 @@ def test_recv_cb_n_times(self):

def consume_single_frame():
mutable['counter'] += 1

c._consume_single_frame = consume_single_frame
c._recv_cb()

Expand Down Expand Up @@ -779,7 +795,7 @@ def test_streams_can_replace_none_headers(self):
(b"name", b"value"),
(b"other_name", b"other_value")
]

def test_stream_opening_sends_headers(self):
def data_callback(frame):
assert isinstance(frame, HeadersFrame)
Expand Down Expand Up @@ -1548,7 +1564,7 @@ def test_connection_error_when_send_out_of_range_frame(self):
class NullEncoder(object):
@staticmethod
def encode(headers):

def to_str(v):
if is_py2:
return str(v)
Expand All @@ -1557,7 +1573,7 @@ def to_str(v):
v = str(v, 'utf-8')
return v

return '\n'.join("%s%s" % (to_str(name), to_str(val))
return '\n'.join("%s%s" % (to_str(name), to_str(val))
for name, val in headers)


Expand Down