| 1 |
from urllib.parse import urljoin |
| 2 |
|
| 3 |
from django.conf import STATICFILES_STORAGE_ALIAS |
| 4 |
from django.contrib.staticfiles import storage |
| 5 |
from django.forms import Media |
| 6 |
from django.templatetags.static import static |
| 7 |
from django.test import SimpleTestCase, override_settings |
| 8 |
|
| 9 |
|
| 10 |
class StaticTestStorage(storage.StaticFilesStorage): |
| 11 |
def url(self, name): |
| 12 |
return urljoin("https://example.com/assets/", name) |
| 13 |
|
| 14 |
|
| 15 |
@override_settings( |
| 16 |
INSTALLED_APPS=("django.contrib.staticfiles",), |
| 17 |
STORAGES={ |
| 18 |
STATICFILES_STORAGE_ALIAS: { |
| 19 |
"BACKEND": "staticfiles_tests.test_forms.StaticTestStorage", |
| 20 |
"OPTIONS": {"location": "http://media.example.com/static/"}, |
| 21 |
} |
| 22 |
}, |
| 23 |
) |
| 24 |
class StaticFilesFormsMediaTestCase(SimpleTestCase): |
| 25 |
def test_absolute_url(self): |
| 26 |
m = Media( |
| 27 |
css={"all": ("path/to/css1", "/path/to/css2")}, |
| 28 |
js=( |
| 29 |
"/path/to/js1", |
| 30 |
"http://media.other.com/path/to/js2", |
| 31 |
"https://secure.other.com/path/to/js3", |
| 32 |
static("relative/path/to/js4"), |
| 33 |
), |
| 34 |
) |
| 35 |
self.assertEqual( |
| 36 |
str(m), |
| 37 |
'<link href="https://example.com/assets/path/to/css1" media="all" ' |
| 38 |
'rel="stylesheet">\n' |
| 39 |
'<link href="/path/to/css2" media="all" rel="stylesheet">\n' |
| 40 |
'<script src="/path/to/js1"></script>\n' |
| 41 |
'<script src="http://media.other.com/path/to/js2"></script>\n' |
| 42 |
'<script src="https://secure.other.com/path/to/js3"></script>\n' |
| 43 |
'<script src="https://example.com/assets/relative/path/to/js4"></script>', |
| 44 |
) |