Skip to content

Commit 619926a

Browse files
author
Mike Taylor
authored
Merge pull request #1998 from karlcow/1995/1
Fix #1995 - Adds proper unicode handling for Form data
2 parents 58d7215 + 0256790 commit 619926a

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

tests/test_form.py

+12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class TestForm(unittest.TestCase):
1616

1717
def setUp(self):
1818
"""Set up."""
19+
self.maxDiff = None
1920
webcompat.app.config['TESTING'] = True
2021
self.app = webcompat.app.test_client()
2122

@@ -28,6 +29,12 @@ def test_normalize_url(self):
2829
r = form.normalize_url('http://example.com')
2930
self.assertEqual(r, 'http://example.com')
3031

32+
r = form.normalize_url(u'愛')
33+
self.assertEqual(r, u'http://愛')
34+
35+
r = form.normalize_url(u'http://愛')
36+
self.assertEqual(r, u'http://愛')
37+
3138
r = form.normalize_url('https://example.com')
3239
self.assertEqual(r, 'https://example.com')
3340

@@ -136,3 +143,8 @@ def test_build_formdata(self):
136143
expected = {'body': u'<!-- @browser: None -->\n<!-- @ua_header: None -->\n<!-- @reported_with: None -->\n\n**URL**: None\n\n**Browser / Version**: None\n**Operating System**: None\n**Tested Another Browser**: Unknown\n\n**Problem type**: Unknown\n**Description**: None\n**Steps to Reproduce**:\nNone\n\n\n\n_From [webcompat.com](https://webcompat.com/) with \u2764\ufe0f_', 'title': 'None - unknown'} # nopep8
137144
self.assertIs(type(actual), dict)
138145
self.assertEqual(actual, expected)
146+
# testing with unicode strings.
147+
expected = {'body': u'<!-- @browser: None -->\n<!-- @ua_header: None -->\n<!-- @reported_with: None -->\n\n**URL**: \u611b\n\n**Browser / Version**: None\n**Operating System**: None\n**Tested Another Browser**: Unknown\n\n**Problem type**: Unknown\n**Description**: None\n**Steps to Reproduce**:\nNone\n\n\n\n_From [webcompat.com](https://webcompat.com/) with \u2764\ufe0f_', 'title': u'\u611b - unknown'} # nopep8
148+
form_object = {'url': u'愛'}
149+
actual = form.build_formdata(form_object)
150+
self.assertEqual(actual, expected)

webcompat/form.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,17 @@ def normalize_url(url):
145145
# if url starts with a bad scheme, parsed.netloc will be empty,
146146
# so we use parsed.path instead
147147
path = parsed.path.lstrip('/')
148-
url = '{}://{}'.format(parsed.scheme, path)
148+
url = u'{}://{}'.format(parsed.scheme, path)
149149
if parsed.query:
150150
url += '?' + parsed.query
151151
if parsed.fragment:
152152
url += '#' + parsed.fragment
153153
elif not parsed.scheme:
154154
# We assume that http is missing not https
155155
if url.startswith("//"):
156-
url = "http://{}".format(url[2:])
156+
url = u"http://{}".format(url[2:])
157157
else:
158-
url = 'http://{}'.format(url)
158+
url = u'http://{}'.format(url)
159159
return url
160160

161161

@@ -221,15 +221,16 @@ def build_formdata(form_object):
221221
NOTE: Add milestone "needstriage" when create new issue
222222
"""
223223
# Do domain extraction for adding to the summary/title
224+
# form_object always returns a unicode string
224225
url = form_object.get('url')
225226
normalized_url = normalize_url(url)
226227
domain = domain_name(normalized_url)
227228
problem_summary = get_problem_summary(form_object.get('problem_category'))
228229

229230
if domain:
230-
summary = '{0} - {1}'.format(domain, problem_summary)
231+
summary = u'{0} - {1}'.format(domain, problem_summary)
231232
else:
232-
summary = '{0} - {1}'.format(normalized_url, problem_summary)
233+
summary = u'{0} - {1}'.format(normalized_url, problem_summary)
233234

234235
metadata_keys = ['browser', 'ua_header', 'reported_with']
235236
extra_label = form_object.get('extra_label', None)
@@ -265,7 +266,7 @@ def build_formdata(form_object):
265266
""".format(**formdata)
266267
# Add the image, if there was one.
267268
if form_object.get('image_upload') is not None:
268-
body += '\n\n![Screenshot of the site issue]({image_url})'.format(
269+
body += u'\n\n![Screenshot of the site issue]({image_url})'.format(
269270
image_url=form_object.get('image_upload').get('url'))
270271
# Append "from webcompat.com" message to bottom (for GitHub issue viewers)
271272
body += u'\n\n{0}'.format(GITHUB_HELP)

0 commit comments

Comments
 (0)