Ticket #16816: 16816.verify-exists-test-mock.diff

File 16816.verify-exists-test-mock.diff, 2.0 KB (added by Julien Phalip, 13 years ago)
  • tests/regressiontests/forms/tests/fields.py

    diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
    index d37ad5e..f815c77 100644
    a b def verify_exists_urls(existing_urls=()):  
    5353        @wraps(func)
    5454        def wrapper(*args, **kwargs):
    5555            from django.core import validators
    56             # patch urllib2
    57             original_urlopen = validators.urllib2.urlopen
    58             def urlopen(req):
    59                 url = req.get_full_url()
    60                 if url in existing_urls:
     56            # patch urllib2.OpenerDirector
     57            original_open = validators.urllib2.OpenerDirector.open
     58            def custom_open(self, req, data=None):
     59                if req.get_full_url() in existing_urls:
    6160                    return True
    6261                raise Exception()
    6362            try:
    64                 urllib2.urlopen = urlopen
     63                urllib2.OpenerDirector.open = custom_open
    6564                func(*args, **kwargs)
    6665            finally:
    67                 # unpatch urllib2
    68                 validators.urllib2.urlopen = original_urlopen
     66                # unpatch urllib2.OpenerDirector
     67                validators.urllib2.OpenerDirector.open = original_open
    6968        return wrapper
    7069    return decorator
    7170
    class FieldsTests(SimpleTestCase):  
    690689        except ValidationError, e:
    691690            self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
    692691
     692    @verify_exists_urls((u'http://xn--hxargifdar.idn.icann.org/%CE%91%CF%81%CF%87%CE%B9%CE%BA%CE%AE_%CF%83%CE%B5%CE%BB%CE%AF%CE%B4%CE%B1',))
    693693    def test_urlfield_10(self):
    694         # UTF-8 in the domain. 
     694        # UTF-8 in the domain.
    695695        f = URLField(verify_exists=True)
    696696        url = u'http://\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac.idn.icann.org/\u0391\u03c1\u03c7\u03b9\u03ba\u03ae_\u03c3\u03b5\u03bb\u03af\u03b4\u03b1'
    697697        self.assertEqual(url, f.clean(url))
Back to Top