From 7dbc0286dbceee6e575db101dd52f5c71d21d0fe Mon Sep 17 00:00:00 2001 From: Tomas Zarate <102381901+TOMASEZARATE@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:40:53 +0000 Subject: [PATCH] Support gzip-compressed HTTP responses serve_content() now accepts compress=Compression.GZIP, matching the old unfinished compress attribute. The body is gzipped and sent with Content-Encoding: gzip and the correct Content-Length, but only when the request's Accept-Encoding header allows it. Assisted-by: Cursor Agent with Grok 4.7 --- README.rst | 11 +++++++- pytest_localserver/http.py | 47 +++++++++++++++++++++++++++------ tests/test_http.py | 53 ++++++++++++++++++++++++++++---------- 3 files changed, 89 insertions(+), 22 deletions(-) diff --git a/README.rst b/README.rst index 7a4e557..d1817d6 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 (enumeration) * ``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,13 @@ suggest poking around in the code itself. If chunk encoding is applied, each str or bytes in ``content`` becomes one chunk in the response. + The ``compress`` attribute or parameter can be set to + + * ``Compression.GZIP``, telling the server to gzip-compress the response + and send a ``Content-Encoding: gzip`` header, but only when the request's + ``Accept-Encoding`` header allows gzip + * ``None``, telling the server not to compress the response + 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..0b46981 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 @@ -52,12 +53,29 @@ def __bool__(self): return bool(self.value) +class Compression(enum.Enum): + GZIP = "gzip" + + def _encode_chunk(chunk, charset): if isinstance(chunk, str): chunk = chunk.encode(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 +126,17 @@ def __call__(self, environ, start_response): else: content = self.content - if self.chunked == Chunked.YES or ( - self.chunked == Chunked.AUTO and "chunked" in self.headers.get("Transfer-encoding", "") - ): + compressed = self.compress == Compression.GZIP and _accepts_gzip(request) + if compressed: + content = gzip.compress(_as_bytes(content)) + + if self.chunked == Chunked.YES: + chunked = True + elif self.chunked == Chunked.AUTO and "chunked" in self.headers.get("Transfer-encoding", ""): + chunked = True + else: + chunked = False + 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 +151,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 +170,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 ``Compression.GZIP`` 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 +188,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..67e47a4 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=http.Compression.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 = http.Compression.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=http.Compression.GZIP) + 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=http.Compression.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):