Repository: mrf345/flask_minify

Issue:
Decorator returns an invalid response when cache is enabled 
Found an invariant condition in `decorators.minify` which returns an invalid response whenever caching is enabled

```
tests/integration.py:166: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.nox/test-3-7/lib/python3.7/site-packages/werkzeug/test.py:1006: in get
    return self.open(*args, **kw)
.nox/test-3-7/lib/python3.7/site-packages/flask/testing.py:227: in open
    follow_redirects=follow_redirects,
.nox/test-3-7/lib/python3.7/site-packages/werkzeug/test.py:970: in open
    response = self.run_wsgi_app(environ.copy(), buffered=buffered)
.nox/test-3-7/lib/python3.7/site-packages/werkzeug/test.py:861: in run_wsgi_app
    rv = run_wsgi_app(self.application, environ, buffered=buffered)
.nox/test-3-7/lib/python3.7/site-packages/werkzeug/test.py:1096: in run_wsgi_app
    app_rv = app(environ, start_response)
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:2464: in __call__
    return self.wsgi_app(environ, start_response)
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:2450: in wsgi_app
    response = self.handle_exception(e)
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:1867: in handle_exception
    reraise(exc_type, exc_value, tb)
.nox/test-3-7/lib/python3.7/site-packages/flask/_compat.py:39: in reraise
    raise value
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:2447: in wsgi_app
    response = self.full_dispatch_request()
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:1953: in full_dispatch_request
    return self.finalize_request(rv)
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:1968: in finalize_request
    response = self.make_response(rv)
```



Discussion:

Patch:
diff --git a/flask_minify/about.py b/flask_minify/about.py
index 2173daa..8d3fcfd 100644
--- a/flask_minify/about.py
+++ b/flask_minify/about.py
@@ -1,4 +1,4 @@
-__version__ = '0.30'
+__version__ = '0.31'
 __doc__ = 'Flask extension to minify html, css, js and less.'
 __license__ = 'MIT'
 __author__ = 'Mohamed Feddad'
diff --git a/flask_minify/decorators.py b/flask_minify/decorators.py
index e86ae78..dd379f7 100644
--- a/flask_minify/decorators.py
+++ b/flask_minify/decorators.py
@@ -23,11 +23,10 @@ def minify(html=False, js=False, cssless=False, cache=True, fail_safe=True):
     -------
         String of minified HTML content.
     '''
-    def wrapper(function):
+    def decorator(function):
         @wraps(function)
-        def decorator(*args, **kwargs):
-            response_text = function(*args, **kwargs)
-            text = response_text
+        def wrapper(*args, **kwargs):
+            text = function(*args, **kwargs)
             key = None
             cache_key, cached = function.__dict__.get('minify', (None, None))
             should_minify = isinstance(text, str) and any([html, js, cssless])
@@ -41,11 +40,10 @@ def minify(html=False, js=False, cssless=False, cache=True, fail_safe=True):
                                              not html, cssless, js)
 
                     if cache:
-                        function.__dict__['minify'] = (hashing(response_text)
-                                                       .hexdigest(), text)
-
-            return cache if all([cache_key == key,
-                                 cache,
-                                 should_minify]) else text
-        return decorator
-    return wrapper
+                        function.__dict__['minify'] = (key, text)
+
+            return cached if all([cache_key == key,
+                                  cache,
+                                  should_minify]) else text
+        return wrapper
+    return decorator


Test patch:
diff --git a/tests/integration.py b/tests/integration.py
index 23fc411..20ecc01 100644
--- a/tests/integration.py
+++ b/tests/integration.py
@@ -3,8 +3,9 @@ import os
 import sys
 from pytest import fixture
 from flask import send_from_directory
+from xxhash import xxh64
 
-from .setup import store_minify, app
+from .setup import store_minify, app, html_decorated
 from .constants import (HTML, LESS, FALSE_LESS, MINIFED_HTML, MINIFIED_JS,
                         MINIFED_LESS, MINIFIED_JS_RAW, MINIFIED_LESS_RAW, JS,
                         JS_RAW, LESS_RAW, JS_WITH_TYPE, MINIFIED_JS_WITH_TYPE,
@@ -160,6 +161,24 @@ def test_html_minify_decorated(client):
     assert resp == MINIFED_HTML
 
 
+def test_html_minify_decorated_cache(client):
+    store_minify.passive = True
+    client.get('/html_decorated').data
+    resp = client.get('/html_decorated').data
+    hash_key = xxh64(HTML).hexdigest()
+
+    if sys.version_info[0] >= 3:
+        cache_tuple = html_decorated.__wrapped__.minify
+    else:
+        cache_tuple = [getattr(c.cell_contents, '__dict__')
+                       for c in html_decorated.__closure__
+                       if hasattr(c.cell_contents, '__dict__')
+                       ][-1]['minify']
+
+    assert resp == MINIFED_HTML
+    assert cache_tuple == (hash_key, resp.decode('utf-8'))
+
+
 def test_javascript_minify_decorated(client):
     '''test minifying javascript decorator'''
     store_minify.passive = True