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

Commit

Permalink
Merge pull request #201 from irachex/py27-compat
Browse files Browse the repository at this point in the history
For python2.7 compatibility
  • Loading branch information
Lukasa committed Feb 20, 2016
2 parents b428195 + d964df0 commit 6be7831
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
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
16 changes: 16 additions & 0 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

0 comments on commit 6be7831

Please sign in to comment.