diff --git a/tests/regressiontests/forms/tests/error_messages.py b/tests/regressiontests/forms/tests/error_messages.py
index a9f4f37..3290d85 100644
a
|
b
|
from django.forms import *
|
4 | 4 | from django.test import TestCase |
5 | 5 | from django.utils.safestring import mark_safe |
6 | 6 | from django.utils import unittest |
| 7 | from regressiontests.forms.tests.fields import verify_exists_urls |
7 | 8 | |
8 | 9 | class AssertFormErrorsMixin(object): |
9 | 10 | def assertFormErrors(self, expected, the_callable, *args, **kwargs): |
… |
… |
class FormsErrorMessagesTestCase(unittest.TestCase, AssertFormErrorsMixin):
|
139 | 140 | self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', None)) |
140 | 141 | self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', '')) |
141 | 142 | |
| 143 | @verify_exists_urls() |
142 | 144 | def test_urlfield(self): |
143 | 145 | e = { |
144 | 146 | 'required': 'REQUIRED', |
diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
index 2303b4e..58c2b70 100644
a
|
b
|
import re
|
30 | 30 | import os |
31 | 31 | import urllib2 |
32 | 32 | from decimal import Decimal |
| 33 | from functools import wraps |
33 | 34 | |
34 | 35 | from django.core.files.uploadedfile import SimpleUploadedFile |
35 | 36 | from django.forms import * |
… |
… |
def fix_os_paths(x):
|
48 | 49 | return x |
49 | 50 | |
50 | 51 | |
| 52 | def verify_exists_urls(existing_urls=()): |
| 53 | def decorator(func): |
| 54 | @wraps(func) |
| 55 | def wrapper(*args, **kwargs): |
| 56 | from django.core import validators |
| 57 | # patch urllib2 |
| 58 | original_urlopen = validators.urllib2.urlopen |
| 59 | def urlopen(req): |
| 60 | url = req.get_full_url() |
| 61 | if url in existing_urls: |
| 62 | return True |
| 63 | raise Exception() |
| 64 | try: |
| 65 | urllib2.urlopen = urlopen |
| 66 | func(*args, **kwargs) |
| 67 | finally: |
| 68 | # unpatch urllib2 |
| 69 | validators.urllib2.urlopen = original_urlopen |
| 70 | return wrapper |
| 71 | return decorator |
| 72 | |
| 73 | |
51 | 74 | class FieldsTests(TestCase): |
52 | 75 | |
53 | 76 | def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs): |
… |
… |
class FieldsTests(TestCase):
|
592 | 615 | self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.') |
593 | 616 | self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com') |
594 | 617 | |
| 618 | @verify_exists_urls(('http://www.google.com/',)) |
595 | 619 | def test_urlfield_3(self): |
596 | 620 | f = URLField(verify_exists=True) |
597 | | self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection |
| 621 | self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) |
598 | 622 | self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example') |
599 | 623 | self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain |
600 | 624 | self.assertRaises(ValidationError, f.clean, 'http://qa-dev.w3.org/link-testsuite/http.php?code=405') # Method not allowed |
… |
… |
class FieldsTests(TestCase):
|
608 | 632 | except ValidationError, e: |
609 | 633 | self.assertEqual("[u'This URL appears to be a broken link.']", str(e)) |
610 | 634 | |
| 635 | @verify_exists_urls(('http://www.google.com/',)) |
611 | 636 | def test_urlfield_4(self): |
612 | 637 | f = URLField(verify_exists=True, required=False) |
613 | 638 | self.assertEqual(u'', f.clean('')) |
614 | | self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection |
| 639 | self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) |
615 | 640 | |
616 | 641 | def test_urlfield_5(self): |
617 | 642 | f = URLField(min_length=15, max_length=20) |
… |
… |
class FieldsTests(TestCase):
|
660 | 685 | except ValidationError, e: |
661 | 686 | self.assertEqual("[u'This URL appears to be a broken link.']", str(e)) |
662 | 687 | |
| 688 | @verify_exists_urls(('http://xn--tr-xka.djangoproject.com/',)) |
663 | 689 | def test_urlfield_10(self): |
664 | | # UTF-8 char in path, enclosed by a monkey-patch to make sure |
665 | | # the encoding is passed to urllib2.urlopen |
666 | 690 | f = URLField(verify_exists=True) |
667 | | try: |
668 | | _orig_urlopen = urllib2.urlopen |
669 | | urllib2.urlopen = lambda req: True |
670 | | url = u'http://t\xfcr.djangoproject.com/' |
671 | | self.assertEqual(url, f.clean(url)) |
672 | | finally: |
673 | | urllib2.urlopen = _orig_urlopen |
| 691 | url = u'http://t\xfcr.djangoproject.com/' |
| 692 | self.assertEqual(url, f.clean(url)) |
674 | 693 | |
675 | 694 | # BooleanField ################################################################ |
676 | 695 | |