diff options
| author | Claude Paroz <claude@2xlibre.net> | 2012-10-20 15:36:24 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2012-10-20 15:36:24 +0200 |
| commit | dcbf08cce59ceb83918b1b63c2bb827828bbdd2c (patch) | |
| tree | 009de73799b60ed78eda80347aa3e7feb7ea63f1 /django | |
| parent | dfd4a7175119ddb422d8426dcc15902265d5a428 (diff) | |
Fixed #19094 -- Improved FakePayload to support write, len and string input
Thanks Ondrej Slinták for the suggestion.
Diffstat (limited to 'django')
| -rw-r--r-- | django/test/client.py | 22 |
1 files changed, 19 insertions, 3 deletions
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): """ |
