Skip to content

Commit

Permalink
test: add go optional parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
mrf345 committed Oct 22, 2023
1 parent e0c8827 commit 7dd0df1
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 24 deletions.
1 change: 1 addition & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pytest-runner
pytest-cov
coverage
bandit
tdewolff-minify
isort==5.10.1
black==22.3.0
importlib_metadata==4.8.3
33 changes: 29 additions & 4 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
}
</style>"""

FALSE_JS = """<script>
let body = (
color: 'red';;
}
</script>"""

JS_RAW = """
console.warn( [
'testing',
Expand Down Expand Up @@ -74,7 +80,7 @@
"<html>",
JS,
'<script type="text/script" src="testing/88.js"></script>',
LESS,
f"<style>{CSS_EDGE_CASES}</style>",
'<script type="application/script" src="testing/1.js"></script>',
JS,
'<script src="testing/nested/2.js"></script>',
Expand All @@ -83,6 +89,8 @@
)

MINIFED_HTML = b"<html><body><h1> HTML </h1></body></html>"
MINIFIED_HTML_GO = b"<html><body><h1>HTML</h1></body></html>"


MINIFIED_JS = b'<script>["J","S"].reduce(function(a,r){return a+r})</script>'

Expand All @@ -91,9 +99,11 @@
b"reduce(function(a,r){return a+r})</script>"
)

MINIFED_LESS = b"<style>body{color:red;}</style>"
MINIFIED_LESS = b"<style>body{color:red;}</style>"

MINIFIED_JS_RAW = b"console.warn(['testing',' suite']);"
MINIFIED_JS_RAW_GO = b'console.warn(["testing"," suite"])'


MINIFIED_LESS_RAW = b"body{color:red;}"

Expand All @@ -104,13 +114,28 @@
"background-color:var(--main-bg-color)}#some-id{grid-area:1 / 2 / 2 / 3}"
)

MINIFED_HTML_EMBEDDED_TAGS = bytes(
MINIFIED_CSS_EDGE_CASES_GO = ":root{--main-bg-color:brown}.some-class{color:#caf2ff;background-color:var(--main-bg-color)}#some-id{grid-area:1/2/2/3}"

MINIFIED_HTML_EMBEDDED_TAGS = bytes(
"".join(
[
"<html>",
MINIFIED_JS.decode("utf-8"),
'<script type="text/script" src="testing/88.js"></script>',
f"<style>{MINIFIED_CSS_EDGE_CASES}</style>",
'<script type="application/script" src="testing/1.js"></script>',
MINIFIED_JS.decode("utf-8"),
'<script src="testing/nested/2.js"></script></html>',
]
).encode("utf-8")
)
MINIFIED_HTML_EMBEDDED_TAGS_GO = bytes(
"".join(
[
"<html>",
MINIFIED_JS.decode("utf-8"),
'<script type="text/script" src="testing/88.js"></script>',
MINIFED_LESS.decode("utf-8"),
f"<style>{MINIFIED_CSS_EDGE_CASES_GO}</style>",
'<script type="application/script" src="testing/1.js"></script>',
MINIFIED_JS.decode("utf-8"),
'<script src="testing/nested/2.js"></script></html>',
Expand Down
39 changes: 19 additions & 20 deletions tests/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
JS_WITH_TYPE,
LESS,
LESS_RAW,
MINIFED_HTML,
MINIFED_HTML_EMBEDDED_TAGS,
MINIFED_LESS,
MINIFIED_HTML,
MINIFIED_HTML_EMBEDDED_TAGS,
MINIFIED_LESS,
MINIFIED_JS,
MINIFIED_JS_RAW,
MINIFIED_JS_WITH_TYPE,
MINIFIED_LESS_RAW,
)
from .setup import app, html_decorated, store_minify
from .setup import create_app


app, store_minify = create_app(go=False)


@fixture
Expand All @@ -28,7 +31,7 @@ def client():
store_minify.fail_safe = False
store_minify.cssless = True
store_minify.js = True
store_minify.bypass = []
store_minify.bypass = ["html_embedded"]
store_minify.bypass_caching = []
store_minify.passive = False
store_minify.parser.runtime_options["html"]["minify_inline"] = {
Expand Down Expand Up @@ -72,7 +75,7 @@ def add_url_rule(route, handler):
def test_html_minify(client):
"""testing HTML minify option"""
resp = client.get("/html")
assert MINIFED_HTML == resp.data
assert MINIFIED_HTML == resp.data


def test_html_bypassing(client):
Expand All @@ -92,18 +95,18 @@ def test_lesscss_minify(client):
"""testing css and less minify option"""
store_minify.cssless = True
resp = client.get("/cssless")
assert MINIFED_LESS == resp.data
assert MINIFIED_LESS == resp.data


def test_minify_cache(client):
"""testing caching minifed response"""
"""testing caching MINIFIED response"""
store_minify.cache.limit = 10
client.get("/cssless") # hit it twice, to get the cached minified response
resp = client.get("/cssless").data

assert resp == MINIFED_LESS
assert resp == MINIFIED_LESS
assert (
MINIFED_LESS.decode("utf-8")
MINIFIED_LESS.decode("utf-8")
in store_minify.cache._cache.get("cssless", {}).values()
)

Expand Down Expand Up @@ -132,7 +135,7 @@ def test_caching_limit_only_when_needed(client):

assert len(store_minify.cache._cache.get("cssless", {})) == 1
for r in resp:
assert MINIFED_LESS == r
assert MINIFIED_LESS == r


def test_caching_limit_exceeding(client):
Expand All @@ -155,7 +158,7 @@ def test_bypass_caching(client):
for v in store_minify.cache._cache.get("cssless", {}).values()
]

assert MINIFED_LESS == resp.data
assert MINIFIED_LESS == resp.data
assert resp.data not in resp_values


Expand Down Expand Up @@ -184,15 +187,15 @@ def test_html_minify_decorated(client):
store_minify.passive = True
resp = client.get("/html_decorated").data

assert resp == MINIFED_HTML
assert resp == MINIFIED_HTML


def test_html_minify_decorated_cache(client):
store_minify.passive = True
client.get("/html_decorated").data
resp = client.get("/html_decorated").data

assert resp == MINIFED_HTML
assert resp == MINIFIED_HTML


def test_javascript_minify_decorated(client):
Expand All @@ -208,7 +211,7 @@ def test_minify_less_decorated(client):
store_minify.passive = True
resp = client.get("/less_decorated").data

assert resp == MINIFED_LESS
assert resp == MINIFIED_LESS


def test_minify_static_js_with_add_url_rule(client):
Expand Down Expand Up @@ -288,7 +291,7 @@ def test_script_types(client):

def test_html_with_embedded_tags(client):
"""test html with embedded js and less tags"""
assert client.get("/html_embedded").data == MINIFED_HTML_EMBEDDED_TAGS
assert client.get("/html_embedded").data == MINIFIED_HTML_EMBEDDED_TAGS


def test_unicode_endpoint(client):
Expand All @@ -297,7 +300,3 @@ def test_unicode_endpoint(client):

assert resp.status == "200 OK"
assert resp.data.decode("utf-8") == "–"


if __name__ == "__main__":
app.run()
Loading

0 comments on commit 7dd0df1

Please sign in to comment.