diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index 29c4a485d2..e05a203e4f 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -113,17 +113,10 @@ class ZulipTestCase(TestCase): def set_http_host(self, kwargs): # type: (Dict[str, Any]) -> None if 'subdomain' in kwargs: - if kwargs['subdomain'] != "": - kwargs["HTTP_HOST"] = "%s.%s" % (kwargs["subdomain"], settings.EXTERNAL_HOST) - else: - kwargs["HTTP_HOST"] = settings.EXTERNAL_HOST + kwargs['HTTP_HOST'] = Realm.host_for_subdomain(kwargs['subdomain']) del kwargs['subdomain'] elif 'HTTP_HOST' not in kwargs: - if self.DEFAULT_SUBDOMAIN == "": - kwargs["HTTP_HOST"] = settings.EXTERNAL_HOST - else: - kwargs["HTTP_HOST"] = "%s.%s" % (self.DEFAULT_SUBDOMAIN, - settings.EXTERNAL_HOST,) + kwargs['HTTP_HOST'] = Realm.host_for_subdomain(self.DEFAULT_SUBDOMAIN) @instrument_url def client_patch(self, url, info={}, **kwargs): diff --git a/zerver/models.py b/zerver/models.py index ace2f8849c..18a466f3c3 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -276,8 +276,13 @@ class Realm(ModelReprMixin, models.Model): @property def host(self): # type: () -> str - if self.subdomain not in [None, ""]: - return "%s.%s" % (self.subdomain, settings.EXTERNAL_HOST) + return self.host_for_subdomain(self.subdomain) + + @staticmethod + def host_for_subdomain(subdomain): + # type: (str) -> str + if subdomain not in [None, ""]: + return "%s.%s" % (subdomain, settings.EXTERNAL_HOST) return settings.EXTERNAL_HOST @property