diff options
author | Marc Abramowitz <marc@marc-abramowitz.com> | 2016-03-07 15:19:32 -0800 |
---|---|---|
committer | Marc Abramowitz <marc@marc-abramowitz.com> | 2016-03-07 15:19:32 -0800 |
commit | f6ef23ba610ffe47d08d9684ba6bd554c78862fb (patch) | |
tree | cc8bca89295e3387f078fe019992ca99995a71da | |
parent | 92a0a74a48bc6e2498543944f9634dd1658fff39 (diff) | |
download | paste-gzip_work_with_generator.tar.gz |
Make gzip work with app that is generatorgzip_work_with_generator
```
$ tox
...
py26: commands succeeded
py27: commands succeeded
py34: commands succeeded
py35: commands succeeded
pypy: commands succeeded
congratulations :)
```
-rw-r--r-- | paste/gzipper.py | 15 | ||||
-rw-r--r-- | tests/test_gzipper.py | 53 |
2 files changed, 60 insertions, 8 deletions
diff --git a/paste/gzipper.py b/paste/gzipper.py index eca8775..5f59141 100644 --- a/paste/gzipper.py +++ b/paste/gzipper.py @@ -71,15 +71,18 @@ class GzipResponse(object): return [s] def finish_response(self, app_iter): - if self.compressible: - output = gzip.GzipFile(mode='wb', compresslevel=self.compress_level, - fileobj=self.buffer) - else: - output = self.buffer + output = None + try: for s in app_iter: + if not output: + if self.compressible: + output = gzip.GzipFile(mode='wb', compresslevel=self.compress_level, + fileobj=self.buffer) + else: + output = self.buffer output.write(s) - if self.compressible: + if output and self.compressible: output.close() finally: if hasattr(app_iter, 'close'): diff --git a/tests/test_gzipper.py b/tests/test_gzipper.py index 54b7901..8ad5ec3 100644 --- a/tests/test_gzipper.py +++ b/tests/test_gzipper.py @@ -3,17 +3,66 @@ from paste.gzipper import middleware import gzip import six + def simple_app(environ, start_response): start_response('200 OK', [('content-type', 'text/plain')]) return [b'this is a test'] -wsgi_app = middleware(simple_app) -app = TestApp(wsgi_app) + +def simple_app_nothing(environ, start_response): + start_response('200 OK', [('content-type', 'text/plain')]) + return [b''] + + +def simple_app_generator(environ, start_response): + start_response('200 OK', [('content-type', 'text/plain')]) + yield b'this is a test' + + +def simple_app_generator_nothing(environ, start_response): + start_response('200 OK', [('content-type', 'text/plain')]) + yield b'' + def test_gzip(): + wsgi_app = middleware(simple_app) + app = TestApp(wsgi_app) + res = app.get( + '/', extra_environ=dict(HTTP_ACCEPT_ENCODING='gzip')) + assert int(res.header('content-length')) == len(res.body) + assert res.body != b'this is a test' + actual = gzip.GzipFile(fileobj=six.BytesIO(res.body)).read() + assert actual == b'this is a test' + + +def test_gzip_nothing(): + wsgi_app = middleware(simple_app_nothing) + app = TestApp(wsgi_app) + res = app.get( + '/', extra_environ=dict(HTTP_ACCEPT_ENCODING='gzip')) + assert int(res.header('content-length')) == len(res.body) + assert res.body != b'this is a test' + actual = gzip.GzipFile(fileobj=six.BytesIO(res.body)).read() + assert actual == b'' + + +def test_gzip_generator(): + wsgi_app = middleware(simple_app_generator) + app = TestApp(wsgi_app) res = app.get( '/', extra_environ=dict(HTTP_ACCEPT_ENCODING='gzip')) assert int(res.header('content-length')) == len(res.body) assert res.body != b'this is a test' actual = gzip.GzipFile(fileobj=six.BytesIO(res.body)).read() assert actual == b'this is a test' + + +def test_gzip_generator_nothing(): + wsgi_app = middleware(simple_app_generator_nothing) + app = TestApp(wsgi_app) + res = app.get( + '/', extra_environ=dict(HTTP_ACCEPT_ENCODING='gzip')) + assert int(res.header('content-length')) == len(res.body) + assert res.body != b'this is a test' + actual = gzip.GzipFile(fileobj=six.BytesIO(res.body)).read() + assert actual == b'' |