Skip to content

Commit

Permalink
Add support for casting ImageUpload type to bytes (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkraczkowski committed Oct 26, 2021
1 parent fa56ffe commit 647e275
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion chocs/__version__.py
@@ -1 +1 @@
__version__ = "1.2.0"
__version__ = "1.3.0"
5 changes: 4 additions & 1 deletion chocs/http/http_multipart_message_parser.py
Expand Up @@ -51,7 +51,6 @@ def __bool__(self) -> bool:
return len(self) > 0

def __str__(self) -> str:

if not self._str:
self._str = self.read().decode()

Expand All @@ -63,6 +62,10 @@ def __enter__(self) -> IO[Any]:
def __exit__(self, *args: Any) -> None:
self.close()

def __bytes__(self) -> bytes:
self.seek(0)
return self.read()


class ParserState(Enum):
PART_BOUNDARY = 0
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "chocs"
version = "1.2.0"
version = "1.3.0"
description = "Lightweight and powerful WSGI/AWS lambda framework for rapid building rest applications."
authors = [
"Dawid Kraczkowski <dawid.kraczkowski@gmail.com>"
Expand Down
12 changes: 12 additions & 0 deletions tests/http/test_http_message.py
Expand Up @@ -129,3 +129,15 @@ def test_simple_http_message() -> None:
assert http_message == "Hello World!"
assert http_message.upper() == "HELLO WORLD!"
assert isinstance(http_message, SimpleHttpMessage)


def test_can_convert_uploaded_file_to_bytes() -> None:
request = HttpRequest(
multipart_body["REQUEST_METHOD"],
body=multipart_body["wsgi.input"],
headers={"content-type": multipart_body["CONTENT_TYPE"]},
)
body = request.parsed_body
file_bytes = bytes(body["file_a"])

assert file_bytes == b"GIF87a\x02\x00\x02\x00\x91\x00\x00\x00\x00\x00\xff\x8c\x00\xff\xff\xff\x00\x00\x00!\xf9\x04\t\x00\x00\x03\x00,\x00\x00\x00\x00\x02\x00\x02\x00\x00\x02\x02\x8cS\x00;"

0 comments on commit 647e275

Please sign in to comment.