def GET(self, path: str, **kwargs: object) -> httpx.Response:
return self._request("GET", path, **kwargs)
+ def _raw_body(
+ self,
+ method: str,
+ path: str,
+ *,
+ cert: str | None = None,
+ **kwargs: object,
+ ) -> bytes:
+ """Return the response body WITHOUT content-decoding.
+
+ httpx transparently inflates gzip/deflate responses, so ``.content`` is
+ the decoded plaintext. The mod_deflate round-trip tests need the raw
+ compressed bytes (to re-POST them through the inflate input filter), so
+ stream the response and read ``iter_raw()``, which yields the bytes as
+ they came off the wire -- the analog of LWP not auto-decoding.
+ """
+ client = self._client_for(cert) if cert is not None else self._client
+ request = client.build_request(method, self._url(path), **kwargs) # type: ignore[arg-type]
+ response = client.send(request, stream=True)
+ try:
+ return b"".join(response.iter_raw())
+ finally:
+ response.close()
+
+ def GET_RAW(self, path: str, **kwargs: object) -> bytes:
+ """GET ``path`` and return the raw, undecoded response body bytes."""
+ return self._raw_body("GET", path, **kwargs)
+
def HEAD(self, path: str, **kwargs: object) -> httpx.Response:
return self._request("HEAD", path, **kwargs)
@need_module("filter", "include", "deflate")
def test_pr49328(http):
- content = http.GET(URI, headers={"Accept-Encoding": "gzip"}).content
+ # GET_RAW: keep the gzip stream undecoded so we can re-POST it through the
+ # inflate input filter (httpx would otherwise auto-decompress .content).
+ content = http.GET_RAW(URI, headers={"Accept-Encoding": "gzip"})
deflated = http.POST_BODY(
INFLATOR, content=content, headers={"Content-Encoding": "gzip"}
)
for uri in _uris(http):
original = http.GET_BODY(uri)
- # httpx auto-decompresses; ask the server explicitly for the raw gzip
- # by reading .content with the gzip Accept-Encoding, then re-POST it.
- deflated_resp = http.GET(uri, headers=DEFLATE_HEADERS)
- # We need the raw gzip bytes to re-inflate; httpx exposes the (already
- # decoded) text, so reconstruct via the same endpoint the Perl test
- # used: POST the deflated bytes back. To obtain the raw deflated bytes
- # without httpx auto-decoding we disable decoding through a header probe;
- # fall back to comparing decoded bodies, which is what the round-trip
- # asserts anyway.
- deflated = deflated_resp.content
+ # httpx auto-decompresses, so GET_RAW reads the body undecoded to get
+ # the actual gzip stream; re-POST it through the inflate input filter
+ # (echo_post) and assert it round-trips back to the original.
+ deflated = http.GET_RAW(uri, headers=DEFLATE_HEADERS)
deflated_str_q0 = http.GET_BODY(uri, headers=DEFLATE_HEADERS_Q0)