mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 09:13:23 +00:00
Fix python3 support for requests file uploads.
This commit is contained in:
@@ -211,9 +211,9 @@ def test_post_file(tmpdir, scheme):
|
|||||||
# This also tests that we do the right thing with matching the body when they are files.
|
# This also tests that we do the right thing with matching the body when they are files.
|
||||||
with vcr.use_cassette(str(tmpdir.join('post_file.yaml')),
|
with vcr.use_cassette(str(tmpdir.join('post_file.yaml')),
|
||||||
match_on=('method', 'scheme', 'host', 'port', 'path', 'query', 'body')) as cass:
|
match_on=('method', 'scheme', 'host', 'port', 'path', 'query', 'body')) as cass:
|
||||||
with open('tox.ini') as f:
|
with open('tox.ini', 'rb') as f:
|
||||||
tox_content = f.read()
|
tox_content = f.read()
|
||||||
assert cass.requests[0].body.read() == tox_content
|
assert cass.requests[0].body.read() == tox_content
|
||||||
with open('tox.ini') as f:
|
with open('tox.ini', 'rb') as f:
|
||||||
new_response = requests.post(url, f).content
|
new_response = requests.post(url, f).content
|
||||||
assert original_response == new_response
|
assert original_response == new_response
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import StringIO
|
from six import BytesIO, binary_type
|
||||||
from six.moves.urllib.parse import urlparse, parse_qsl
|
from six.moves.urllib.parse import urlparse, parse_qsl
|
||||||
|
|
||||||
|
|
||||||
@@ -28,14 +28,19 @@ class Request(object):
|
|||||||
self.method = method
|
self.method = method
|
||||||
self.uri = uri
|
self.uri = uri
|
||||||
self._was_file = hasattr(body, 'read')
|
self._was_file = hasattr(body, 'read')
|
||||||
self._body = body.read() if self._was_file else body
|
if self._was_file:
|
||||||
|
self._body = body.read()
|
||||||
|
if not isinstance(self._body, binary_type):
|
||||||
|
self._body = self._body.encode('utf-8')
|
||||||
|
else:
|
||||||
|
self._body = body
|
||||||
self.headers = {}
|
self.headers = {}
|
||||||
for key in headers:
|
for key in headers:
|
||||||
self.add_header(key, headers[key])
|
self.add_header(key, headers[key])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def body(self):
|
def body(self):
|
||||||
return StringIO.StringIO(self._body) if self._was_file else self._body
|
return BytesIO(self._body) if self._was_file else self._body
|
||||||
|
|
||||||
@body.setter
|
@body.setter
|
||||||
def body(self, value):
|
def body(self, value):
|
||||||
|
|||||||
Reference in New Issue
Block a user