Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #6189 -- Modified test that need Internet access so they use a …

…mock instead. Thanks Gregor Müellegger for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16451 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 25c5251acd79293c8cbaef393ceb1db388262190 1 parent 2d6dec2
@ramiro ramiro authored
View
2  tests/regressiontests/forms/tests/error_messages.py
@@ -4,6 +4,7 @@
from django.test import TestCase
from django.utils.safestring import mark_safe
from django.utils import unittest
+from regressiontests.forms.tests.fields import verify_exists_urls
class AssertFormErrorsMixin(object):
def assertFormErrors(self, expected, the_callable, *args, **kwargs):
@@ -139,6 +140,7 @@ def test_filefield(self):
self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', None))
self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', ''))
+ @verify_exists_urls()
def test_urlfield(self):
e = {
'required': 'REQUIRED',
View
43 tests/regressiontests/forms/tests/fields.py
@@ -25,11 +25,11 @@
__init__(). For example, CharField has a max_length option.
"""
import datetime
-import time
import re
import os
import urllib2
from decimal import Decimal
+from functools import wraps
from django.core.files.uploadedfile import SimpleUploadedFile
from django.forms import *
@@ -48,6 +48,28 @@ def fix_os_paths(x):
return x
+def verify_exists_urls(existing_urls=()):
+ def decorator(func):
+ @wraps(func)
+ def wrapper(*args, **kwargs):
+ from django.core import validators
+ # patch urllib2
+ original_urlopen = validators.urllib2.urlopen
+ def urlopen(req):
+ url = req.get_full_url()
+ if url in existing_urls:
+ return True
+ raise Exception()
+ try:
+ urllib2.urlopen = urlopen
+ func(*args, **kwargs)
+ finally:
+ # unpatch urllib2
+ validators.urllib2.urlopen = original_urlopen
+ return wrapper
+ return decorator
+
+
class FieldsTests(TestCase):
def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs):
@@ -595,9 +617,10 @@ def test_urlfield_2(self):
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.')
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com')
+ @verify_exists_urls(('http://www.google.com/',))
def test_urlfield_3(self):
f = URLField(verify_exists=True)
- self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection
+ self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com'))
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')
self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain
self.assertRaises(ValidationError, f.clean, 'http://qa-dev.w3.org/link-testsuite/http.php?code=405') # Method not allowed
@@ -611,10 +634,11 @@ def test_urlfield_3(self):
except ValidationError, e:
self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
+ @verify_exists_urls(('http://www.google.com/',))
def test_urlfield_4(self):
f = URLField(verify_exists=True, required=False)
self.assertEqual(u'', f.clean(''))
- self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection
+ self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com'))
def test_urlfield_5(self):
f = URLField(min_length=15, max_length=20)
@@ -663,17 +687,12 @@ def test_urlfield_9(self):
except ValidationError, e:
self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
+ @verify_exists_urls(('http://xn--tr-xka.djangoproject.com/',))
def test_urlfield_10(self):
- # UTF-8 char in path, enclosed by a monkey-patch to make sure
- # the encoding is passed to urllib2.urlopen
+ # UTF-8 char in path
f = URLField(verify_exists=True)
- try:
- _orig_urlopen = urllib2.urlopen
- urllib2.urlopen = lambda req: True
- url = u'http://t\xfcr.djangoproject.com/'
- self.assertEqual(url, f.clean(url))
- finally:
- urllib2.urlopen = _orig_urlopen
+ url = u'http://t\xfcr.djangoproject.com/'
+ self.assertEqual(url, f.clean(url))
# BooleanField ################################################################
Please sign in to comment.
Something went wrong with that request. Please try again.