forked from nickjj/flask-static-digest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: takes into account when statics not in host_url root
Refs: nickjj#21
- Loading branch information
Showing
5 changed files
with
161 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,3 +81,6 @@ coverage.xml | |
*.pot | ||
|
||
# End of https://www.gitignore.io/api/python,osx | ||
|
||
env/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from flask_static_digest import parse_digest_host_url | ||
|
||
import pytest as pt | ||
|
||
|
||
@pt.mark.parametrize( | ||
"digest_host_url, expected", [ | ||
( | ||
"https://cdn.example.com", | ||
("https://cdn.example.com", "") | ||
), | ||
( | ||
"https://cdn.example.com/", | ||
("https://cdn.example.com", "") | ||
), | ||
( | ||
"https://cdn.example.com/myapp", | ||
("https://cdn.example.com", "myapp") | ||
), | ||
( | ||
"https://cdn.example.com/myapp/", | ||
("https://cdn.example.com", "myapp") | ||
), | ||
( | ||
"https://cdn.example.com/myapp/anotherdir", | ||
("https://cdn.example.com", "myapp/anotherdir") | ||
), | ||
( | ||
"https://cdn.example.com/myapp/anotherdir/", | ||
("https://cdn.example.com", "myapp/anotherdir") | ||
), | ||
] | ||
) | ||
def test_parse_function(digest_host_url, expected): | ||
assert parse_digest_host_url(digest_host_url) == expected | ||
|
||
|
||
def test_joined_urls(): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from flask import Flask | ||
from flask_static_digest import FlaskStaticDigest | ||
import pytest as pt | ||
import json | ||
|
||
|
||
fake_manifest = { | ||
"hey.png": "hey-HASH.png", | ||
"images/hey.png": "images/hey-HASH.png", | ||
"images/card/hey.png": "images/card/hey-HASH.png" | ||
} | ||
|
||
|
||
def make_app(tmp_path, host_url, static_url_path): | ||
appdir = tmp_path / 'fakeappdir' | ||
appdir.mkdir() | ||
|
||
staticdir = appdir / 'static' | ||
staticdir.mkdir() | ||
|
||
manifest_f = staticdir / 'cache_manifest.json' | ||
manifest_f.write_text(json.dumps(fake_manifest)) | ||
|
||
app = Flask( | ||
__name__, root_path=appdir, static_folder=staticdir, | ||
static_url_path=static_url_path) | ||
app.config['TESTING'] = True | ||
app.config['FLASK_STATIC_DIGEST_HOST_URL'] = host_url | ||
|
||
with app.app_context(): | ||
return app | ||
|
||
|
||
@pt.mark.parametrize( | ||
"host_url, static_url_path, file, expected", [ | ||
( | ||
"https://cdn.example.com", None, "hey.png", | ||
"https://cdn.example.com/static/hey-HASH.png" | ||
), | ||
( | ||
"https://cdn.example.com", None, "images/hey.png", | ||
"https://cdn.example.com/static/images/hey-HASH.png" | ||
), | ||
( | ||
"https://cdn.example.com", "/static/something/", "hey.png", | ||
"https://cdn.example.com/static/something/hey-HASH.png" | ||
), | ||
( | ||
pt.param( | ||
"https://cdn.example.com", "static/something/", "hey.png", | ||
"https://cdn.example.com/static/something/hey-HASH.png", | ||
marks=pt.mark.xfail(raises=ValueError) | ||
) | ||
), | ||
( | ||
"https://cdn.example.com/myapp", None, "images/hey.png", | ||
"https://cdn.example.com/myapp/static/images/hey-HASH.png" | ||
), | ||
( | ||
"https://cdn.example.com/myapp", '/static', "images/hey.png", | ||
"https://cdn.example.com/myapp/static/images/hey-HASH.png" | ||
), | ||
( | ||
"https://cdn.example.com/myapp", "/static/something/", "hey.png", | ||
"https://cdn.example.com/myapp/static/something/hey-HASH.png" | ||
), | ||
( | ||
"https://cdn.example.com/myapp/anotherdir", None, "images/hey.png", | ||
"https://cdn.example.com/myapp/anotherdir" | ||
"/static/images/hey-HASH.png" | ||
), | ||
(None, None, "hey.png", "/static/hey-HASH.png"), | ||
(None, '/mystatic/url', "hey.png", "/mystatic/url/hey-HASH.png"), | ||
] | ||
) | ||
def test_get_url(tmp_path, host_url, static_url_path, file, expected): | ||
app = make_app(tmp_path, host_url, static_url_path) | ||
ext = FlaskStaticDigest(app=app) | ||
|
||
ret = ext.static_url_for('static', filename=file) | ||
|
||
assert ret == expected |