def test_valid(self): test_data = [ # expected, url ('about:mozilla', 'about:mozilla'), ('chrome://foo', 'chrome://foo'), ('ftp://example.com/', 'ftp://example.com'), # From Django's URLField test data ('http://localhost/', 'http://localhost'), ('http://example.com/', 'http://example.com'), ('http://example.com./', 'http://example.com.'), ('http://www.example.com/', 'http://www.example.com'), ('http://www.example.com:8000/test', 'http://www.example.com:8000/test'), ('http://valid-with-hyphens.com/', 'valid-with-hyphens.com'), ('http://subdomain.domain.com/', 'subdomain.domain.com'), ('http://200.8.9.10/', 'http://200.8.9.10'), ('http://200.8.9.10:8000/test', 'http://200.8.9.10:8000/test'), ('http://valid-----hyphens.com/', 'http://valid-----hyphens.com'), ('http://www.example.com/s/http://code.djangoproject.com/tkt/13', 'www.example.com/s/http://code.djangoproject.com/tkt/13'), ] f = EnhancedURLField() for expected, url in test_data: try: eq_(f.clean(url), expected) except ValidationError: print url raise
def test_invalid(self): test_data = [ # From Django's URLField test data ('This field is required.', ''), ('This field is required.', None), ('Enter a valid URL.', 'foo'), ('Enter a valid URL.', 'http://'), ('Enter a valid URL.', 'http://example'), ('Enter a valid URL.', 'http://example.'), ('Enter a valid URL.', 'com.'), ('Enter a valid URL.', '.'), ('Enter a valid URL.', 'http://.com'), ('Enter a valid URL.', 'http://invalid-.com'), ('Enter a valid URL.', 'http://-invalid.com'), ('Enter a valid URL.', 'http://inv-.alid-.com'), ('Enter a valid URL.', 'http://inv-.-alid.com'), ('Enter a valid URL.', '[a'), ('Enter a valid URL.', 'http://[a'), ] f = EnhancedURLField() for msg, url in test_data: try: f.clean(url) except ValidationError as exc: eq_(exc.messages, [msg])
def test_invalid(self): test_data = [ # From Django's URLField test data ('This field is required.', ''), ('This field is required.', None), ('Enter a valid URL.', 'foo'), ('Enter a valid URL.', 'http://'), ('Enter a valid URL.', 'http://example'), ('Enter a valid URL.', 'http://example.'), ('Enter a valid URL.', 'com.'), ('Enter a valid URL.', '.'), ('Enter a valid URL.', 'http://.com'), ('Enter a valid URL.', 'http://invalid-.com'), ('Enter a valid URL.', 'http://-invalid.com'), ('Enter a valid URL.', 'http://inv-.alid-.com'), ('Enter a valid URL.', 'http://inv-.-alid.com'), ('Enter a valid URL.', '[a'), ('Enter a valid URL.', 'http://[a'), ] f = EnhancedURLField() for msg, url in test_data: try: f.clean(url) except ValidationError as exc: assert exc.messages == [msg]
def test_valid(self): test_data = [ # expected, url ('about:mozilla', 'about:mozilla'), ('chrome://foo', 'chrome://foo'), ('ftp://example.com', 'ftp://example.com'), # From Django's URLField test data ('http://localhost', 'http://localhost'), ('http://example.com', 'http://example.com'), ('http://example.com.', 'http://example.com.'), ('http://www.example.com', 'http://www.example.com'), ('http://www.example.com:8000/test', 'http://www.example.com:8000/test'), ('http://valid-with-hyphens.com', 'valid-with-hyphens.com'), ('http://subdomain.domain.com', 'subdomain.domain.com'), ('http://200.8.9.10', 'http://200.8.9.10'), ('http://200.8.9.10:8000/test', 'http://200.8.9.10:8000/test'), ('http://valid-----hyphens.com', 'http://valid-----hyphens.com'), ('http://www.example.com/s/http://code.djangoproject.com/tkt/13', 'www.example.com/s/http://code.djangoproject.com/tkt/13'), ] f = EnhancedURLField() for expected, url in test_data: try: assert f.clean(url) == expected except ValidationError: print url raise
class ResponseForm(forms.Form): """Basic response feedback form.""" # NB: The class 'url' is hard-coded in the Testpilot extension to # accommodate pre-filling the field client-side. url = EnhancedURLField(required=False, widget=forms.TextInput(attrs={ 'placeholder': 'http://', 'class': 'url' })) description = forms.CharField(widget=forms.Textarea(), required=True) # required=False means this allowed to be False, not that it can # be blank. happy = forms.BooleanField(required=False, widget=forms.HiddenInput()) email_ok = forms.BooleanField(required=False) email = forms.EmailField(required=False) browser_ok = forms.BooleanField(required=False) browser_data = forms.CharField(widget=forms.Textarea(), required=False) # These are hidden fields on the form which we have here so we can # abuse the fields for data validation. manufacturer = forms.CharField( required=False, widget=forms.HiddenInput(attrs={'class': 'manufacturer'})) device = forms.CharField( required=False, widget=forms.HiddenInput(attrs={'class': 'device'}))
class ResponseForm(forms.Form): """Basic response feedback form.""" # NB: The class 'url' is hard-coded in the Testpilot extension to # accommodate pre-filling the field client-side. url = EnhancedURLField(required=False, widget=forms.TextInput(attrs={ 'placeholder': 'http://', 'class': 'url' })) description = forms.CharField(widget=forms.Textarea(), required=True) # required=False means this allowed to be False, not that it can # be blank. happy = forms.BooleanField(required=False, widget=forms.HiddenInput()) email_ok = forms.BooleanField(required=False) email = forms.EmailField(required=False) browser_ok = forms.BooleanField(required=False) browser_data = forms.CharField(widget=forms.Textarea(), required=False) # These are hidden fields on the form which we have here so we can # abuse the fields for data validation. manufacturer = forms.CharField( required=False, widget=forms.HiddenInput(attrs={'class': 'manufacturer'})) device = forms.CharField( required=False, widget=forms.HiddenInput(attrs={'class': 'device'})) def clean_url(self): # Truncate the url if it's > URL_LENGTH characters. We truncate rather # than use it to validate because it probably doesn't matter if we keep # super long urls and we'd rather not error out on the user. data = self.cleaned_data['url'] data = data[:URL_LENGTH] return data