Skip to content

Commit

Permalink
BLANK_SCHEME -> NO_SCHEME, option for custom value (#21)
Browse files Browse the repository at this point in the history
* BLANK_SCHEME -> NO_SCHEME, option for custom value

Also use constant in test.

* Add docstring for param
  • Loading branch information
birdsarah authored Apr 9, 2020
1 parent 6c97221 commit ff5d63a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
12 changes: 7 additions & 5 deletions domain_utils/domain_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from tldextract import TLDExtract
from urllib.parse import urlparse

BLANK_SCHEME = 'blank'
NO_SCHEME = 'no_scheme'


def _load_and_update_extractor(function):
Expand Down Expand Up @@ -240,15 +240,17 @@ def get_stripped_url(url, scheme=False, drop_non_http=False, use_netloc=True, ex
)


def get_scheme(url):

def get_scheme(url, no_scheme=NO_SCHEME):
"""
Given an url extract from it the scheme
Given an url, extract from it the scheme.
Parameters
----------
url: string
The URL from where we want to get the scheme
no_scheme: any
The value to use if no scheme is detected.
Default is ``no_scheme``
Returns
----------
Expand All @@ -261,4 +263,4 @@ def get_scheme(url):
if scheme:
return scheme
else:
return BLANK_SCHEME
return no_scheme
26 changes: 15 additions & 11 deletions tests/test_get_scheme.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
from domain_utils import get_scheme
from domain_utils import get_scheme, NO_SCHEME


def test_get_scheme_no_scheme():
def test_no_scheme():
result = get_scheme("domain.net")
assert result == 'blank'
assert result == NO_SCHEME


def test_get_scheme_file():
def test_file():
result = get_scheme("file:///home/user/index.html")
assert result == 'file'


def test_get_scheme_https():
def test_https():
result = get_scheme("https://domain.net")
assert result == 'https'


def test_get_scheme_http():
def test_http():
result = get_scheme("http://domain.net")
assert result == 'http'


def test_get_scheme_about():
def test_about():
result = get_scheme("about:config")
assert result == 'about'


def test_get_scheme_webpack():
def test_webpack():
result = get_scheme("webpack://index.js")
assert result == 'webpack'


def test_get_scheme_empty():
def test_empty():
result = get_scheme("")
assert result == 'blank'
assert result == NO_SCHEME


def test_get_scheme_ws():
def test_ws():
result = get_scheme("ws://socket")
assert result == 'ws'


def test_pass_custom_empty_value():
assert get_scheme('10.0.0.1/path/to/index.html', no_scheme=None) is None

0 comments on commit ff5d63a

Please sign in to comment.