diff --git a/CHANGES b/CHANGES index be63e00..c1418e3 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,8 @@ +Unreleased +---------- + +Support gzip-compressed HTTP responses + 0.10.0 (2026-10-12) ------------------- diff --git a/README.rst b/README.rst index 7a4e557..31e86be 100644 --- a/README.rst +++ b/README.rst @@ -135,6 +135,7 @@ suggest poking around in the code itself. * ``content`` - content of next response (str, bytes, or iterable of either) * ``headers`` - response headers (dict) * ``chunked`` - whether to chunk-encode the response (enumeration) + * ``compress`` - whether to gzip-compress the response * ``store_request_data`` - whether to store request data for later use Once these attributes are set, all subsequent requests will be answered with @@ -148,7 +149,8 @@ suggest poking around in the code itself. code=200, headers=None, chunked=pytest_localserver.http.Chunked.NO, - store_request_data=True + store_request_data=True, + compress=None ) The ``chunked`` attribute or parameter can be set to @@ -161,6 +163,10 @@ suggest poking around in the code itself. If chunk encoding is applied, each str or bytes in ``content`` becomes one chunk in the response. + If ``compress`` is set to ``'gzip'`` or ``True``, the response body is + gzip-compressed and sent with a ``Content-Encoding: gzip`` header, but only + when the request's ``Accept-Encoding`` header allows gzip. + You can use ``store_request_data=False`` to disable loading the request data into memory. This will make it impossible to check the request data using ``httpserver.requests[index].data`` but may make sense when posting a larger amount of diff --git a/pytest_localserver/http.py b/pytest_localserver/http.py index 270ec6f..46dc161 100644 --- a/pytest_localserver/http.py +++ b/pytest_localserver/http.py @@ -3,6 +3,7 @@ # This program is release under the MIT license. You can find the full text of # the license in the LICENSE file. import enum +import gzip import itertools import json import threading @@ -58,6 +59,19 @@ def _encode_chunk(chunk, charset): return f"{len(chunk):x}".encode(charset) + b"\r\n" + chunk + b"\r\n" +def _as_bytes(content, charset="utf-8"): + if isinstance(content, str): + return content.encode(charset) + if isinstance(content, bytes): + return content + return b"".join(item.encode(charset) if isinstance(item, str) else item for item in content) + + +def _accepts_gzip(request): + encodings = request.accept_encodings + return encodings.quality("gzip") > 0 or encodings.quality("x-gzip") > 0 + + class ContentServer(WSGIServer): """ Small test server which can be taught which content (i.e. string) to serve @@ -108,9 +122,14 @@ def __call__(self, environ, start_response): else: content = self.content - if self.chunked == Chunked.YES or ( + compressed = self.compress in (True, "gzip") and _accepts_gzip(request) + if compressed: + content = gzip.compress(_as_bytes(content)) + + chunked = self.chunked == Chunked.YES or ( self.chunked == Chunked.AUTO and "chunked" in self.headers.get("Transfer-encoding", "") - ): + ) + if chunked: # If the code below ever changes to allow setting the charset of # the Response object, the charset used here should also be changed # to match. But until that happens, use UTF-8 since it is Werkzeug's @@ -125,14 +144,16 @@ def __call__(self, environ, start_response): response.headers.clear() response.headers.extend(self.headers) - # FIXME get compression working! - # if self.compress == 'gzip': - # content = gzip.compress(content.encode('utf-8')) - # response.content_encoding = 'gzip' + if compressed: + response.content_encoding = "gzip" + if not chunked: + response.content_length = len(content) return response(environ, start_response) - def serve_content(self, content, code=200, headers=None, chunked=Chunked.NO, store_request_data=True): + def serve_content( + self, content, code=200, headers=None, chunked=Chunked.NO, store_request_data=True, compress=None + ): """ Serves string content (with specified HTTP error code) as response to all subsequent request. @@ -142,6 +163,8 @@ def serve_content(self, content, code=200, headers=None, chunked=Chunked.NO, sto :param headers: HTTP headers to be returned :param chunked: whether to apply chunked transfer encoding to the content :param store_request_data: whether to store data sent as request payload. + :param compress: set to ``'gzip'`` or ``True`` to gzip-compress the response + when the client accepts it """ if not isinstance(content, (str, bytes, list, tuple)): # If content is an iterable which is not known to be a string, @@ -158,5 +181,6 @@ def serve_content(self, content, code=200, headers=None, chunked=Chunked.NO, sto self.code = code self.chunked = chunked self.store_request_data = store_request_data + self.compress = compress if headers: self.headers = Headers(headers) diff --git a/tests/test_http.py b/tests/test_http.py index b603eb0..ec9b1e1 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1,3 +1,4 @@ +import gzip import itertools import subprocess import sys @@ -53,19 +54,45 @@ def test_GET_request(httpserver): assert "text/plain" in resp.headers["Content-type"] -# FIXME get compression working! -# def test_gzipped_GET_request(httpserver): -# httpserver.serve_content('TEST!', headers={'Content-type': 'text/plain'}) -# httpserver.compress = 'gzip' -# resp = requests.get(httpserver.url, headers={ -# 'User-Agent': 'Test method', -# 'Accept-encoding': 'gzip' -# }) -# assert resp.text == 'TEST!' -# assert resp.status_code == 200 -# assert resp.content_encoding == 'gzip' -# assert resp.headers['Content-type'] == 'text/plain' -# assert resp.headers['content-encoding'] == 'gzip' +def test_gzipped_GET_request(httpserver): + httpserver.serve_content("TEST!", headers={"Content-type": "text/plain"}, compress="gzip") + resp = requests.get(httpserver.url, headers={"User-Agent": "Test method", "Accept-Encoding": "gzip"}) + assert resp.text == "TEST!" + assert resp.status_code == 200 + assert "text/plain" in resp.headers["Content-type"] + assert resp.headers["content-encoding"] == "gzip" + assert resp.headers["Content-Length"] == str(len(gzip.compress(b"TEST!"))) + + +def test_gzipped_GET_request_via_attribute(httpserver): + httpserver.serve_content("TEST!", headers={"Content-type": "text/plain"}) + httpserver.compress = "gzip" + resp = requests.get(httpserver.url, headers={"User-Agent": "Test method", "Accept-Encoding": "gzip"}) + assert resp.text == "TEST!" + assert resp.headers["content-encoding"] == "gzip" + assert resp.headers["Content-Length"] == str(len(gzip.compress(b"TEST!"))) + + +def test_gzipped_GET_request_bytes_content(httpserver): + httpserver.serve_content(b"TEST!", headers={"Content-type": "text/plain"}, compress=True) + resp = requests.get(httpserver.url, headers={"Accept-Encoding": "gzip"}) + assert resp.content == b"TEST!" + assert resp.headers["content-encoding"] == "gzip" + + +def test_gzip_not_applied_without_accept_encoding(httpserver): + httpserver.serve_content("TEST!", headers={"Content-type": "text/plain"}, compress="gzip") + resp = requests.get(httpserver.url, headers={"User-Agent": "Test method", "Accept-Encoding": "identity"}) + assert resp.text == "TEST!" + assert resp.status_code == 200 + assert "content-encoding" not in resp.headers + + +def test_gzip_not_applied_by_default(httpserver): + httpserver.serve_content("TEST!", headers={"Content-type": "text/plain"}) + resp = requests.get(httpserver.url, headers={"Accept-Encoding": "gzip"}) + assert resp.text == "TEST!" + assert "content-encoding" not in resp.headers def test_HEAD_request(httpserver):