Skip to content

Commit

Permalink
add tests and docs for strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
hbusul committed Sep 13, 2023
1 parent 75e0599 commit af2dbcd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
34 changes: 31 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ The :code:`StreamingFormDataParser` class expects a dictionary of HTTP request
headers when being instantiated. These headers are used to determine the input
:code:`Content-Type` and a few other metadata.

Optionally, you can enable strict mode in the parser by setting the :code:`strict`
keyword argument to :code:`True`. In strict mode, the parser throws
:code:`UnexpectedPartException` if it starts to parse a field whose name has not
been registered. When not in strict mode, unexpected parts are silently ignored.

2. Input Registration
~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -118,9 +123,10 @@ API
:code:`StreamingFormDataParser`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This class is the main entry point, and expects a dictionary of HTTP request
:code:`headers`. These headers are used to determine the input
:code:`Content-Type` and a few other metadata.
This class is the main entry point. It expects a dictionary of HTTP request
:code:`headers` and has a keyword argument :code:`strict`. The headers are used
to determine the input :code:`Content-Type` and a few other metadata. The strict
flag is used to enable or disable the strict mode.

:code:`Target` classes
~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -225,6 +231,28 @@ snippet.
>>> target = ValueTarget(validator=MaxSizeValidator(100))
Exceptions
~~~~~~~~~~

:code:`ParseFailedException`
````````````````````````````
This exception is the base class of the :code:`streaming_form_data` exceptions.
It can be raised during initialization, registering parts or reading chunks.

:code:`UnexpectedPartException`
```````````````````````````````
This exception is raised when the parser is in strict mode and starts to parse
an unexpected part. It contains :code:`part_name` attribute to check the name of
the unexpected part. In can only be raised from :code:`data_received`.

.. code-block:: python
>>> try:
>>> parser.data_received(chunk)
>>> except streaming_form_data.parser.UnexpectedPartException as e:
>>> print(e.part_name)
>>> raise
Examples
--------

Expand Down
17 changes: 17 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from streaming_form_data import StreamingFormDataParser
from streaming_form_data.targets import ValueTarget
from streaming_form_data.parser import UnexpectedPartException


class CustomTarget(ValueTarget):
Expand All @@ -23,3 +24,19 @@ def test_custom_target_exception():

with pytest.raises(ValueError):
parser.data_received(data)


def test_unexpected_part_exception():
target = ValueTarget()

encoder = MultipartEncoder(fields={"value": "hello world", "extra": "field"})

parser = StreamingFormDataParser(
headers={"Content-Type": encoder.content_type}, strict=True
)
parser.register("value", target)

data = encoder.to_string()

with pytest.raises(UnexpectedPartException):
parser.data_received(data)

0 comments on commit af2dbcd

Please sign in to comment.