Skip to content

Commit

Permalink
ensure our boundary matches: improve the message (#124)
Browse files Browse the repository at this point in the history
* ensure our boundary matches: improve the message

The error message should report expected actual mismatch.

* improve syntax

* use walrus operator

* Oops, walrus not supported

* reverse random paste

* Test multi parser error boundary mismatch error message

* Fix expected error message

* rebase

---------

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
  • Loading branch information
yecril23pl and Kludex authored Sep 29, 2024
1 parent a169d93 commit 83191cb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion multipart/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
else:
# Check to ensure our boundary matches
if c != boundary[index + 2]:
msg = "Did not find boundary character %r at index " "%d" % (c, index + 2)
msg = "Expected boundary character %r, got %r at index %d" % (boundary[index + 2], c, index + 2)
self.logger.warning(msg)
e = MultipartParseError(msg)
e.offset = i
Expand Down
7 changes: 6 additions & 1 deletion tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,12 @@ def test_bad_start_boundary(self) -> None:
self.make("boundary")
data = b"--boundaryfoobar"
with self.assertRaises(MultipartParseError):
i = self.f.write(data)
self.f.write(data)

self.make("boundary")
data = b"--Boundary\r\nfoobar"
with self.assertRaisesRegex(MultipartParseError, "Expected boundary character %r, got %r" % (b"b"[0], b"B"[0])):
self.f.write(data)

def test_octet_stream(self) -> None:
files: list[File] = []
Expand Down

0 comments on commit 83191cb

Please sign in to comment.