@@ -465,6 +465,130 @@ def test_multipart_multi_field_app_reads_body(tmpdir: Path, test_client_factory:
465465 assert response .json () == {"some" : "data" , "second" : "key pair" }
466466
467467
468+ @pytest .mark .parametrize (
469+ "app,expectation" ,
470+ [
471+ (app , pytest .raises (MultiPartException )),
472+ (Starlette (routes = [Mount ("/" , app = app )]), does_not_raise ()),
473+ ],
474+ )
475+ def test_urlencoded_too_many_fields_raise (
476+ app : ASGIApp ,
477+ expectation : AbstractContextManager [Exception ],
478+ test_client_factory : TestClientFactory ,
479+ ) -> None :
480+ client = test_client_factory (app )
481+ data = "&" .join (f"N{ i } =" for i in range (1001 ))
482+ with expectation :
483+ res = client .post (
484+ "/" ,
485+ content = data ,
486+ headers = {"Content-Type" : "application/x-www-form-urlencoded" },
487+ )
488+ assert res .status_code == 400
489+ assert res .text == "Too many fields. Maximum number of fields is 1000."
490+
491+
492+ @pytest .mark .parametrize (
493+ "app,expectation" ,
494+ [
495+ (app , pytest .raises (MultiPartException )),
496+ (Starlette (routes = [Mount ("/" , app = app )]), does_not_raise ()),
497+ ],
498+ )
499+ def test_urlencoded_field_exceeds_max_part_size_raise (
500+ app : ASGIApp ,
501+ expectation : AbstractContextManager [Exception ],
502+ test_client_factory : TestClientFactory ,
503+ ) -> None :
504+ client = test_client_factory (app )
505+ data = "field=" + "x" * (1024 * 1024 + 1 )
506+ with expectation :
507+ res = client .post (
508+ "/" ,
509+ content = data ,
510+ headers = {"Content-Type" : "application/x-www-form-urlencoded" },
511+ )
512+ assert res .status_code == 400
513+ assert res .text == "Field exceeded maximum size of 1024KB."
514+
515+
516+ @pytest .mark .parametrize (
517+ "app,expectation" ,
518+ [
519+ (app , pytest .raises (MultiPartException )),
520+ (Starlette (routes = [Mount ("/" , app = app )]), does_not_raise ()),
521+ ],
522+ )
523+ def test_urlencoded_field_name_exceeds_max_part_size_raise (
524+ app : ASGIApp ,
525+ expectation : AbstractContextManager [Exception ],
526+ test_client_factory : TestClientFactory ,
527+ ) -> None :
528+ client = test_client_factory (app )
529+ data = "x" * (1024 * 1024 + 1 ) + "=value"
530+ with expectation :
531+ res = client .post (
532+ "/" ,
533+ content = data ,
534+ headers = {"Content-Type" : "application/x-www-form-urlencoded" },
535+ )
536+ assert res .status_code == 400
537+ assert res .text == "Field exceeded maximum size of 1024KB."
538+
539+
540+ @pytest .mark .parametrize (
541+ "app,expectation" ,
542+ [
543+ (make_app_max_parts (max_fields = 1 ), pytest .raises (MultiPartException )),
544+ (
545+ Starlette (routes = [Mount ("/" , app = make_app_max_parts (max_fields = 1 ))]),
546+ does_not_raise (),
547+ ),
548+ ],
549+ )
550+ def test_urlencoded_max_fields_is_customizable (
551+ app : ASGIApp ,
552+ expectation : AbstractContextManager [Exception ],
553+ test_client_factory : TestClientFactory ,
554+ ) -> None :
555+ client = test_client_factory (app )
556+ with expectation :
557+ res = client .post (
558+ "/" ,
559+ content = "a=1&b=2" ,
560+ headers = {"Content-Type" : "application/x-www-form-urlencoded" },
561+ )
562+ assert res .status_code == 400
563+ assert res .text == "Too many fields. Maximum number of fields is 1."
564+
565+
566+ @pytest .mark .parametrize (
567+ "app,expectation" ,
568+ [
569+ (make_app_max_parts (max_part_size = 1024 * 10 ), pytest .raises (MultiPartException )),
570+ (
571+ Starlette (routes = [Mount ("/" , app = make_app_max_parts (max_part_size = 1024 * 10 ))]),
572+ does_not_raise (),
573+ ),
574+ ],
575+ )
576+ def test_urlencoded_max_part_size_is_customizable (
577+ app : ASGIApp ,
578+ expectation : AbstractContextManager [Exception ],
579+ test_client_factory : TestClientFactory ,
580+ ) -> None :
581+ client = test_client_factory (app )
582+ with expectation :
583+ res = client .post (
584+ "/" ,
585+ content = "field=" + "x" * (1024 * 10 + 1 ),
586+ headers = {"Content-Type" : "application/x-www-form-urlencoded" },
587+ )
588+ assert res .status_code == 400
589+ assert res .text == "Field exceeded maximum size of 10KB."
590+
591+
468592def test_user_safe_decode_helper () -> None :
469593 result = _user_safe_decode (b"\xc4 \x99 \xc5 \xbc \xc4 \x87 " , "utf-8" )
470594 assert result == "ężć"
0 commit comments