self.write('hello world')
def get_app_kwargs(self):
- return dict(gzip=True)
+ return dict(
+ gzip=True,
+ static_path=os.path.join(os.path.dirname(__file__), 'static'))
def test_gzip(self):
response = self.fetch('/')
'gzip')
self.assertEqual(response.headers['Vary'], 'Accept-Encoding')
+ def test_gzip_static(self):
+ # The streaming responses in StaticFileHandler have subtle
+ # interactions with the gzip output so test this case separately.
+ response = self.fetch('/robots.txt')
+ self.assertEqual(
+ response.headers.get(
+ 'Content-Encoding',
+ response.headers.get('X-Consumed-Content-Encoding')),
+ 'gzip')
+ self.assertEqual(response.headers['Vary'], 'Accept-Encoding')
+
def test_gzip_not_requested(self):
response = self.fetch('/', use_gzip=False)
self.assertNotIn('Content-Encoding', response.headers)
ctype = _unicode(headers.get("Content-Type", "")).split(";")[0]
self._gzipping = self._compressible_type(ctype) and \
(not finishing or len(chunk) >= self.MIN_LENGTH) and \
- (finishing or "Content-Length" not in headers) and \
("Content-Encoding" not in headers)
if self._gzipping:
headers["Content-Encoding"] = "gzip"
self._gzip_file = gzip.GzipFile(mode="w", fileobj=self._gzip_value)
chunk = self.transform_chunk(chunk, finishing)
if "Content-Length" in headers:
- headers["Content-Length"] = str(len(chunk))
+ # The original content length is no longer correct.
+ # If this is the last (and only) chunk, we can set the new
+ # content-length; otherwise we remove it and fall back to
+ # chunked encoding.
+ if finishing:
+ headers["Content-Length"] = str(len(chunk))
+ else:
+ del headers["Content-Length"]
return status_code, headers, chunk
def transform_chunk(self, chunk, finishing):