diff options
| author | farhan <farhanalirazaazeemi@gmail.com> | 2026-01-06 00:34:39 +0500 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2026-02-10 17:59:02 -0500 |
| commit | 7732f942a98a709750fc1fed2c69741183844a3c (patch) | |
| tree | 6d290a5577ef26eda941243874eba700540c6495 /tests/requests_tests | |
| parent | 56ed37e17e5b1a509aa68a0c797dcff34fcc1366 (diff) | |
Fixed #36841 -- Made multipart parser class pluggable on HttpRequest.
Diffstat (limited to 'tests/requests_tests')
| -rw-r--r-- | tests/requests_tests/tests.py | 72 |
1 files changed, 71 insertions, 1 deletions
diff --git a/tests/requests_tests/tests.py b/tests/requests_tests/tests.py index 36843df9b6..e52989b0da 100644 --- a/tests/requests_tests/tests.py +++ b/tests/requests_tests/tests.py @@ -13,7 +13,11 @@ from django.http import ( RawPostDataException, UnreadablePostError, ) -from django.http.multipartparser import MAX_TOTAL_HEADER_SIZE, MultiPartParserError +from django.http.multipartparser import ( + MAX_TOTAL_HEADER_SIZE, + MultiPartParser, + MultiPartParserError, +) from django.http.request import split_domain_port from django.test import RequestFactory, SimpleTestCase, override_settings from django.test.client import BOUNDARY, MULTIPART_CONTENT, FakePayload @@ -1112,6 +1116,72 @@ class RequestsTests(SimpleTestCase): request.session["key"] = "value" self.assertEqual(request_copy.session, {}) + def test_custom_multipart_parser_class(self): + + class CustomMultiPartParser(MultiPartParser): + def parse(self): + post, files = super().parse() + post._mutable = True + post["custom_parser_used"] = "yes" + post._mutable = False + return post, files + + class CustomWSGIRequest(WSGIRequest): + multipart_parser_class = CustomMultiPartParser + + payload = FakePayload( + "\r\n".join( + [ + "--boundary", + 'Content-Disposition: form-data; name="name"', + "", + "value", + "--boundary--", + ] + ) + ) + request = CustomWSGIRequest( + { + "REQUEST_METHOD": "POST", + "CONTENT_TYPE": "multipart/form-data; boundary=boundary", + "CONTENT_LENGTH": len(payload), + "wsgi.input": payload, + } + ) + self.assertEqual(request.POST.get("custom_parser_used"), "yes") + self.assertEqual(request.POST.get("name"), "value") + + def test_multipart_parser_class_immutable_after_parse(self): + payload = FakePayload( + "\r\n".join( + [ + "--boundary", + 'Content-Disposition: form-data; name="name"', + "", + "value", + "--boundary--", + ] + ) + ) + request = WSGIRequest( + { + "REQUEST_METHOD": "POST", + "CONTENT_TYPE": "multipart/form-data; boundary=boundary", + "CONTENT_LENGTH": len(payload), + "wsgi.input": payload, + } + ) + + # Access POST to trigger parsing. + request.POST + + msg = ( + "You cannot set the multipart parser class after the upload has been " + "processed." + ) + with self.assertRaisesMessage(RuntimeError, msg): + request.multipart_parser_class = MultiPartParser + class HostValidationTests(SimpleTestCase): poisoned_hosts = [ |
