# Path: django_core/utils/urls.py
# def get_query_values_from_url(url, keys=None):
#     """Gets query string values from a url.
# 
#     if a list of keys are provided, then a dict will be returned.  If only a
#     single string key is provided, then only a single value will be returned.
# 
#     >>> url = 'http://helloworld.com/some/path?test=5&hello=world&john=doe'
#     >>> get_query_values_from_url(url=url, keys='test')
#     "5"
#     >>> get_query_values_from_url(url=url, keys=['test'])
#     {'test': '5'}
#     >>> get_query_values_from_url(url=url, keys=['test', 'john'])
#     {'test': '5', 'john': 'doe'}
#     >>> get_query_values_from_url(url=url, keys=['test', 'john', 'blah'])
#     {'test': '5', 'john': 'doe', 'blah': None}
#     """
#     if not url or '?' not in url:
#         # no query params
#         return None
# 
#     parsed_url = urlparse(url)
#     query = dict(parse_qsl(parsed_url.query))
# 
#     if keys is None:
#         return query
# 
#     if isinstance(keys, string_types):
#         return query.get(keys)
# 
#     return {k: query.get(k) for k in keys}
# 
# Path: django_core/utils/urls.py
# def replace_url_query_values(url, replace_vals):
#     """Replace querystring values in a url string.
# 
#     >>> url = 'http://helloworld.com/some/path?test=5'
#     >>> replace_vals = {'test': 10}
#     >>> replace_url_query_values(url=url, replace_vals=replace_vals)
#     'http://helloworld.com/some/path?test=10'
#     """
#     if '?' not in url:
#         return url
# 
#     parsed_url = urlparse(url)
#     query = dict(parse_qsl(parsed_url.query))
#     query.update(replace_vals)
#     return '{0}?{1}'.format(url.split('?')[0], urlencode(query))
# 
# Path: django_core/utils/urls.py
# def is_legit_next_url(next_url):
# 
#     if not next_url:
#         return False
# 
#     is_fully_qualified_url = (next_url.startswith('http') or
#                               next_url.startswith('//'))
#     site_domain = get_setting('SITE_DOMAIN', default=None)
#     parsed_url = urlparse(next_url)
# 
#     try:
#         portless_netloc = parsed_url.netloc.split(':')[0]
#     except:
#         portless_netloc = None
# 
#     if is_fully_qualified_url:
#         # domain validaition required
# 
#         if not site_domain:
#             # can't validate without a SITE_DOMAIN setting
#             return False
# 
#         if portless_netloc and portless_netloc.endswith(site_domain):
#             # trusted domain
#             return True
# 
#         return False
# 
#     if next_url.startswith('/'):
#         # url relative to the current site
#         return True
# 
#     return False


from django.test.testcases import TestCase
from django_core.utils.urls import get_query_values_from_url
from django_core.utils.urls import replace_url_query_values
from django_core.utils.urls import is_legit_next_url


class UrlUtilsTestCase(TestCase):
    """Test case for url utils."""

    def test_replace_url_query_values(self):
        """Test replacing querysting values in a url."""
        url = 'http://helloworld.com/some/path?test=5'
        replace_vals = {'test': 10}
        new_url = replace_url_query_values(url=url, replace_vals=replace_vals)
        self.assertEqual(new_url, 'http://helloworld.com/some/path?test=10')

    def test_get_query_values_from_url_single_key(self):
        """Test getting query values from a url."""
        url = 'http://helloworld.com/some/path?test=5&hello=world&john=doe'
        self.assertEqual("5", get_query_values_from_url(url=url, keys='test'))

    def test_get_query_values_from_url_single_key_list(self):
        """Test getting query values from a url."""
        url = 'http://helloworld.com/some/path?test=5&hello=world&john=doe'
        self.assertEqual(
            {'test': '5'},

# Path: django_core/utils/urls.py
# def get_query_values_from_url(url, keys=None):
#     """Gets query string values from a url.
# 
#     if a list of keys are provided, then a dict will be returned.  If only a
#     single string key is provided, then only a single value will be returned.
# 
#     >>> url = 'http://helloworld.com/some/path?test=5&hello=world&john=doe'
#     >>> get_query_values_from_url(url=url, keys='test')
#     "5"
#     >>> get_query_values_from_url(url=url, keys=['test'])
#     {'test': '5'}
#     >>> get_query_values_from_url(url=url, keys=['test', 'john'])
#     {'test': '5', 'john': 'doe'}
#     >>> get_query_values_from_url(url=url, keys=['test', 'john', 'blah'])
#     {'test': '5', 'john': 'doe', 'blah': None}
#     """
#     if not url or '?' not in url:
#         # no query params
#         return None
# 
#     parsed_url = urlparse(url)
#     query = dict(parse_qsl(parsed_url.query))
# 
#     if keys is None:
#         return query
# 
#     if isinstance(keys, string_types):
#         return query.get(keys)
# 
#     return {k: query.get(k) for k in keys}
# 
# Path: django_core/utils/urls.py
# def replace_url_query_values(url, replace_vals):
#     """Replace querystring values in a url string.
# 
#     >>> url = 'http://helloworld.com/some/path?test=5'
#     >>> replace_vals = {'test': 10}
#     >>> replace_url_query_values(url=url, replace_vals=replace_vals)
#     'http://helloworld.com/some/path?test=10'
#     """
#     if '?' not in url:
#         return url
# 
#     parsed_url = urlparse(url)
#     query = dict(parse_qsl(parsed_url.query))
#     query.update(replace_vals)
#     return '{0}?{1}'.format(url.split('?')[0], urlencode(query))
# 
# Path: django_core/utils/urls.py
# def is_legit_next_url(next_url):
# 
#     if not next_url:
#         return False
# 
#     is_fully_qualified_url = (next_url.startswith('http') or
#                               next_url.startswith('//'))
#     site_domain = get_setting('SITE_DOMAIN', default=None)
#     parsed_url = urlparse(next_url)
# 
#     try:
#         portless_netloc = parsed_url.netloc.split(':')[0]
#     except:
#         portless_netloc = None
# 
#     if is_fully_qualified_url:
#         # domain validaition required
# 
#         if not site_domain:
#             # can't validate without a SITE_DOMAIN setting
#             return False
# 
#         if portless_netloc and portless_netloc.endswith(site_domain):
#             # trusted domain
#             return True
# 
#         return False
# 
#     if next_url.startswith('/'):
#         # url relative to the current site
#         return True
# 
#     return False


# Path: tests/utils/test_urls.py
from django.test.testcases import TestCase
from django_core.utils.urls import get_query_values_from_url
from django_core.utils.urls import replace_url_query_values
from django_core.utils.urls import is_legit_next_url



class UrlUtilsTestCase(TestCase):
    """Test case for url utils."""

    def test_replace_url_query_values(self):
        """Test replacing querysting values in a url."""
        url = 'http://helloworld.com/some/path?test=5'
        replace_vals = {'test': 10}
        new_url = replace_url_query_values(url=url, replace_vals=replace_vals)
        self.assertEqual(new_url, 'http://helloworld.com/some/path?test=10')

    def test_get_query_values_from_url_single_key(self):
        """Test getting query values from a url."""
        url = 'http://helloworld.com/some/path?test=5&hello=world&john=doe'
        self.assertEqual("5", get_query_values_from_url(url=url, keys='test'))

    def test_get_query_values_from_url_single_key_list(self):
        """Test getting query values from a url."""
        url = 'http://helloworld.com/some/path?test=5&hello=world&john=doe'
        self.assertEqual(
            {'test': '5'},

            get_query_values_from_url(url=url, keys=['test'])