@@ -756,21 +756,22 @@ def test_get_port_with_x_forwarded_port(self):
756
756
self .assertEqual (request .get_port (), '8080' )
757
757
758
758
@override_settings (DEBUG = True , ALLOWED_HOSTS = [])
759
- def test_host_validation_disabled_in_debug_mode (self ):
760
- """If ALLOWED_HOSTS is empty and DEBUG is True, all hosts pass."""
761
- request = HttpRequest ()
762
- request .META = {
763
- 'HTTP_HOST' : 'example.com' ,
764
- }
765
- self .assertEqual (request .get_host (), 'example.com' )
759
+ def test_host_validation_in_debug_mode (self ):
760
+ """
761
+ If ALLOWED_HOSTS is empty and DEBUG is True, variants of localhost are
762
+ allowed.
763
+ """
764
+ valid_hosts = ['localhost' , '127.0.0.1' , '[::1]' ]
765
+ for host in valid_hosts :
766
+ request = HttpRequest ()
767
+ request .META = {'HTTP_HOST' : host }
768
+ self .assertEqual (request .get_host (), host )
766
769
767
- # Invalid hostnames would normally raise a SuspiciousOperation,
768
- # but we have DEBUG=True, so this check is disabled.
769
- request = HttpRequest ()
770
- request .META = {
771
- 'HTTP_HOST' : "invalid_hostname.com" ,
772
- }
773
- self .assertEqual (request .get_host (), "invalid_hostname.com" )
770
+ # Other hostnames raise a SuspiciousOperation.
771
+ with self .assertRaises (SuspiciousOperation ):
772
+ request = HttpRequest ()
773
+ request .META = {'HTTP_HOST' : 'example.com' }
774
+ request .get_host ()
774
775
775
776
@override_settings (ALLOWED_HOSTS = [])
776
777
def test_get_host_suggestion_of_allowed_host (self ):
0 commit comments