From dcbf08cce59ceb83918b1b63c2bb827828bbdd2c Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 20 Oct 2012 15:36:24 +0200 Subject: Fixed #19094 -- Improved FakePayload to support write, len and string input MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thanks Ondrej Slinták for the suggestion. --- django/test/client.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'django') diff --git a/django/test/client.py b/django/test/client.py index 8fd765ec9a..6d12321075 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -43,11 +43,20 @@ class FakePayload(object): length. This makes sure that views can't do anything under the test client that wouldn't work in Real Life. """ - def __init__(self, content): - self.__content = BytesIO(content) - self.__len = len(content) + def __init__(self, content=None): + self.__content = BytesIO() + self.__len = 0 + self.read_started = False + if content is not None: + self.write(content) + + def __len__(self): + return self.__len def read(self, num_bytes=None): + if not self.read_started: + self.__content.seek(0) + self.read_started = True if num_bytes is None: num_bytes = self.__len or 0 assert self.__len >= num_bytes, "Cannot read more than the available bytes from the HTTP incoming data." @@ -55,6 +64,13 @@ class FakePayload(object): self.__len -= num_bytes return content + def write(self, content): + if self.read_started: + raise ValueError("Unable to write a payload after he's been read") + content = force_bytes(content) + self.__content.write(content) + self.__len += len(content) + class ClientHandler(BaseHandler): """ -- cgit v1.3