Skip to content

Commit

Permalink
Issue webcompat#767: normalize_url doesn't break when scheme is prope…
Browse files Browse the repository at this point in the history
…rly spelled
  • Loading branch information
daliacoss committed Mar 8, 2016
1 parent 6801fe8 commit 54a7d0a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 6 additions & 0 deletions tests/test_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class TestForm(unittest.TestCase):

def test_normalize_url(self):

r = form.normalize_url('http://example.com')
self.assertEqual(r, 'http://example.com')

r = form.normalize_url('https://example.com')
self.assertEqual(r, 'https://example.com')

r = form.normalize_url('example.com')
self.assertEqual(r, 'http://example.com')

Expand Down
9 changes: 4 additions & 5 deletions webcompat/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,21 @@ def normalize_url(url):
url = url.strip()
parsed = urlparse.urlparse(url)

if url.startswith(BAD_SCHEMES):
if url.startswith(BAD_SCHEMES) and not url.startswith(SCHEMES):
# if url starts with a bad scheme, parsed.netloc will be empty,
# so we use parsed.path instead
path = parsed.path.lstrip('/')
url = '%s://%s' % (parsed.scheme, path)
url = '{}://{}'.format(parsed.scheme, path)
if parsed.query:
url += '?' + parsed.query
if parsed.fragment:
url += '#' + parsed.fragment
elif not parsed.scheme:
# We assume that http is missing not https
if url.startswith("//"):
url = "http://%s" % (url[2:])
url = "http://{}".format(url[2:])
else:
url = 'http://%s' % (url)

url = 'http://{}'.format(url)
return url


Expand Down

0 comments on commit 54a7d0a

Please sign in to comment.