1
1
import asyncio
2
+ import gzip
2
3
from unittest import mock
3
4
4
5
import pytest
14
15
teardown_test_loop , unittest_run_loop )
15
16
16
17
17
- def _create_example_app ():
18
+ _hello_world_str = "Hello, world"
19
+ _hello_world_bytes = _hello_world_str .encode ('utf-8' )
20
+ _hello_world_gz = gzip .compress (_hello_world_bytes )
21
+
18
22
23
+ def _create_example_app ():
19
24
@asyncio .coroutine
20
25
def hello (request ):
21
- return web .Response (body = b"Hello, world" )
26
+ return web .Response (body = _hello_world_bytes )
27
+
28
+ @asyncio .coroutine
29
+ def gzip_hello (request ):
30
+ return web .Response (body = _hello_world_gz ,
31
+ headers = {'Content-Encoding' : 'gzip' })
22
32
23
33
@asyncio .coroutine
24
34
def websocket_handler (request ):
@@ -36,12 +46,13 @@ def websocket_handler(request):
36
46
37
47
@asyncio .coroutine
38
48
def cookie_handler (request ):
39
- resp = web .Response (body = b"Hello, world" )
49
+ resp = web .Response (body = _hello_world_bytes )
40
50
resp .set_cookie ('cookie' , 'val' )
41
51
return resp
42
52
43
53
app = web .Application ()
44
54
app .router .add_route ('*' , '/' , hello )
55
+ app .router .add_route ('*' , '/gzip_hello' , gzip_hello )
45
56
app .router .add_route ('*' , '/websocket' , websocket_handler )
46
57
app .router .add_route ('*' , '/cookie' , cookie_handler )
47
58
return app
@@ -58,7 +69,40 @@ def test_get_route():
58
69
resp = yield from client .request ("GET" , "/" )
59
70
assert resp .status == 200
60
71
text = yield from resp .text ()
61
- assert "Hello, world" in text
72
+ assert _hello_world_str == text
73
+
74
+ loop .run_until_complete (test_get_route ())
75
+
76
+
77
+ def test_auto_gzip_decompress ():
78
+ with loop_context () as loop :
79
+ app = _create_example_app ()
80
+ with _TestClient (_TestServer (app , loop = loop ), loop = loop ) as client :
81
+
82
+ @asyncio .coroutine
83
+ def test_get_route ():
84
+ nonlocal client
85
+ resp = yield from client .request ("GET" , "/gzip_hello" )
86
+ assert resp .status == 200
87
+ data = yield from resp .read ()
88
+ assert data == _hello_world_bytes
89
+
90
+ loop .run_until_complete (test_get_route ())
91
+
92
+
93
+ def test_noauto_gzip_decompress ():
94
+ with loop_context () as loop :
95
+ app = _create_example_app ()
96
+ with _TestClient (_TestServer (app , loop = loop ), loop = loop ,
97
+ auto_decompress = False ) as client :
98
+
99
+ @asyncio .coroutine
100
+ def test_get_route ():
101
+ nonlocal client
102
+ resp = yield from client .request ("GET" , "/gzip_hello" )
103
+ assert resp .status == 200
104
+ data = yield from resp .read ()
105
+ assert data == _hello_world_gz
62
106
63
107
loop .run_until_complete (test_get_route ())
64
108
@@ -73,7 +117,7 @@ def test_get_route():
73
117
resp = yield from client .request ("GET" , "/" )
74
118
assert resp .status == 200
75
119
text = yield from resp .text ()
76
- assert "Hello, world" in text
120
+ assert _hello_world_str == text
77
121
78
122
loop .run_until_complete (test_get_route ())
79
123
@@ -102,15 +146,15 @@ def test_example_with_loop(self):
102
146
request = yield from self .client .request ("GET" , "/" )
103
147
assert request .status == 200
104
148
text = yield from request .text ()
105
- assert "Hello, world" in text
149
+ assert _hello_world_str == text
106
150
107
151
def test_example (self ):
108
152
@asyncio .coroutine
109
153
def test_get_route ():
110
154
resp = yield from self .client .request ("GET" , "/" )
111
155
assert resp .status == 200
112
156
text = yield from resp .text ()
113
- assert "Hello, world" in text
157
+ assert _hello_world_str == text
114
158
115
159
self .loop .run_until_complete (test_get_route ())
116
160
@@ -141,7 +185,7 @@ def test_get_route():
141
185
resp = yield from test_client .request ("GET" , "/" )
142
186
assert resp .status == 200
143
187
text = yield from resp .text ()
144
- assert "Hello, world" in text
188
+ assert _hello_world_str == text
145
189
146
190
loop .run_until_complete (test_get_route ())
147
191
@@ -176,7 +220,7 @@ def test_test_client_methods(method, loop, test_client):
176
220
resp = yield from getattr (test_client , method )("/" )
177
221
assert resp .status == 200
178
222
text = yield from resp .text ()
179
- assert "Hello, world" in text
223
+ assert _hello_world_str == text
180
224
181
225
182
226
@asyncio .coroutine
0 commit comments