2017-01-07 19:11:46 +08:00
|
|
|
from urllib.parse import urljoin
|
|
|
|
|
2015-11-07 19:24:38 +08:00
|
|
|
from django.contrib.staticfiles import storage
|
|
|
|
from django.forms import Media
|
2018-01-22 05:38:52 +08:00
|
|
|
from django.templatetags.static import static
|
2015-11-07 19:24:38 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
|
|
|
|
|
|
|
|
|
|
|
class StaticTestStorage(storage.StaticFilesStorage):
|
|
|
|
def url(self, name):
|
2022-02-04 03:24:19 +08:00
|
|
|
return urljoin("https://example.com/assets/", name)
|
2015-11-07 19:24:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
@override_settings(
|
2022-02-04 03:24:19 +08:00
|
|
|
STATIC_URL="http://media.example.com/static/",
|
|
|
|
INSTALLED_APPS=("django.contrib.staticfiles",),
|
|
|
|
STATICFILES_STORAGE="staticfiles_tests.test_forms.StaticTestStorage",
|
2015-11-07 19:24:38 +08:00
|
|
|
)
|
|
|
|
class StaticFilesFormsMediaTestCase(SimpleTestCase):
|
|
|
|
def test_absolute_url(self):
|
|
|
|
m = Media(
|
2022-02-04 03:24:19 +08:00
|
|
|
css={"all": ("path/to/css1", "/path/to/css2")},
|
2016-01-15 01:16:42 +08:00
|
|
|
js=(
|
2022-02-04 03:24:19 +08:00
|
|
|
"/path/to/js1",
|
|
|
|
"http://media.other.com/path/to/js2",
|
|
|
|
"https://secure.other.com/path/to/js3",
|
|
|
|
static("relative/path/to/js4"),
|
2016-01-15 01:16:42 +08:00
|
|
|
),
|
2015-11-07 19:24:38 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
str(m),
|
2022-01-21 22:46:37 +08:00
|
|
|
"""<link href="https://example.com/assets/path/to/css1" media="all" rel="stylesheet">
|
|
|
|
<link href="/path/to/css2" media="all" rel="stylesheet">
|
2019-12-11 16:49:54 +08:00
|
|
|
<script src="/path/to/js1"></script>
|
|
|
|
<script src="http://media.other.com/path/to/js2"></script>
|
|
|
|
<script src="https://secure.other.com/path/to/js3"></script>
|
2022-02-04 03:24:19 +08:00
|
|
|
<script src="https://example.com/assets/relative/path/to/js4"></script>""",
|
2015-11-07 19:24:38 +08:00
|
|
|
)
|