Skip to content

Commit

Permalink
chore: use MultipartEncoder for multiple inputs test case
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhantgoel committed Nov 6, 2024
1 parent 2205f42 commit c7bf149
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
8 changes: 4 additions & 4 deletions streaming_form_data/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(self, next_target: Callable):

self._next_target = next_target

self._targets: List[BaseTarget] = []
self.targets: List[BaseTarget] = []
self._validator = None # next_target should have a validator

self._next_multipart_filename: Optional[str] = None
Expand All @@ -91,14 +91,14 @@ def on_start(self):
target.set_multipart_filename(self._next_multipart_content_type)
self._next_multipart_content_type = None

self._targets.append(target)
self.targets.append(target)
target.start()

def on_data_received(self, chunk: bytes):
self._targets[-1].data_received(chunk)
self.targets[-1].data_received(chunk)

def on_finish(self):
self._targets[-1].finish()
self.targets[-1].finish()

def set_multipart_filename(self, filename: str):
self._next_multipart_filename = filename
Expand Down
44 changes: 18 additions & 26 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,44 +842,36 @@ def test_extra_headers():


def test_multiple_inputs(tmp_path):
data = b"""\
--111217161535371856203688828
Content-Disposition: form-data; name="files"; filename="first.txt"
Content-Type: text/plain
first
--111217161535371856203688828
Content-Disposition: form-data; name="files"; filename="second.txt"
Content-Type: text/plain
second
--111217161535371856203688828
Content-Disposition: form-data; name="files"; filename="third.txt"
Content-Type: text/plain
third
--111217161535371856203688828--
""".replace(b"\n", b"\r\n")
for filename in ("first.txt", "second.txt", "third.txt"):
with open(tmp_path / filename, "w") as file:
file.write(f"{filename}")

encoder = MultipartEncoder(
fields=[
("files", ("files", open(tmp_path / "first.txt", "rb"), "text/plain")),
("files", ("files", open(tmp_path / "second.txt", "rb"), "text/plain")),
("files", ("files", open(tmp_path / "third.txt", "rb"), "text/plain")),
]
)

class next_target:
def __init__(self):
self._index = 0

def __call__(self):
return FileTarget(tmp_path / f"{self._index}.txt")
return ValueTarget()

target = MultipleTargets(next_target())

parser = StreamingFormDataParser(
headers={
"Content-Type": "multipart/form-data; boundary=111217161535371856203688828"
}
)
parser = StreamingFormDataParser(headers={"Content-Type": encoder.content_type})
parser.register("files", target)

parser.data_received(data)
parser.data_received(encoder.to_string())

assert len(target._targets) == 3
assert len(target.targets) == 3
assert target.targets[0].value == b"first.txt"
assert target.targets[1].value == b"second.txt"
assert target.targets[2].value == b"third.txt"


def test_case_insensitive_content_disposition_header():
Expand Down

0 comments on commit c7bf149

Please sign in to comment.