11import copy
2+ import itertools
23import json
34import os
45import pickle
@@ -488,15 +489,24 @@ def test_stream_interface(self):
488489
489490 def test_redirect_url_max_length (self ):
490491 base_url = "https://example.com/"
491- for length in (
492- MAX_URL_REDIRECT_LENGTH - 1 ,
493- MAX_URL_REDIRECT_LENGTH ,
492+ for length , response_class in itertools . product (
493+ ( MAX_URL_REDIRECT_LENGTH - 1 , MAX_URL_REDIRECT_LENGTH ) ,
494+ ( HttpResponseRedirect , HttpResponsePermanentRedirect ) ,
494495 ):
495496 long_url = base_url + "x" * (length - len (base_url ))
496- with self .subTest (length = length ):
497- response = HttpResponseRedirect (long_url )
497+ with self .subTest (length = length , response_class = response_class ):
498+ response = response_class (long_url )
498499 self .assertEqual (response .url , long_url )
499- response = HttpResponsePermanentRedirect (long_url )
500+
501+ def test_redirect_url_max_length_override_via_param (self ):
502+ base_url = "https://example.com/"
503+ for (max_length , length ), response_class in itertools .product (
504+ ((None , MAX_URL_REDIRECT_LENGTH + 1 ), (100 , 99 ), (100 , 100 )),
505+ (HttpResponseRedirect , HttpResponsePermanentRedirect ),
506+ ):
507+ long_url = base_url + "x" * (length - len (base_url ))
508+ with self .subTest (length = length , response_class = response_class ):
509+ response = response_class (long_url , max_length = max_length )
500510 self .assertEqual (response .url , long_url )
501511
502512 def test_unsafe_redirect (self ):
@@ -506,11 +516,23 @@ def test_unsafe_redirect(self):
506516 "file:///etc/passwd" ,
507517 "é" * (MAX_URL_REDIRECT_LENGTH + 1 ),
508518 ]
509- for url in bad_urls :
510- with self .assertRaises (DisallowedRedirect ):
511- HttpResponseRedirect (url )
512- with self .assertRaises (DisallowedRedirect ):
513- HttpResponsePermanentRedirect (url )
519+ for url , response_class in itertools .product (
520+ bad_urls , (HttpResponseRedirect , HttpResponsePermanentRedirect )
521+ ):
522+ with (
523+ self .subTest (url = url , response_class = response_class ),
524+ self .assertRaises (DisallowedRedirect ),
525+ ):
526+ response_class (url )
527+
528+ def test_unsafe_redirect_via_max_length (self ):
529+ url = "https://example.com/"
530+ for response_class in (HttpResponseRedirect , HttpResponsePermanentRedirect ):
531+ with (
532+ self .subTest (response_class = response_class ),
533+ self .assertRaises (DisallowedRedirect ),
534+ ):
535+ response_class (url , max_length = len (url ) - 1 )
514536
515537 def test_header_deletion (self ):
516538 r = HttpResponse ("hello" )
0 commit comments