"""交付面:按 content-type 的 gzip(阶段 30)。 立项依据(阶段 29 两把尺):首访过网 1 292 834 B 里 `/api` 占 773 646 B,是当时唯一没压的一族; 但同一个路径前缀下 JSON 能省 83.4 %、`thumb/*.png` 反而 +1.1 % ⇒ 判据只能是 media type, 不能是路径前缀。所以这里逐条锁死「什么被压、什么必须原样、压完必须等价」: · 文本族(JSON / CSV / 纯文本导出 / SVG / JS)达阈值 ⇒ gzip; · 二进制(PNG 缩略图、video/mp4 交付片)⇒ **一个字节都不许动**, 尤其是 mp4:Starlette 压完会删 `Content-Length`,播放器靠它算 Range 进度条; · 阈值以下、客户端不收 gzip、上游已经压过 ⇒ 原样透传,绝不冒充 `Content-Encoding`; · 压完解压回来必须与原文逐字节相同,`Vary` 必须含 accept-encoding(否则中间缓存会串味)。 与被测代码同一把尺的常量从 `app.main` 导入,不在这儿抄第二份数字。 """ from __future__ import annotations import gzip import pytest from fastapi import FastAPI, Request from fastapi.responses import (FileResponse, JSONResponse, PlainTextResponse, Response, StreamingResponse) from fastapi.testclient import TestClient from app.main import (GZIP_LEVEL, GZIP_MINIMUM, ContentTypeGZipMiddleware, gzip_eligible) # 中文正文:JSON 里带 CJK 时 gzip 的相对收益更高(UTF-8 一个汉字 3 B),与真实列表接口同形状 BIG = ('{"shots":[' + '{"title":"深夜面馆","line":"最后一盏灯"},' * 400 + "]}").encode("utf-8") SMALL = b'{"ok":true}' # 已压过的位图:gzip 它只会变大(阶段 29 实测 +1.1 %),拿真实字节形状做断言 WEBP = b"RIFF" + (36 + 4096).to_bytes(4, "little") + b"WEBPVP8 " + bytes(range(256)) * 16 @pytest.fixture() def client(tmp_path): """一个只装被测中间件的小 app:形状对齐真实端点,不牵进鉴权与数据库。""" app = FastAPI() app.add_middleware(ContentTypeGZipMiddleware, minimum_size=GZIP_MINIMUM, compresslevel=GZIP_LEVEL) png = tmp_path / "thumb.png" # 高度不可压的二进制(gzip 压它只会变大),模拟 /api/media/thumb/*.png png.write_bytes(bytes(range(256)) * 400) mp4 = tmp_path / "final.mp4" mp4.write_bytes(bytes(range(256)) * 200) @app.get("/api/projects") def projects(): # 大 JSON:/api/projects 的形状 return JSONResponse({"raw": BIG.decode()}) @app.get("/api/meta") def meta(): return JSONResponse({"options": ["x"], "ok": True}) @app.get("/api/export.csv") def csv_export(): return PlainTextResponse(BIG.decode(), media_type="text/csv; charset=utf-8") @app.get("/api/thumb.png") def thumb(): return FileResponse(png, media_type="image/png") @app.get("/api/download.mp4") def download(): return FileResponse(mp4, media_type="video/mp4") @app.get("/api/tiny") def tiny(): return Response(SMALL, media_type="application/json") @app.get("/api/pre-encoded") def pre_encoded(): return Response(gzip.compress(SMALL), media_type="application/json", headers={"Content-Encoding": "gzip", "Content-Length": str(len(gzip.compress(SMALL)))}) @app.post("/api/no-content") def no_content(): return Response(status_code=204) @app.get("/api/stream") def stream(): return JSONResponse({"a": 1}, headers={"Content-Type": "text/event-stream"}) @app.get("/api/stream-json") def stream_json(): # 与真实接口的唯一差别就是分块:用来暴露 minimum_size 在流式下不生效 return StreamingResponse(iter([SMALL]), media_type="application/json") @app.get("/api/stream-webp") def stream_webp(): return StreamingResponse(iter([WEBP]), media_type="image/webp") return TestClient(app) def gz(r): return r.headers.get("content-encoding", "") def accept_gzip(): return {"Accept-Encoding": "gzip, deflate, br"} # ---------------------------------------------------------------- 该压的必须压 def test_large_json_is_gzipped_and_decodes_identical(client): r = client.get("/api/projects", headers=accept_gzip()) assert r.status_code == 200 assert gz(r) == "gzip", "大 JSON 没压 ⇒ 首屏最大的一笔字节白过网" assert "accept-encoding" in r.headers.get("vary", "").lower(), "缺 Vary ⇒ 共享缓存会把 gzip 发给不收它的客户端" assert r.json() == {"raw": BIG.decode()}, "解压后必须与原文等价" def test_text_exports_are_gzipped(client): r = client.get("/api/export.csv", headers=accept_gzip()) assert gz(r) == "gzip" assert r.content == BIG # ---------------------------------------------------------------- 不该压的一律别动 def test_png_thumbnail_is_untouched(client): """缩略图压完是 +1.1 %(阶段 29 实测),而且会丢掉 Content-Length。""" r = client.get("/api/thumb.png", headers=accept_gzip()) assert gz(r) == "" assert int(r.headers["content-length"]) == len(r.content) == 400 * 256 def test_video_range_request_keeps_content_length_and_status(client): """播放器 seek:206 + Content-Range + 切片长度,一个头都不能被压缩层动过。""" r = client.get("/api/download.mp4", headers=dict(accept_gzip(), Range="bytes=100-199")) assert r.status_code == 206 assert gz(r) == "" assert r.headers["content-range"] == "bytes 100-199/51200" assert int(r.headers["content-length"]) == 100 == len(r.content) assert r.headers.get("accept-ranges") == "bytes" def test_small_response_below_threshold_is_not_compressed(client): r = client.get("/api/tiny", headers=accept_gzip()) assert gz(r) == "" and r.content == SMALL assert int(r.headers["content-length"]) == len(SMALL) def test_client_without_gzip_support_gets_identity(client): """不收 gzip 的客户端必须拿到原文,且**不能**看到 content-encoding。""" r = client.get("/api/projects", headers={"Accept-Encoding": "identity"}) assert gz(r) == "" assert r.json() == {"raw": BIG.decode()} assert int(r.headers["content-length"]) == len(r.content) def test_already_encoded_response_is_not_double_compressed(client): """`/assets` 那一层自带 gzip,外层必须认出来并透传(叠两层 = 解压只剥一层)。""" r = client.get("/api/pre-encoded", headers=accept_gzip()) assert r.headers["content-encoding"].lower().count("gzip") == 1 assert r.json() == {"ok": True} def test_204_has_no_body_and_no_encoding(client): r = client.post("/api/no-content", headers=accept_gzip()) assert r.status_code == 204 and gz(r) == "" and r.content == b"" def test_event_stream_is_never_buffered(client): """SSE 攒成块就失去实时性(本仓现在没有,但判据要挡住将来加的人)。""" r = client.get("/api/stream", headers=accept_gzip()) assert gz(r) == "" # ------------------------------------------------- 流式分支:minimum_size 管不到,只有 media type 管得到 # 阶段 30 的 A/B(`reports/ui_evidence/nex-p30/apigate.log`)在真 app 上量到的: # `/api/exports/77` 只有 742 B、字幕 834 B,都在阈值以下却仍然被压了。 # 原因在 starlette 0.46.2 `gzip.py`:`minimum_size` 只挡「一条 body 消息且 more_body=False」 # 的一次性响应;真 app 的路由都坐在鉴权 `BaseHTTPMiddleware` 里面,它的 body 是分块转发的 # (首条 more_body=True)⇒ 走流式分支,不看体积。 # 这不是缺陷(量到的每条小 JSON 都还是变小了:742→497、834→458),但意味着 # **二进制的安全完全靠 content-type 判据,体积阈值保护不了它** —— 下面两条把这个语义钉住。 def test_streaming_small_json_is_compressed_documents_threshold_semantics(client): """流式小 JSON:体积阈值不生效,照样压(与真 app 里 742 B 的 /api/exports/77 同形)。""" r = client.get("/api/stream-json", headers=accept_gzip()) assert gz(r) == "gzip" assert r.content == SMALL def test_streaming_webp_thumbnail_is_still_untouched(client): """缩略图走的正是流式:压不压只由 media type 决定,这条是二进制的安全边界。 这里不比具体的 Content-Length(`StreamingResponse` 本来就不发它,真 thumb 路由用的 `FileResponse` 才发),比的是**同一个端点在两种 Accept-Encoding 下头必须一模一样**: 压缩层对不该压的响应必须完全隐形,一个头都不许多加、不许改写。 """ a = client.get("/api/stream-webp", headers=accept_gzip()) b = client.get("/api/stream-webp", headers={"Accept-Encoding": "identity"}) assert gz(a) == "", "流式二进制被压 ⇒ thumb 白烧 CPU 还变大" assert a.content == WEBP == b.content assert dict(a.headers) == dict(b.headers), "压缩层动了不该动的响应头" # ---------------------------------------------------------------- 判据本身 @pytest.mark.parametrize("ct,want", [ ("application/json", True), ("application/json; charset=utf-8", True), ("text/plain; charset=utf-8", True), ("text/csv; charset=utf-8", True), ("text/html", True), ("application/javascript", True), ("image/svg+xml", True), ("text/event-stream", False), ("image/png", False), ("video/mp4", False), ("audio/mpeg", False), ("application/zip", False), ("application/octet-stream", False), ("", False), ]) def test_gzip_eligible_matrix(ct, want): assert gzip_eligible(ct) is want def test_registered_inside_auth_so_the_size_threshold_is_real(): """小 app 证明行为,这一条钉真 app 的挂载位置:**压缩层在鉴权之内**, 不是越靠外越好。 阶段 30 先把它挂在最外层,结果 `BaseHTTPMiddleware` 的分块转发让每一个响应 都以 `more_body=True` 进来 ⇒ `GZIP_MINIMUM` 整体失效,4 个 <1 kB 的哈希 chunk 被压(A/B 读数 `reports/ui_evidence/nex-p30/servecheck.log`),与项目自己声明的 1 kB 底线(`SPA_GZIP_MINIMUM`、nginx `gzip_min_length`)冲突。顺序写反就是回归。 """ from app.main import app from starlette.middleware.base import BaseHTTPMiddleware from fastapi.middleware.cors import CORSMiddleware chain = [m.cls for m in app.user_middleware] assert chain == [CORSMiddleware, BaseHTTPMiddleware, ContentTypeGZipMiddleware], chain