Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions mongoengine/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ def validate(self, value):
# Check first if the scheme is valid
scheme = value.split('://')[0].lower()
if scheme not in self.schemes:
self.error('Invalid scheme {} in URL: {}'.format(scheme, value))
self.error(u'Invalid scheme {} in URL: {}'.format(scheme, value))
return

# Then check full URL
if not self.url_regex.match(value):
self.error('Invalid URL: {}'.format(value))
self.error(u'Invalid URL: {}'.format(value))
return


Expand Down
26 changes: 24 additions & 2 deletions tests/fields/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,40 @@ class Person(Document):
person.validate()

def test_url_validation(self):
"""Ensure that URLFields validate urls properly.
"""
"""Ensure that URLFields validate urls properly."""
class Link(Document):
url = URLField()

Link.drop_collection()

link = Link()
link.url = 'google'
self.assertRaises(ValidationError, link.validate)

link.url = 'http://www.google.com:8080'
link.validate()

def test_unicode_url_validation(self):
"""Ensure unicode URLs are validated properly."""
class Link(Document):
url = URLField()

Link.drop_collection()

link = Link()
link.url = u'http://привет.com'

# TODO fix URL validation - this *IS* a valid URL
# For now we just want to make sure that the error message is correct
try:
link.validate()
self.assertTrue(False)
except ValidationError as e:
self.assertEqual(
unicode(e),
u"ValidationError (Link:None) (Invalid URL: http://\u043f\u0440\u0438\u0432\u0435\u0442.com: ['url'])"
)

def test_url_scheme_validation(self):
"""Ensure that URLFields validate urls with specific schemes properly.
"""
Expand Down