flask-squeeze 3.3.3__tar.gz → 3.3.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/PKG-INFO +2 -2
- flask_squeeze-3.3.4/flask_squeeze/cache.py +45 -0
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/flask_squeeze/compress.py +9 -14
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/flask_squeeze/flask_squeeze.py +73 -74
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/flask_squeeze/minify.py +11 -16
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/flask_squeeze/utils.py +4 -11
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/flask_squeeze.egg-info/PKG-INFO +2 -2
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/flask_squeeze.egg-info/SOURCES.txt +1 -0
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/pyproject.toml +2 -2
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/tests/test_flask_squeeze.py +46 -1
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/LICENSE +0 -0
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/README.md +0 -0
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/flask_squeeze/__init__.py +0 -0
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/flask_squeeze/log.py +0 -0
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/flask_squeeze/models.py +0 -0
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/flask_squeeze.egg-info/dependency_links.txt +0 -0
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/flask_squeeze.egg-info/requires.txt +0 -0
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/flask_squeeze.egg-info/top_level.txt +0 -0
- {flask_squeeze-3.3.3 → flask_squeeze-3.3.4}/setup.cfg +0 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
5
|
+
|
|
6
|
+
if TYPE_CHECKING:
|
|
7
|
+
from .models import Encoding
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True)
|
|
11
|
+
class CacheKey:
|
|
12
|
+
path: str
|
|
13
|
+
encoding: Encoding | None
|
|
14
|
+
|
|
15
|
+
@property
|
|
16
|
+
def normalized(self) -> str:
|
|
17
|
+
flat_path = self.path.replace("/", "_")
|
|
18
|
+
encoding = self.encoding.value if self.encoding else "none"
|
|
19
|
+
return f"{flat_path}.{encoding}"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True)
|
|
23
|
+
class CachedData:
|
|
24
|
+
original_hash: str
|
|
25
|
+
data: bytes
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass
|
|
29
|
+
class Cache:
|
|
30
|
+
data: dict[str, tuple[str, bytes]]
|
|
31
|
+
""" Maps request path to original data hash and compressed bytes """
|
|
32
|
+
|
|
33
|
+
def get(self, cache_key: CacheKey) -> CachedData | None:
|
|
34
|
+
"""Get the cached hash and data for a given cache key."""
|
|
35
|
+
|
|
36
|
+
if cache_key.normalized not in self.data:
|
|
37
|
+
return None
|
|
38
|
+
|
|
39
|
+
original_hash, data = self.data[cache_key.normalized]
|
|
40
|
+
return CachedData(original_hash, data)
|
|
41
|
+
|
|
42
|
+
def set(self, cache_key: CacheKey, original_hash: str, data: bytes) -> None:
|
|
43
|
+
"""Set the cached data for a given cache key."""
|
|
44
|
+
|
|
45
|
+
self.data[cache_key.normalized] = (original_hash, data)
|
|
@@ -1,38 +1,39 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
1
3
|
import gzip
|
|
2
4
|
import time
|
|
3
5
|
import zlib
|
|
4
6
|
from dataclasses import dataclass
|
|
5
7
|
|
|
6
8
|
import brotli
|
|
7
|
-
from flask import Response
|
|
8
9
|
|
|
9
|
-
from
|
|
10
|
+
from .models import Encoding
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
@dataclass(frozen=True)
|
|
13
|
-
class
|
|
14
|
-
data: bytes
|
|
14
|
+
class CompressionInfo:
|
|
15
15
|
encoding: Encoding
|
|
16
16
|
quality: int
|
|
17
17
|
duration: float
|
|
18
18
|
ratio: float
|
|
19
19
|
|
|
20
20
|
@property
|
|
21
|
-
def
|
|
22
|
-
|
|
21
|
+
def headers(self) -> dict:
|
|
22
|
+
value = "; ".join(
|
|
23
23
|
[
|
|
24
24
|
f"ratio={self.ratio:.1f}x",
|
|
25
25
|
f"qualtiy={self.quality}",
|
|
26
26
|
f"duration={self.duration * 1000:.1f}ms",
|
|
27
27
|
],
|
|
28
28
|
)
|
|
29
|
+
return {"X-Flask-Squeeze-Compress": value}
|
|
29
30
|
|
|
30
31
|
|
|
31
32
|
def compress(
|
|
32
33
|
data: bytes,
|
|
33
34
|
encode_choice: Encoding,
|
|
34
35
|
quality: int,
|
|
35
|
-
) ->
|
|
36
|
+
) -> tuple[bytes, CompressionInfo]:
|
|
36
37
|
t0 = time.perf_counter()
|
|
37
38
|
|
|
38
39
|
compressors = {
|
|
@@ -43,15 +44,9 @@ def compress(
|
|
|
43
44
|
|
|
44
45
|
compressed_data = compressors[encode_choice](data, quality)
|
|
45
46
|
|
|
46
|
-
return
|
|
47
|
-
data=compressed_data,
|
|
47
|
+
return compressed_data, CompressionInfo(
|
|
48
48
|
encoding=encode_choice,
|
|
49
49
|
quality=quality,
|
|
50
50
|
duration=time.perf_counter() - t0,
|
|
51
51
|
ratio=len(data) / len(compressed_data),
|
|
52
52
|
)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
def update_response_with_compressed_data(response: Response, result: CompressionResult) -> None:
|
|
56
|
-
response.set_data(result.data)
|
|
57
|
-
response.headers["X-Flask-Squeeze-Compress"] = result.info
|
|
@@ -4,11 +4,10 @@ import hashlib
|
|
|
4
4
|
|
|
5
5
|
from flask import Flask, Response, request
|
|
6
6
|
|
|
7
|
-
from
|
|
8
|
-
|
|
9
|
-
from .compress import compress, update_response_with_compressed_data
|
|
7
|
+
from .cache import Cache, CacheKey
|
|
8
|
+
from .compress import CompressionInfo, compress
|
|
10
9
|
from .log import d_log, log
|
|
11
|
-
from .minify import
|
|
10
|
+
from .minify import MinificationInfo, minify
|
|
12
11
|
from .models import (
|
|
13
12
|
Encoding,
|
|
14
13
|
Minification,
|
|
@@ -16,18 +15,19 @@ from .models import (
|
|
|
16
15
|
choose_encoding_from_headers_and_config,
|
|
17
16
|
choose_minification_from_mimetype_and_config,
|
|
18
17
|
)
|
|
18
|
+
from .utils import add_breach_exploit_protection_header, update_response_headers
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
class Squeeze:
|
|
22
22
|
__slots__ = "app", "cache_static"
|
|
23
23
|
app: Flask
|
|
24
24
|
|
|
25
|
-
cache_static:
|
|
25
|
+
cache_static: Cache
|
|
26
26
|
""" (request.path, encoding) -> (original file sha256 hash, compressed bytes) """
|
|
27
27
|
|
|
28
28
|
def __init__(self, app: Flask | None = None) -> None:
|
|
29
29
|
"""Initialize Flask-Squeeze with or without app."""
|
|
30
|
-
self.cache_static = {}
|
|
30
|
+
self.cache_static = Cache(data={})
|
|
31
31
|
if app is None:
|
|
32
32
|
return
|
|
33
33
|
self.app = app
|
|
@@ -89,23 +89,20 @@ class Squeeze:
|
|
|
89
89
|
) -> None:
|
|
90
90
|
assert encode_choice or minify_choice
|
|
91
91
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
data, minification_info, compression_info = self.squeeze(
|
|
93
|
+
response.get_data(as_text=False),
|
|
94
|
+
ResourceType.dynamic,
|
|
95
|
+
minify_choice,
|
|
96
|
+
encode_choice,
|
|
97
|
+
)
|
|
95
98
|
|
|
96
|
-
|
|
97
|
-
minification_result = minify(response.get_data(as_text=False), minify_choice)
|
|
98
|
-
update_response_with_minified_data(response, minification_result)
|
|
99
|
+
response.set_data(data)
|
|
99
100
|
|
|
100
|
-
|
|
101
|
+
if minification_info:
|
|
102
|
+
response.headers.update(minification_info.headers)
|
|
101
103
|
|
|
102
|
-
if
|
|
103
|
-
|
|
104
|
-
response.get_data(as_text=False),
|
|
105
|
-
encode_choice,
|
|
106
|
-
self.get_configured_quality(encode_choice, ResourceType.dynamic),
|
|
107
|
-
)
|
|
108
|
-
update_response_with_compressed_data(response, compression_result)
|
|
104
|
+
if compression_info:
|
|
105
|
+
response.headers.update(compression_info.headers)
|
|
109
106
|
add_breach_exploit_protection_header(response)
|
|
110
107
|
|
|
111
108
|
####################################################################################
|
|
@@ -117,69 +114,70 @@ class Squeeze:
|
|
|
117
114
|
encode_choice: Encoding | None,
|
|
118
115
|
minify_choice: Minification | None,
|
|
119
116
|
) -> None:
|
|
120
|
-
|
|
117
|
+
"""
|
|
118
|
+
If the hash of the current response matches the hash of the cached response,
|
|
119
|
+
return the cached response. Otherwise, compress and minify the response and
|
|
120
|
+
cache the compressed response.
|
|
121
|
+
"""
|
|
122
|
+
assert encode_choice or minify_choice
|
|
121
123
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
+
data = response.get_data(as_text=False)
|
|
125
|
+
data_hash = hashlib.sha256(data).hexdigest()
|
|
124
126
|
|
|
125
|
-
|
|
127
|
+
cache_key = CacheKey(request.path, encode_choice)
|
|
128
|
+
cached = self.cache_static.get(cache_key)
|
|
126
129
|
|
|
127
|
-
if
|
|
128
|
-
|
|
130
|
+
if cached is not None and data_hash == cached.original_hash:
|
|
131
|
+
log(2, "Found in cache, hashes match. RETURN")
|
|
132
|
+
response.set_data(cached.data)
|
|
133
|
+
response.headers["X-Flask-Squeeze-Cache"] = "HIT"
|
|
134
|
+
return
|
|
129
135
|
|
|
130
|
-
|
|
136
|
+
# Not in cache, compress and minify
|
|
131
137
|
|
|
132
|
-
|
|
133
|
-
|
|
138
|
+
log(2, "Not in cache or hashes don't match. Squeeze and cache.")
|
|
139
|
+
data, minification_info, compression_info = self.squeeze(
|
|
140
|
+
data,
|
|
141
|
+
ResourceType.static,
|
|
142
|
+
minify_choice,
|
|
143
|
+
encode_choice,
|
|
144
|
+
)
|
|
134
145
|
|
|
135
|
-
|
|
136
|
-
log(2, "Found in cache, hashes match. RETURN")
|
|
137
|
-
response.set_data(compressed_data)
|
|
138
|
-
response.headers["X-Flask-Squeeze-Cache"] = "HIT"
|
|
139
|
-
return
|
|
146
|
+
response.set_data(data)
|
|
140
147
|
|
|
141
|
-
|
|
148
|
+
if minification_info:
|
|
149
|
+
response.headers.update(minification_info.headers)
|
|
142
150
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
update_response_with_minified_data(response, minfication_result)
|
|
146
|
-
current_data = minfication_result.data
|
|
147
|
-
if encode_choice is not None:
|
|
148
|
-
compression_result = compress(
|
|
149
|
-
current_data,
|
|
150
|
-
encode_choice,
|
|
151
|
-
self.get_configured_quality(encode_choice, ResourceType.static),
|
|
152
|
-
)
|
|
153
|
-
update_response_with_compressed_data(response, compression_result)
|
|
154
|
-
current_data = compression_result.data
|
|
151
|
+
if compression_info:
|
|
152
|
+
response.headers.update(compression_info.headers)
|
|
155
153
|
|
|
156
|
-
|
|
154
|
+
response.headers["X-Flask-Squeeze-Cache"] = "MISS"
|
|
157
155
|
|
|
158
|
-
|
|
159
|
-
|
|
156
|
+
# Cache the compressed data, with the dash of the original data
|
|
157
|
+
self.cache_static.set(cache_key, data_hash, data)
|
|
160
158
|
|
|
161
|
-
|
|
159
|
+
def squeeze(
|
|
160
|
+
self,
|
|
161
|
+
data: bytes,
|
|
162
|
+
resource_type: ResourceType,
|
|
163
|
+
minify_choice: Minification | None,
|
|
164
|
+
encode_choice: Encoding | None,
|
|
165
|
+
) -> tuple[bytes, MinificationInfo | None, CompressionInfo | None]:
|
|
166
|
+
assert encode_choice or minify_choice
|
|
162
167
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
update_response_with_compressed_data(response, compression_result)
|
|
177
|
-
|
|
178
|
-
# Assert: At least one of minify or compress was run
|
|
179
|
-
|
|
180
|
-
compressed_data = response.get_data(as_text=False)
|
|
181
|
-
self.cache_static[(request.path, encode_choice_str)] = (original_sha256, compressed_data)
|
|
182
|
-
response.headers["X-Flask-Squeeze-Cache"] = "MISS"
|
|
168
|
+
minification_info = None
|
|
169
|
+
if minify_choice is not None:
|
|
170
|
+
data, minification_info = minify(data, minify_choice)
|
|
171
|
+
|
|
172
|
+
compression_info = None
|
|
173
|
+
if encode_choice is not None:
|
|
174
|
+
data, compression_info = compress(
|
|
175
|
+
data,
|
|
176
|
+
encode_choice,
|
|
177
|
+
self.get_configured_quality(encode_choice, resource_type),
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
return data, minification_info, compression_info
|
|
183
181
|
|
|
184
182
|
####################################################################################
|
|
185
183
|
#### MARK: After Request
|
|
@@ -223,13 +221,14 @@ class Squeeze:
|
|
|
223
221
|
|
|
224
222
|
# At least one of minify or compress is requested
|
|
225
223
|
|
|
226
|
-
|
|
224
|
+
response.direct_passthrough = False # In both cases, we need to read the data
|
|
227
225
|
|
|
228
226
|
if request.path.startswith("/static/"):
|
|
229
227
|
self.run_static(response, encode_choice, minify_choice)
|
|
230
228
|
else:
|
|
231
229
|
self.run_dynamic(response, encode_choice, minify_choice)
|
|
232
230
|
|
|
233
|
-
update_response_headers(response,
|
|
234
|
-
|
|
231
|
+
update_response_headers(response, encode_choice)
|
|
232
|
+
|
|
233
|
+
log(1, f"Static cache: {self.cache_static.data.keys()}")
|
|
235
234
|
return response
|
|
@@ -1,28 +1,29 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
1
3
|
import time
|
|
2
4
|
from dataclasses import dataclass
|
|
3
5
|
|
|
4
6
|
import rcssmin
|
|
5
7
|
import rjsmin
|
|
6
|
-
from flask import Response
|
|
7
8
|
|
|
8
|
-
from
|
|
9
|
+
from .models import Minification
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
@dataclass(frozen=True)
|
|
12
|
-
class
|
|
13
|
-
data: bytes
|
|
13
|
+
class MinificationInfo:
|
|
14
14
|
minification: Minification
|
|
15
15
|
duration: float
|
|
16
16
|
ratio: float
|
|
17
17
|
|
|
18
18
|
@property
|
|
19
|
-
def
|
|
20
|
-
|
|
19
|
+
def headers(self) -> dict:
|
|
20
|
+
value = "; ".join(
|
|
21
21
|
[
|
|
22
22
|
f"ratio={self.ratio:.1f}x",
|
|
23
23
|
f"duration={self.duration * 1000:.1f}ms",
|
|
24
24
|
],
|
|
25
25
|
)
|
|
26
|
+
return {"X-Flask-Squeeze-Minify": value}
|
|
26
27
|
|
|
27
28
|
|
|
28
29
|
def minify_html(html_text: str) -> str:
|
|
@@ -81,7 +82,7 @@ def minify_js(data: str) -> str:
|
|
|
81
82
|
return minified
|
|
82
83
|
|
|
83
84
|
|
|
84
|
-
def minify(data: bytes, minify_choice: Minification) ->
|
|
85
|
+
def minify(data: bytes, minify_choice: Minification) -> tuple[bytes, MinificationInfo]:
|
|
85
86
|
"""
|
|
86
87
|
Run the minification using the correct minify function and return the minified data.
|
|
87
88
|
"""
|
|
@@ -94,16 +95,10 @@ def minify(data: bytes, minify_choice: Minification) -> MinificationResult:
|
|
|
94
95
|
Minification.js: lambda d: minify_js(d),
|
|
95
96
|
}
|
|
96
97
|
|
|
97
|
-
|
|
98
|
+
minified_data = minifiers[minify_choice](data.decode("utf-8")).encode("utf-8")
|
|
98
99
|
|
|
99
|
-
return
|
|
100
|
-
data=minified,
|
|
100
|
+
return minified_data, MinificationInfo(
|
|
101
101
|
minification=minify_choice,
|
|
102
102
|
duration=time.perf_counter() - t0,
|
|
103
|
-
ratio=len(data) / len(
|
|
103
|
+
ratio=len(data) / len(minified_data),
|
|
104
104
|
)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
def update_response_with_minified_data(response: Response, result: MinificationResult) -> None:
|
|
108
|
-
response.set_data(result.data)
|
|
109
|
-
response.headers["X-Flask-Squeeze-Minify"] = result.info
|
|
@@ -6,7 +6,7 @@ from typing import TYPE_CHECKING
|
|
|
6
6
|
if TYPE_CHECKING:
|
|
7
7
|
from flask import Response
|
|
8
8
|
|
|
9
|
-
from
|
|
9
|
+
from .models import Encoding
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
def add_breach_exploit_protection_header(response: Response) -> None:
|
|
@@ -18,11 +18,7 @@ def add_breach_exploit_protection_header(response: Response) -> None:
|
|
|
18
18
|
response.headers["X-Flask-Squeeze-Breach-Protection"] = rand_str
|
|
19
19
|
|
|
20
20
|
|
|
21
|
-
def update_response_headers(
|
|
22
|
-
response: Response,
|
|
23
|
-
original_content_length: int,
|
|
24
|
-
encode_choice: Encoding | None,
|
|
25
|
-
) -> None:
|
|
21
|
+
def update_response_headers(response: Response, encode_choice: Encoding | None) -> None:
|
|
26
22
|
"""
|
|
27
23
|
Set the Content-Length header if it has changed.
|
|
28
24
|
Set the Content-Encoding header if compressed data is served.
|
|
@@ -32,9 +28,6 @@ def update_response_headers(
|
|
|
32
28
|
if encode_choice is not None:
|
|
33
29
|
response.headers["Content-Encoding"] = encode_choice.value
|
|
34
30
|
vary = {x.strip() for x in response.headers.get("Vary", "").split(",")}
|
|
35
|
-
|
|
36
|
-
vary.discard("")
|
|
37
|
-
response.headers["Vary"] = ",".join(vary)
|
|
31
|
+
response.headers["Vary"] = ",".join((vary | {"Accept-Encoding"}) - {""})
|
|
38
32
|
|
|
39
|
-
|
|
40
|
-
response.headers["Content-Length"] = response.content_length
|
|
33
|
+
response.headers["Content-Length"] = response.content_length
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "flask-squeeze"
|
|
3
|
-
version = "3.3.
|
|
3
|
+
version = "3.3.4"
|
|
4
4
|
description = "Compress and minify Flask responses!"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [{ name = "Marcel Kröker", email = "kroeker.marcel@gmail.com" }]
|
|
@@ -30,7 +30,7 @@ dependencies = [
|
|
|
30
30
|
|
|
31
31
|
[dependency-groups]
|
|
32
32
|
dev = [
|
|
33
|
-
"pytest-cov ~=
|
|
33
|
+
"pytest-cov ~= 5.0.0",
|
|
34
34
|
"ruff>=0.9.3",
|
|
35
35
|
]
|
|
36
36
|
|
|
@@ -75,7 +75,8 @@ def test_get_index(client: FlaskClient, use_encoding: str) -> None:
|
|
|
75
75
|
def test_get_css_file(client: FlaskClient, use_encoding: str, use_minify_css: bool) -> None:
|
|
76
76
|
print("test_get_css_file with", use_encoding, "minify:", use_minify_css)
|
|
77
77
|
client.application.config.update({"SQUEEZE_MINIFY_CSS": use_minify_css})
|
|
78
|
-
|
|
78
|
+
url = "/static/fomantic.css"
|
|
79
|
+
r = client.get(url, headers={"Accept-Encoding": use_encoding})
|
|
79
80
|
assert content_length_correct(r)
|
|
80
81
|
response_length = int(r.headers.get("Content-Length", "0"))
|
|
81
82
|
encoding = r.headers.get("Content-Encoding", "")
|
|
@@ -272,3 +273,47 @@ def test_static_file_cache_behavior(
|
|
|
272
273
|
# Fourth request: HIT
|
|
273
274
|
r = client.get(url_path, headers={"Accept-Encoding": use_encoding})
|
|
274
275
|
assert r.headers.get("X-Flask-Squeeze-Cache") == "HIT"
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def test_malformed_accept_encoding(client: FlaskClient) -> None:
|
|
279
|
+
# Case 1: Missing Accept-Encoding
|
|
280
|
+
r = client.get("/static/jquery.js")
|
|
281
|
+
assert "Content-Encoding" not in r.headers
|
|
282
|
+
|
|
283
|
+
# Case 2: Malformed Accept-Encoding
|
|
284
|
+
r = client.get("/static/jquery.js", headers={"Accept-Encoding": "invalid-encoding"})
|
|
285
|
+
assert "Content-Encoding" not in r.headers
|
|
286
|
+
|
|
287
|
+
# Case 3: Conflicting Accept-Encoding values
|
|
288
|
+
r = client.get("/static/jquery.js", headers={"Accept-Encoding": "gzip, br"})
|
|
289
|
+
assert r.headers.get("Content-Encoding") in {"gzip", "br"}
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def test_cache_invalidation_on_compression_disable(client: FlaskClient) -> None:
|
|
293
|
+
# Enable compression and fetch the file
|
|
294
|
+
client.application.config.update({"SQUEEZE_COMPRESS": True})
|
|
295
|
+
r = client.get("/static/jquery.js", headers={"Accept-Encoding": "gzip"})
|
|
296
|
+
assert r.headers.get("X-Flask-Squeeze-Cache") == "MISS"
|
|
297
|
+
|
|
298
|
+
# Fetch again to ensure it is cached
|
|
299
|
+
r = client.get("/static/jquery.js", headers={"Accept-Encoding": "gzip"})
|
|
300
|
+
assert r.headers.get("X-Flask-Squeeze-Cache") == "HIT"
|
|
301
|
+
|
|
302
|
+
# Disable compression
|
|
303
|
+
client.application.config.update({"SQUEEZE_COMPRESS": False})
|
|
304
|
+
|
|
305
|
+
# Fetch again, cache should be invalidated
|
|
306
|
+
r = client.get("/static/jquery.js", headers={"Accept-Encoding": "gzip"})
|
|
307
|
+
assert r.headers.get("X-Flask-Squeeze-Cache") == "MISS"
|
|
308
|
+
assert "Content-Encoding" not in r.headers
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def test_small_response_below_min_size(client: FlaskClient) -> None:
|
|
312
|
+
min_size = 1000
|
|
313
|
+
client.application.config.update({"SQUEEZE_MIN_SIZE": min_size})
|
|
314
|
+
|
|
315
|
+
# A small response
|
|
316
|
+
r = client.get("/static/smallfile.js", headers={"Accept-Encoding": "gzip"})
|
|
317
|
+
assert r.content_length < min_size # Ensure response is small
|
|
318
|
+
assert "Content-Encoding" not in r.headers
|
|
319
|
+
assert "X-Flask-Squeeze-Minify" not in r.headers
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|