Skip to content

Commit

Permalink
File upload fix (#1312)
Browse files Browse the repository at this point in the history
* Fix when ClientRequest get data as file with 'r+b' params
add test for this bug

* Add lines to CONTRIBUTORS.txt and CHANGES.rst
  • Loading branch information
demmsnt authored and asvetlov committed Oct 15, 2016
1 parent 52373a4 commit a476058
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ CHANGES

- Raise ValueError if BasicAuth login has a ":" character #1307

-
- Fix bug when ClientRequest send payload file with opened as
open('filename', 'r+b') #1306

-

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,4 @@ Yusuke Tsutsumi
Pau Freixes
Alexey Firsov
Vikas Kawadia
Dmitry Shamov
4 changes: 3 additions & 1 deletion aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ def update_body_from_data(self, data, skip_auto_headers):
size = len(data.getbuffer())
self.headers[hdrs.CONTENT_LENGTH] = str(size)
self.chunked = False
elif not self.chunked and isinstance(data, io.BufferedReader):
elif not self.chunked and \
(isinstance(data, io.BufferedReader) or
isinstance(data, io.BufferedRandom)):
# Not chunking if content-length can be determined
try:
size = os.fstat(data.fileno()).st_size - data.tell()
Expand Down
18 changes: 17 additions & 1 deletion tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import zlib
from http.cookies import SimpleCookie
from unittest import mock
import tempfile

import pytest
from multidict import CIMultiDict, CIMultiDictProxy, upstr
from yarl import URL

import aiohttp
from aiohttp import BaseConnector, helpers
from aiohttp import BaseConnector, helpers, hdrs
from aiohttp.client_reqrep import ClientRequest, ClientResponse


Expand Down Expand Up @@ -522,6 +523,21 @@ def test_pass_falsy_data(loop):
yield from req.close()


@asyncio.coroutine
def test_pass_falsy_data_file(loop):
with tempfile.TemporaryFile() as testfile:
testfile.write(b'data')
testfile.seek(0)
skip = frozenset([hdrs.CONTENT_TYPE])
req = ClientRequest(
'post', URL('http://python.org/'),
data=testfile,
skip_auto_headers=skip,
loop=loop)
assert req.headers.get('CONTENT-LENGTH', None) is not None
yield from req.close()


@asyncio.coroutine
def test_get_with_data(loop):
for meth in ClientRequest.GET_METHODS:
Expand Down

0 comments on commit a476058

Please sign in to comment.