Fixed #22114 -- Stopped adding trailing slashes in URLField.to_python

Thanks coredumperror at gmail.com for the report and Tim Graham
for the review.
This commit is contained in:
Claude Paroz 2014-03-26 18:10:58 +01:00
parent b9e50e4774
commit d320863878
3 changed files with 19 additions and 25 deletions

View File

@ -704,9 +704,6 @@ class URLField(CharField):
# Rebuild the url_fields list, since the domain segment may now # Rebuild the url_fields list, since the domain segment may now
# contain the path too. # contain the path too.
url_fields = split_url(urlunsplit(url_fields)) url_fields = split_url(urlunsplit(url_fields))
if not url_fields[2]:
# the path portion may need to be added before query params
url_fields[2] = '/'
value = urlunsplit(url_fields) value = urlunsplit(url_fields)
return value return value

View File

@ -173,7 +173,7 @@ Backwards incompatible changes in 1.8
Miscellaneous Miscellaneous
~~~~~~~~~~~~~ ~~~~~~~~~~~~~
* ... * ``URLField.to_python`` no longer adds a trailing slash to pathless URLs.
.. _deprecated-features-1.8: .. _deprecated-features-1.8:

View File

@ -748,14 +748,14 @@ class FieldsTests(SimpleTestCase):
self.assertWidgetRendersTo(f, '<input type="url" name="f" id="id_f" />') self.assertWidgetRendersTo(f, '<input type="url" name="f" id="id_f" />')
self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, '') self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, '')
self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None) self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None)
self.assertEqual('http://localhost/', f.clean('http://localhost')) self.assertEqual('http://localhost', f.clean('http://localhost'))
self.assertEqual('http://example.com/', f.clean('http://example.com')) self.assertEqual('http://example.com', f.clean('http://example.com'))
self.assertEqual('http://example.com./', f.clean('http://example.com.')) self.assertEqual('http://example.com.', f.clean('http://example.com.'))
self.assertEqual('http://www.example.com/', f.clean('http://www.example.com')) self.assertEqual('http://www.example.com', f.clean('http://www.example.com'))
self.assertEqual('http://www.example.com:8000/test', f.clean('http://www.example.com:8000/test')) self.assertEqual('http://www.example.com:8000/test', f.clean('http://www.example.com:8000/test'))
self.assertEqual('http://valid-with-hyphens.com/', f.clean('valid-with-hyphens.com')) self.assertEqual('http://valid-with-hyphens.com', f.clean('valid-with-hyphens.com'))
self.assertEqual('http://subdomain.domain.com/', f.clean('subdomain.domain.com')) self.assertEqual('http://subdomain.domain.com', f.clean('subdomain.domain.com'))
self.assertEqual('http://200.8.9.10/', f.clean('http://200.8.9.10')) self.assertEqual('http://200.8.9.10', f.clean('http://200.8.9.10'))
self.assertEqual('http://200.8.9.10:8000/test', f.clean('http://200.8.9.10:8000/test')) self.assertEqual('http://200.8.9.10:8000/test', f.clean('http://200.8.9.10:8000/test'))
self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'foo') self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'foo')
self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'http://') self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'http://')
@ -768,7 +768,7 @@ class FieldsTests(SimpleTestCase):
self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'http://-invalid.com') self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'http://-invalid.com')
self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'http://inv-.alid-.com') self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'http://inv-.alid-.com')
self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'http://inv-.-alid.com') self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'http://inv-.-alid.com')
self.assertEqual('http://valid-----hyphens.com/', f.clean('http://valid-----hyphens.com')) self.assertEqual('http://valid-----hyphens.com', f.clean('http://valid-----hyphens.com'))
self.assertEqual('http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah', f.clean('http://some.idn.xyzäöüßabc.domain.com:123/blah')) self.assertEqual('http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah', f.clean('http://some.idn.xyzäöüßabc.domain.com:123/blah'))
self.assertEqual('http://www.example.com/s/http://code.djangoproject.com/ticket/13804', f.clean('www.example.com/s/http://code.djangoproject.com/ticket/13804')) self.assertEqual('http://www.example.com/s/http://code.djangoproject.com/ticket/13804', f.clean('www.example.com/s/http://code.djangoproject.com/ticket/13804'))
self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, '[a') self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, '[a')
@ -787,8 +787,8 @@ class FieldsTests(SimpleTestCase):
f = URLField(required=False) f = URLField(required=False)
self.assertEqual('', f.clean('')) self.assertEqual('', f.clean(''))
self.assertEqual('', f.clean(None)) self.assertEqual('', f.clean(None))
self.assertEqual('http://example.com/', f.clean('http://example.com')) self.assertEqual('http://example.com', f.clean('http://example.com'))
self.assertEqual('http://www.example.com/', f.clean('http://www.example.com')) self.assertEqual('http://www.example.com', f.clean('http://www.example.com'))
self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'foo') self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'foo')
self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'http://') self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'http://')
self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'http://example') self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 'http://example')
@ -798,25 +798,22 @@ class FieldsTests(SimpleTestCase):
def test_urlfield_5(self): def test_urlfield_5(self):
f = URLField(min_length=15, max_length=20) f = URLField(min_length=15, max_length=20)
self.assertWidgetRendersTo(f, '<input id="id_f" type="url" name="f" maxlength="20" />') self.assertWidgetRendersTo(f, '<input id="id_f" type="url" name="f" maxlength="20" />')
self.assertRaisesMessage(ValidationError, "'Ensure this value has at least 15 characters (it has 13).'", f.clean, 'http://f.com') self.assertRaisesMessage(ValidationError, "'Ensure this value has at least 15 characters (it has 12).'", f.clean, 'http://f.com')
self.assertEqual('http://example.com/', f.clean('http://example.com')) self.assertEqual('http://example.com', f.clean('http://example.com'))
self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 20 characters (it has 38).'", f.clean, 'http://abcdefghijklmnopqrstuvwxyz.com') self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 20 characters (it has 37).'", f.clean, 'http://abcdefghijklmnopqrstuvwxyz.com')
def test_urlfield_6(self): def test_urlfield_6(self):
f = URLField(required=False) f = URLField(required=False)
self.assertEqual('http://example.com/', f.clean('example.com')) self.assertEqual('http://example.com', f.clean('example.com'))
self.assertEqual('', f.clean('')) self.assertEqual('', f.clean(''))
self.assertEqual('https://example.com/', f.clean('https://example.com')) self.assertEqual('https://example.com', f.clean('https://example.com'))
def test_urlfield_7(self): def test_urlfield_7(self):
f = URLField() f = URLField()
self.assertEqual('http://example.com/', f.clean('http://example.com')) self.assertEqual('http://example.com', f.clean('http://example.com'))
self.assertEqual('http://example.com/test', f.clean('http://example.com/test')) self.assertEqual('http://example.com/test', f.clean('http://example.com/test'))
self.assertEqual('http://example.com?some_param=some_value',
def test_urlfield_8(self): f.clean('http://example.com?some_param=some_value'))
# ticket #11826
f = URLField()
self.assertEqual('http://example.com/?some_param=some_value', f.clean('http://example.com?some_param=some_value'))
def test_urlfield_9(self): def test_urlfield_9(self):
f = URLField() f = URLField()