@@ -709,21 +709,22 @@ def test_get_port_with_x_forwarded_port(self):
709
709
self .assertEqual (request .get_port (), '8080' )
710
710
711
711
@override_settings (DEBUG = True , ALLOWED_HOSTS = [])
712
- def test_host_validation_disabled_in_debug_mode (self ):
713
- """If ALLOWED_HOSTS is empty and DEBUG is True, all hosts pass."""
714
- request = HttpRequest ()
715
- request .META = {
716
- 'HTTP_HOST' : 'example.com' ,
717
- }
718
- self .assertEqual (request .get_host (), 'example.com' )
712
+ def test_host_validation_in_debug_mode (self ):
713
+ """
714
+ If ALLOWED_HOSTS is empty and DEBUG is True, variants of localhost are
715
+ allowed.
716
+ """
717
+ valid_hosts = ['localhost' , '127.0.0.1' , '[::1]' ]
718
+ for host in valid_hosts :
719
+ request = HttpRequest ()
720
+ request .META = {'HTTP_HOST' : host }
721
+ self .assertEqual (request .get_host (), host )
719
722
720
- # Invalid hostnames would normally raise a SuspiciousOperation,
721
- # but we have DEBUG=True, so this check is disabled.
722
- request = HttpRequest ()
723
- request .META = {
724
- 'HTTP_HOST' : "invalid_hostname.com" ,
725
- }
726
- self .assertEqual (request .get_host (), "invalid_hostname.com" )
723
+ # Other hostnames raise a SuspiciousOperation.
724
+ with self .assertRaises (SuspiciousOperation ):
725
+ request = HttpRequest ()
726
+ request .META = {'HTTP_HOST' : 'example.com' }
727
+ request .get_host ()
727
728
728
729
@override_settings (ALLOWED_HOSTS = [])
729
730
def test_get_host_suggestion_of_allowed_host (self ):
0 commit comments