Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #767: display proper URL hostname when scheme is missing slashes #951

Merged
merged 4 commits into from
Mar 9, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions tests/test_form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''Tests for form validation.'''

import unittest
from webcompat import form


class TestForm(unittest.TestCase):

This comment was marked as abuse.


def test_normalize_url(self):

r = form.normalize_url('example.com')

This comment was marked as abuse.

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

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('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')

def test_domain_name(self):

r = form.domain_name("http://example.com")
self.assertEqual(r, "example.com")

r = form.domain_name("https://example.com")
self.assertEqual(r, "example.com")
20 changes: 18 additions & 2 deletions webcompat/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
AUTH_REPORT = 'github-auth-report'
PROXY_REPORT = 'github-proxy-report'
SCHEMES = ('http://', 'https://')
BAD_SCHEMES = ('http:/', 'https:/', 'http:', 'https:')

problem_choices = [
(u'detection_bug', u'Desktop site instead of mobile site'),
Expand Down Expand Up @@ -118,9 +119,24 @@ def get_labels(browser_name):
def normalize_url(url):
'''normalize URL for consistency.'''
url = url.strip()
if not url.startswith(SCHEMES):
parsed = urlparse.urlparse(url)

if url.startswith(BAD_SCHEMES):

This comment was marked as abuse.

# 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)

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

if parsed.query:
url += '?' + parsed.query
if parsed.fragment:

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

url += '#' + parsed.fragment
elif not parsed.scheme:
# We assume that http is missing not https
url = 'http://%s' % (url)
if url.startswith("//"):
url = "http://%s" % (url[2:])
else:
url = 'http://%s' % (url)

This comment was marked as abuse.

return url


Expand Down