Skip to content

Commit

Permalink
stripping www. from netloc
Browse files Browse the repository at this point in the history
  • Loading branch information
iambibhas committed Aug 27, 2020
1 parent 85c22df commit 4da48ee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 4 additions & 1 deletion baseframe/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ def cleanurl_filter(url):
if not isinstance(url, furl):
url = furl(url)
url.path.normalize()
return furl().set(netloc=url.netloc, path=url.path).url.lstrip('//').rstrip('/')
netloc = url.netloc.lstrip('www.') if url.netloc else url.netloc
# if scheme is missing, netloc becomes a part of path
path = str(url.path).lstrip('www.') if not url.scheme and url.path else url.path
return furl().set(netloc=netloc, path=path).url.lstrip('//').rstrip('/')


@baseframe.app_template_filter('make_relative_url')
Expand Down
7 changes: 4 additions & 3 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,20 +366,21 @@ def test_none_if_empty(self):

def test_cleanurl(self):
assert (
filters.cleanurl_filter("https://example.com/some/path/?query=value")
filters.cleanurl_filter("https://www.example.com/some/path/?query=value")
== "example.com/some/path"
)
assert (
filters.cleanurl_filter("example.com/some/path/?query=value")
== "example.com/some/path"
)
assert (
filters.cleanurl_filter("example.com/some/path/") == "example.com/some/path"
filters.cleanurl_filter("www.example.com/some/path/")
== "example.com/some/path"
)
assert (
filters.cleanurl_filter("example.com/some/path") == "example.com/some/path"
)
assert filters.cleanurl_filter("example.com/") == "example.com"
assert filters.cleanurl_filter("http://www.example.com/") == "example.com"
assert filters.cleanurl_filter("//example.com/") == "example.com"
assert filters.cleanurl_filter("//test/") == "test"
assert filters.cleanurl_filter("foobar") == "foobar"

0 comments on commit 4da48ee

Please sign in to comment.