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

Commit 3172111

Browse files
committed
Add test for sending file-like objects.
1 parent 30007a0 commit 3172111

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

hyper/http20/connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from ..common.bufsocket import BufferedSocket
1515
from ..common.headers import HTTPHeaderMap
1616
from ..common.util import to_host_port_tuple, to_native_string, to_bytestring
17+
from ..compat import unicode, bytes
1718
from .stream import Stream
1819
from .response import HTTP20Response, HTTP20Push
1920
from .window import FlowControlManager
@@ -164,7 +165,7 @@ def request(self, method, url, body=None, headers={}):
164165
self.putheader(name, value, stream_id, replace=is_default)
165166

166167
# Convert the body to bytes if needed.
167-
if body:
168+
if body and isinstance(body, (unicode, bytes)):
168169
body = to_bytestring(body)
169170

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

test/test_hyper.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,28 @@ def consume_single_frame():
411411

412412
assert mutable['counter'] == 10
413413

414+
def test_sending_file(self, frame_buffer):
415+
# Prepare a socket so we can open a stream.
416+
sock = DummySocket()
417+
c = HTTP20Connection('www.google.com')
418+
c._sock = sock
419+
420+
# Send a request that involves uploading a file handle.
421+
with open(__file__, 'rb') as f:
422+
c.request('GET', '/', body=f)
423+
424+
# Get all the frames
425+
frame_buffer.add_data(b''.join(sock.queue))
426+
frames = list(frame_buffer)
427+
428+
# Reconstruct the file from the sent data.
429+
sent_data = b''.join(
430+
f.data for f in frames if isinstance(f, DataFrame)
431+
)
432+
433+
with open(__file__, 'rb') as f:
434+
assert f.read() == sent_data
435+
414436

415437
class TestServerPush(object):
416438
def setup_method(self, method):

0 commit comments

Comments
 (0)