|
2 | 2 | from __future__ import unicode_literals
|
3 | 3 |
|
4 | 4 | from datetime import datetime, timedelta
|
| 5 | +import io |
| 6 | +import os |
5 | 7 | import re
|
6 | 8 | import types
|
7 | 9 | from unittest import TestCase
|
|
16 | 18 | )
|
17 | 19 | from django.test import SimpleTestCase
|
18 | 20 | from django.test.utils import str_prefix
|
| 21 | +from django.utils._os import upath |
19 | 22 |
|
20 | 23 |
|
21 | 24 | NOW = datetime.now()
|
22 | 25 | EXTENDED_SCHEMES = ['http', 'https', 'ftp', 'ftps', 'git', 'file']
|
23 | 26 |
|
24 |
| -TEST_DATA = ( |
| 27 | +TEST_DATA = [ |
25 | 28 | # (validator, value, expected),
|
26 | 29 | (validate_integer, '42', None),
|
27 | 30 | (validate_integer, '-42', None),
|
|
153 | 156 |
|
154 | 157 | (MinLengthValidator(10), '', ValidationError),
|
155 | 158 |
|
156 |
| - (URLValidator(), 'http://www.djangoproject.com/', None), |
157 |
| - (URLValidator(), 'HTTP://WWW.DJANGOPROJECT.COM/', None), |
158 |
| - (URLValidator(), 'http://localhost/', None), |
159 |
| - (URLValidator(), 'http://example.com/', None), |
160 |
| - (URLValidator(), 'http://www.example.com/', None), |
161 |
| - (URLValidator(), 'http://www.example.com:8000/test', None), |
162 |
| - (URLValidator(), 'http://valid-with-hyphens.com/', None), |
163 |
| - (URLValidator(), 'http://subdomain.example.com/', None), |
164 |
| - (URLValidator(), 'http://200.8.9.10/', None), |
165 |
| - (URLValidator(), 'http://200.8.9.10:8000/test', None), |
166 |
| - (URLValidator(), 'http://valid-----hyphens.com/', None), |
167 |
| - (URLValidator(), 'http://example.com?something=value', None), |
168 |
| - (URLValidator(), 'http://example.com/index.php?something=value&another=value2', None), |
169 |
| - (URLValidator(), 'https://example.com/', None), |
170 |
| - (URLValidator(), 'ftp://example.com/', None), |
171 |
| - (URLValidator(), 'ftps://example.com/', None), |
172 | 159 | (URLValidator(EXTENDED_SCHEMES), 'file://localhost/path', None),
|
173 | 160 | (URLValidator(EXTENDED_SCHEMES), 'git://example.com/', None),
|
174 | 161 |
|
175 |
| - (URLValidator(), 'foo', ValidationError), |
176 |
| - (URLValidator(), 'http://', ValidationError), |
177 |
| - (URLValidator(), 'http://example', ValidationError), |
178 |
| - (URLValidator(), 'http://example.', ValidationError), |
179 |
| - (URLValidator(), 'http://.com', ValidationError), |
180 |
| - (URLValidator(), 'http://invalid-.com', ValidationError), |
181 |
| - (URLValidator(), 'http://-invalid.com', ValidationError), |
182 |
| - (URLValidator(), 'http://invalid.com-', ValidationError), |
183 |
| - (URLValidator(), 'http://inv-.alid-.com', ValidationError), |
184 |
| - (URLValidator(), 'http://inv-.-alid.com', ValidationError), |
185 |
| - (URLValidator(), 'file://localhost/path', ValidationError), |
186 |
| - (URLValidator(), 'git://example.com/', ValidationError), |
187 | 162 | (URLValidator(EXTENDED_SCHEMES), 'git://-invalid.com', ValidationError),
|
188 | 163 |
|
189 | 164 | (BaseValidator(True), True, None),
|
|
208 | 183 | (RegexValidator('x', flags=re.IGNORECASE), 'y', ValidationError),
|
209 | 184 | (RegexValidator('a'), 'A', ValidationError),
|
210 | 185 | (RegexValidator('a', flags=re.IGNORECASE), 'A', None),
|
211 |
| -) |
| 186 | +] |
| 187 | + |
| 188 | + |
| 189 | +def create_path(filename): |
| 190 | + return os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), filename)) |
| 191 | + |
| 192 | +# Add valid and invalid URL tests. |
| 193 | +# This only tests the validator without extended schemes. |
| 194 | +with io.open(create_path('valid_urls.txt'), encoding='utf8') as f: |
| 195 | + for url in f: |
| 196 | + TEST_DATA.append((URLValidator(), url.strip(), None)) |
| 197 | +with io.open(create_path('invalid_urls.txt'), encoding='utf8') as f: |
| 198 | + for url in f: |
| 199 | + TEST_DATA.append((URLValidator(), url.strip(), ValidationError)) |
212 | 200 |
|
213 | 201 |
|
214 | 202 | def create_simple_test_method(validator, expected, value, num):
|
|
0 commit comments