Skip to content

Commit 1f35978

Browse files
committed
Support URLs without scheme
1 parent fcccac6 commit 1f35978

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

django_hosts/test/client.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ class HostClientMixin:
77
"""Test client to help work with django-hosts in tests."""
88

99
def generic(self, method, path, *args, **extra):
10-
if path.startswith('http'):
10+
scheme, netloc, *_others = urlparse(path)
11+
if scheme:
12+
extra["wsgi.url_scheme"] = scheme
13+
if netloc:
1114
# Populate the host header from the URL host
12-
_scheme, host, *_others = urlparse(path)
13-
if extra.get('headers') is None:
14-
extra['headers'] = {}
15-
if extra['headers'].get('host') is None:
16-
extra['headers']['host'] = host
15+
extra["headers"] = extra["headers"] or {}
16+
if extra["headers"].get("host") is None:
17+
extra["headers"]["host"] = netloc
1718
return super().generic(method, path, *args, **extra)
1819

1920

tests/test_client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
class TestHostsClient(TestCase):
77
client_class = HostsClient
88

9-
def test_host_header_set_from_url(self):
9+
def test_host_header_set_from_full_url(self):
1010
response = self.client.get('https://testserver.local/api/v1/users/')
1111
assert response.request['HTTP_HOST'] == 'testserver.local'
1212

13+
def test_host_header_set_from_url_without_scheme(self):
14+
response = self.client.get('//testserver.local/api/v1/users/')
15+
assert response.request['HTTP_HOST'] == 'testserver.local'
16+
1317
def test_host_header_from_user_is_not_overridden(self):
1418
response = self.client.get(
1519
'https://testserver.local/api/v1/users/', headers={'host': 'api.example.com'}

0 commit comments

Comments
 (0)