-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtests.py
192 lines (141 loc) · 5.48 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""Main tests for brotli middleware.
Some of these tests are the same as the ones from starlette.tests.middleware.test_gzip
but using brotli instead.
"""
import gzip
import io
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse, StreamingResponse, JSONResponse, Response
from starlette.testclient import TestClient
from brotli_asgi import BrotliMiddleware
def test_brotli_responses():
app = Starlette()
app.add_middleware(BrotliMiddleware)
@app.route("/")
def homepage(request):
return PlainTextResponse("x" * 4000, status_code=200)
client = TestClient(app)
response = client.get("/", headers={"accept-encoding": "br"})
assert response.status_code == 200
assert response.text == "x" * 4000
assert response.headers["Content-Encoding"] == "br"
assert int(response.headers["Content-Length"]) < 4000
def test_brotli_not_in_accept_encoding():
app = Starlette()
app.add_middleware(BrotliMiddleware)
@app.route("/")
def homepage(request):
return PlainTextResponse("x" * 4000, status_code=200)
client = TestClient(app)
response = client.get("/", headers={"accept-encoding": "identity"})
assert response.status_code == 200
assert response.text == "x" * 4000
assert "Content-Encoding" not in response.headers
assert int(response.headers["Content-Length"]) == 4000
def test_brotli_ignored_for_small_responses():
app = Starlette()
app.add_middleware(BrotliMiddleware)
@app.route("/")
def homepage(request):
return PlainTextResponse("OK", status_code=200)
client = TestClient(app)
response = client.get("/", headers={"accept-encoding": "br"})
assert response.status_code == 200
assert response.text == "OK"
assert "Content-Encoding" not in response.headers
assert int(response.headers["Content-Length"]) == 2
def test_brotli_streaming_response():
app = Starlette()
app.add_middleware(BrotliMiddleware)
@app.route("/")
def homepage(request):
async def generator(bytes, count):
for _ in range(count):
yield bytes
streaming = generator(bytes=b"x" * 400, count=10)
return StreamingResponse(streaming, status_code=200)
client = TestClient(app)
response = client.get("/", headers={"accept-encoding": "br"})
assert response.status_code == 200
assert response.text == "x" * 4000
assert response.headers["Content-Encoding"] == "br"
assert "Content-Length" not in response.headers
def test_brotli_api_options():
"""Tests default values overriding."""
app = Starlette()
app.add_middleware(
BrotliMiddleware,
quality=11,
mode="text",
lgwin=20,
lgblock=16,
)
@app.route("/")
def homepage(request):
return JSONResponse({"data": "a" * 4000}, status_code=200)
client = TestClient(app)
response = client.get("/", headers={"accept-encoding": "br"})
assert response.status_code == 200
def test_gzip_fallback():
app = Starlette()
app.add_middleware(BrotliMiddleware, gzip_fallback=True)
@app.route("/")
def homepage(request):
return PlainTextResponse("x" * 4000, status_code=200)
client = TestClient(app)
response = client.get("/", headers={"accept-encoding": "gzip"})
assert response.status_code == 200
assert response.text == "x" * 4000
assert response.headers["Content-Encoding"] == "gzip"
assert int(response.headers["Content-Length"]) < 4000
def test_gzip_fallback_false():
app = Starlette()
app.add_middleware(BrotliMiddleware, gzip_fallback=False)
@app.route("/")
def homepage(request):
return PlainTextResponse("x" * 4000, status_code=200)
client = TestClient(app)
response = client.get("/", headers={"accept-encoding": "gzip"})
assert response.status_code == 200
assert response.text == "x" * 4000
assert "Content-Encoding" not in response.headers
assert int(response.headers["Content-Length"]) == 4000
def test_excluded_handlers():
app = Starlette()
app.add_middleware(
BrotliMiddleware,
excluded_handlers=["/excluded"],
)
@app.route("/excluded")
def homepage(request):
return PlainTextResponse("x" * 4000, status_code=200)
client = TestClient(app)
response = client.get("/excluded", headers={"accept-encoding": "br"})
assert response.status_code == 200
assert response.text == "x" * 4000
assert "Content-Encoding" not in response.headers
assert int(response.headers["Content-Length"]) == 4000
def test_brotli_avoids_double_encoding():
# See https://github.com/encode/starlette/pull/1901
app = Starlette()
app.add_middleware(BrotliMiddleware, minimum_size=1)
@app.route("/")
def homepage(request):
gzip_buffer = io.BytesIO()
gzip_file = gzip.GzipFile(mode="wb", fileobj=gzip_buffer)
gzip_file.write(b"hello world" * 200)
gzip_file.close()
body = gzip_buffer.getvalue()
return Response(
body,
headers={
"content-encoding": "gzip",
"x-gzipped-content-length": str(len(body))
}
)
client = TestClient(app)
response = client.get("/", headers={"accept-encoding": "br"})
assert response.status_code == 200
assert response.text == "hello world" * 200
assert response.headers["Content-Encoding"] == "gzip"
assert response.headers["Content-Length"] == response.headers["x-gzipped-content-length"]