Skip to content

Commit 698eb33

Browse files
committed
Add test for double encoding, source: fullonic/brotli-asgi#24
1 parent d10b35d commit 698eb33

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

tests.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
"""Main test for zstd middleware.
1+
"""Main tests for zstd middleware.
22
3-
This tests are the same as the ones from starlette.tests.middleware.test_gzip
3+
Some of these tests are the same as the ones from starlette.tests.middleware.test_gzip
44
but using zstd instead.
55
"""
66
import functools
7+
import gzip
8+
import io
79

810
import pytest
911

1012
from starlette.applications import Starlette
1113
from starlette.responses import (
1214
JSONResponse,
1315
PlainTextResponse,
16+
Response,
1417
StreamingResponse,
1518
)
1619
from starlette.testclient import TestClient
@@ -153,3 +156,33 @@ def homepage(request):
153156
assert response.text == "x" * 4000
154157
assert "Content-Encoding" not in response.headers
155158
assert int(response.headers["Content-Length"]) == 4000
159+
160+
161+
def test_zstd_avoids_double_encoding():
162+
# See https://github.com/encode/starlette/pull/1901
163+
164+
app = Starlette()
165+
166+
app.add_middleware(ZstdMiddleware, minimum_size=1)
167+
168+
@app.route("/")
169+
def homepage(request):
170+
gzip_buffer = io.BytesIO()
171+
gzip_file = gzip.GzipFile(mode="wb", fileobj=gzip_buffer)
172+
gzip_file.write(b"hello world" * 200)
173+
gzip_file.close()
174+
body = gzip_buffer.getvalue()
175+
return Response(
176+
body,
177+
headers={
178+
"content-encoding": "gzip",
179+
"x-gzipped-content-length": str(len(body))
180+
}
181+
)
182+
183+
client = TestClient(app)
184+
response = client.get("/", headers={"accept-encoding": "zstd"})
185+
assert response.status_code == 200
186+
assert response.text == "hello world" * 200
187+
assert response.headers["Content-Encoding"] == "gzip"
188+
assert response.headers["Content-Length"] == response.headers["x-gzipped-content-length"]

0 commit comments

Comments
 (0)