Skip to content

Commit

Permalink
added activate subdomains method
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Jun 12, 2018
1 parent b1717cc commit 3d7ae8d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
15 changes: 10 additions & 5 deletions masonite/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self, environ=None):
self.redirect_route = False
self.user_model = None
self.subdomain = None
self._activate_subdomains = False
self._status = '404 Not Found'

if environ:
Expand Down Expand Up @@ -362,14 +363,18 @@ def compile_route_to_url(self, route, params={}):
compiled_url = compiled_url.replace('//', '/')

return compiled_url

def activate_subdomains(self):
self._activate_subdomains = True

def has_subdomain(self):
url = tldextract.extract(self.environ['HTTP_HOST'])
if self._activate_subdomains:
url = tldextract.extract(self.environ['HTTP_HOST'])

if url.subdomain:
self.subdomain = url.subdomain
self.url_params.update({'subdomain': self.subdomain})
return True
if url.subdomain:
self.subdomain = url.subdomain
self.url_params.update({'subdomain': self.subdomain})
return True

return False

Expand Down
5 changes: 4 additions & 1 deletion tests/providers/test_route_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,13 @@ def test_route_subdomain_ignores_routes(self):
self.app.make('Environ')['HTTP_HOST'] = 'subb.domain.com'
self.app.bind('WebRoutes', [get('/test', Controller.show)])

request = self.app.make('Request')
request.activate_subdomains()

self.provider.boot(
self.app.make('WebRoutes'),
self.app.make('Route'),
self.app.make('Request'),
request,
self.app.make('Environ'),
self.app.make('Headers'),
)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_request_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class TestRequestRoutes:
def setup_method(self):
self.request = Request(generate_wsgi()).key(
'NCTpkICMlTXie5te9nJniMj9aVbPM6lsjeq5iDZ0dqY=')

self.request.activate_subdomains()

def test_get_initialized(self):
assert callable(Get)
Expand Down Expand Up @@ -67,6 +69,8 @@ def test_method_type_has_required_subdomain_with_asterick(self):

request.environ['HTTP_HOST'] = 'test.localhost:8000'

request.activate_subdomains()

get = Get().domain('*')
post = Get().domain('*')

Expand All @@ -83,6 +87,8 @@ def test_request_sets_subdomain_on_get(self):

request.environ['HTTP_HOST'] = 'test.localhost:8000'

request.activate_subdomains()

get = Get().domain('*')
post = Get().domain('*')

Expand Down

0 comments on commit 3d7ae8d

Please sign in to comment.