1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-08 16:53:23 +00:00

Ensure that request bodies are always bytes, not text (fixes #163).

It shouldn't matter whether the request body comes from a file or a
string, or whether it is passed to the Request constructor or assigned
later. It should always be stored internally as bytes.
This commit is contained in:
Greg Ward
2015-07-16 14:36:26 -04:00
parent bb05b2fcf7
commit 09b7ccf561
3 changed files with 49 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
from six import BytesIO, binary_type
from six import BytesIO, text_type
from six.moves.urllib.parse import urlparse, parse_qsl
@@ -29,11 +29,9 @@ class Request(object):
self.uri = uri
self._was_file = hasattr(body, 'read')
if self._was_file:
self._body = body.read()
if not isinstance(self._body, binary_type):
self._body = self._body.encode('utf-8')
self.body = body.read()
else:
self._body = body
self.body = body
self.headers = {}
for key in headers:
self.add_header(key, headers[key])
@@ -44,6 +42,8 @@ class Request(object):
@body.setter
def body(self, value):
if isinstance(value, text_type):
value = value.encode('utf-8')
self._body = value
def add_header(self, key, value):