-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
option to disable automatic client response body decompression #2110
Changes from 19 commits
a647b47
dd56884
5d681f6
2b90858
b16fafe
d78d618
72fe4c4
201cfc3
115e414
5559504
109c229
5f025a4
406db0a
7f451ef
1e482da
22c8ba7
2f57548
979585b
15030ba
7a9240a
75f7814
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add ability to disable automatic response decompression |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import asyncio | ||
import gzip | ||
from unittest import mock | ||
|
||
import pytest | ||
|
@@ -14,11 +15,20 @@ | |
teardown_test_loop, unittest_run_loop) | ||
|
||
|
||
def _create_example_app(): | ||
_hello_world_str = "Hello, world" | ||
_hello_world_bytes = _hello_world_str.encode('utf-8') | ||
_hello_world_gz = gzip.compress(_hello_world_bytes) | ||
|
||
|
||
def _create_example_app(): | ||
@asyncio.coroutine | ||
def hello(request): | ||
return web.Response(body=b"Hello, world") | ||
return web.Response(body=_hello_world_bytes) | ||
|
||
@asyncio.coroutine | ||
def gzip_hello(request): | ||
return web.Response(body=_hello_world_gz, | ||
headers={'Content-Encoding': 'gzip'}) | ||
|
||
@asyncio.coroutine | ||
def websocket_handler(request): | ||
|
@@ -36,12 +46,13 @@ def websocket_handler(request): | |
|
||
@asyncio.coroutine | ||
def cookie_handler(request): | ||
resp = web.Response(body=b"Hello, world") | ||
resp = web.Response(body=_hello_world_bytes) | ||
resp.set_cookie('cookie', 'val') | ||
return resp | ||
|
||
app = web.Application() | ||
app.router.add_route('*', '/', hello) | ||
app.router.add_route('*', '/gzip_hello', gzip_hello) | ||
app.router.add_route('*', '/websocket', websocket_handler) | ||
app.router.add_route('*', '/cookie', cookie_handler) | ||
return app | ||
|
@@ -58,7 +69,39 @@ def test_get_route(): | |
resp = yield from client.request("GET", "/") | ||
assert resp.status == 200 | ||
text = yield from resp.text() | ||
assert "Hello, world" in text | ||
assert _hello_world_str == text | ||
|
||
loop.run_until_complete(test_get_route()) | ||
|
||
|
||
def test_auto_gzip_decompress(): | ||
with loop_context() as loop: | ||
app = _create_example_app() | ||
with _TestClient(_TestServer(app, loop=loop), loop=loop) as client: | ||
|
||
@asyncio.coroutine | ||
def test_get_route(): | ||
nonlocal client | ||
resp = yield from client.request("GET", "/gzip_hello") | ||
assert resp.status == 200 | ||
data = yield from resp.read() | ||
assert data == _hello_world_bytes | ||
|
||
loop.run_until_complete(test_get_route()) | ||
|
||
|
||
def test_noauto_gzip_decompress(): | ||
with loop_context() as loop: | ||
app = _create_example_app() | ||
with _TestClient(_TestServer(app, loop=loop), auto_decompress=False) as client: | ||
|
||
@asyncio.coroutine | ||
def test_get_route(): | ||
nonlocal client | ||
resp = yield from client.request("GET", "/gzip_hello") | ||
assert resp.status == 200 | ||
data = yield from resp.read() | ||
assert data == _hello_world_gz | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test coroutine is duplicated at a lot of different places. Maybe you can make a helper outside the test and provide the client instance in argument. I think it would be more readable. What do you think? async def _assertBody(client, url, body):
resp = await ...
assert resp.status == 200
data = await ...
assert data == body There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been avoiding too much refactoring because @asvetlov has frowned upon that in the past in PRs that implement new features. He's wanted these types of changes in multiple PRs. If he gives the go-ahead I can do these suggested changes. |
||
|
||
loop.run_until_complete(test_get_route()) | ||
|
||
|
@@ -73,7 +116,7 @@ def test_get_route(): | |
resp = yield from client.request("GET", "/") | ||
assert resp.status == 200 | ||
text = yield from resp.text() | ||
assert "Hello, world" in text | ||
assert _hello_world_str == text | ||
|
||
loop.run_until_complete(test_get_route()) | ||
|
||
|
@@ -102,15 +145,15 @@ def test_example_with_loop(self): | |
request = yield from self.client.request("GET", "/") | ||
assert request.status == 200 | ||
text = yield from request.text() | ||
assert "Hello, world" in text | ||
assert _hello_world_str == text | ||
|
||
def test_example(self): | ||
@asyncio.coroutine | ||
def test_get_route(): | ||
resp = yield from self.client.request("GET", "/") | ||
assert resp.status == 200 | ||
text = yield from resp.text() | ||
assert "Hello, world" in text | ||
assert _hello_world_str == text | ||
|
||
self.loop.run_until_complete(test_get_route()) | ||
|
||
|
@@ -141,7 +184,7 @@ def test_get_route(): | |
resp = yield from test_client.request("GET", "/") | ||
assert resp.status == 200 | ||
text = yield from resp.text() | ||
assert "Hello, world" in text | ||
assert _hello_world_str == text | ||
|
||
loop.run_until_complete(test_get_route()) | ||
|
||
|
@@ -176,7 +219,7 @@ def test_test_client_methods(method, loop, test_client): | |
resp = yield from getattr(test_client, method)("/") | ||
assert resp.status == 200 | ||
text = yield from resp.text() | ||
assert "Hello, world" in text | ||
assert _hello_world_str == text | ||
|
||
|
||
@asyncio.coroutine | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this rate you can even generate a random string. Like:
I would actually even put the str, bytes and gzip inside the app instance: