Here are the examples of the python api aiohttp.web.StreamResponse taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
81 Examples
3
Example 1
def test_set_status_with_reason():
resp = StreamResponse()
resp.set_status(200, "Everithing is fine!")
assert 200 == resp.status
assert "Everithing is fine!" == resp.reason
3
Example 2
@asyncio.coroutine
def test_get_nodelay_prepared():
resp = StreamResponse()
writer = mock.Mock()
writer.tcp_nodelay = False
req = make_request('GET', '/', writer=writer)
yield from resp.prepare(req)
assert not resp.tcp_nodelay
3
Example 3
@asyncio.coroutine
def test_force_compression_no_accept_backwards_compat():
req = make_request('GET', '/')
resp = StreamResponse()
assert not resp.chunked
assert not resp.compression
resp.enable_compression(force=True)
assert resp.compression
with mock.patch('aiohttp.web_reqrep.ResponseImpl'):
msg = yield from resp.prepare(req)
assert msg.add_compression_filter.called
assert msg.filter is not None
3
Example 4
@asyncio.coroutine
def test_write_returns_empty_tuple_on_empty_data():
resp = StreamResponse()
yield from resp.prepare(make_request('GET', '/'))
assert () == resp.write(b'')
3
Example 5
@asyncio.coroutine
def test_force_compression_no_accept_deflate():
req = make_request('GET', '/')
resp = StreamResponse()
resp.enable_compression(ContentCoding.deflate)
assert resp.compression
with mock.patch('aiohttp.web_reqrep.ResponseImpl'):
msg = yield from resp.prepare(req)
msg.add_compression_filter.assert_called_with('deflate')
assert 'deflate' == resp.headers.get(hdrs.CONTENT_ENCODING)
3
Example 6
@asyncio.coroutine
def test_chunked_encoding():
req = make_request('GET', '/')
resp = StreamResponse()
assert not resp.chunked
resp.enable_chunked_encoding()
assert resp.chunked
with mock.patch('aiohttp.web_reqrep.ResponseImpl'):
msg = yield from resp.prepare(req)
assert msg.chunked
3
Example 7
@asyncio.coroutine
def test_keep_alive_http10_switched_on():
headers = CIMultiDict(Connection='keep-alive')
req = make_request('GET', '/', version=HttpVersion10, headers=headers)
req._message = req._message._replace(should_close=False)
resp = StreamResponse()
yield from resp.prepare(req)
assert resp.keep_alive
3
Example 8
def test_setting_charset():
resp = StreamResponse()
resp.content_type = 'text/html'
resp.charset = 'koi8-r'
assert 'text/html; charset=koi8-r' == resp.headers['content-type']
3
Example 9
def test_set_cork_prepared():
resp = StreamResponse()
writer = mock.Mock()
req = make_request('GET', '/', writer=writer)
yield from resp.prepare(req)
resp.set_tcp_cork(True)
writer.set_tcp_cork.assert_called_with(True)
3
Example 10
def test_reset_charset_after_setting():
resp = StreamResponse()
resp.content_type = 'text/html'
resp.charset = 'koi8-r'
resp.charset = None
assert resp.charset is None
3
Example 11
@asyncio.coroutine
def test_cannot_write_eof_twice():
resp = StreamResponse()
writer = mock.Mock()
yield from resp.prepare(make_request('GET', '/', writer=writer))
resp.write(b'data')
writer.drain.return_value = ()
yield from resp.write_eof()
assert writer.write.called
writer.write.reset_mock()
yield from resp.write_eof()
assert not writer.write.called
3
Example 12
@asyncio.coroutine
def test_force_compression_no_accept_gzip():
req = make_request('GET', '/')
resp = StreamResponse()
resp.enable_compression(ContentCoding.gzip)
assert resp.compression
with mock.patch('aiohttp.web_reqrep.ResponseImpl'):
msg = yield from resp.prepare(req)
msg.add_compression_filter.assert_called_with('gzip')
assert 'gzip' == resp.headers.get(hdrs.CONTENT_ENCODING)
3
Example 13
def test_last_modified_reset():
resp = StreamResponse()
resp.last_modified = 0
resp.last_modified = None
assert resp.last_modified is None
3
Example 14
def test_response_cookie__issue_del_cookie():
resp = StreamResponse()
assert resp.cookies == {}
assert str(resp.cookies) == ''
resp.del_cookie('name')
expected = ('Set-Cookie: name=("")?; '
'expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/')
assert re.match(expected, str(resp.cookies))
3
Example 15
def test_drop_content_length_header_on_setting_len_to_None():
resp = StreamResponse()
resp.content_length = 1
assert "1" == resp.headers['Content-Length']
resp.content_length = None
assert 'Content-Length' not in resp.headers
3
Example 16
@asyncio.coroutine
def test___repr__():
req = make_request('GET', '/path/to')
resp = StreamResponse(reason=301)
yield from resp.prepare(req)
assert "<StreamResponse 301 GET /path/to >" == repr(resp)
3
Example 17
@asyncio.coroutine
def test_chunked_encoding_forbidden_for_http_10():
req = make_request('GET', '/', version=HttpVersion10)
resp = StreamResponse()
resp.enable_chunked_encoding()
with pytest.raises(RuntimeError) as ctx:
yield from resp.prepare(req)
assert re.match("Using chunked encoding is forbidden for HTTP/1.0",
str(ctx.value))
3
Example 18
def test_start_twice():
req = make_request('GET', '/')
resp = StreamResponse()
with pytest.warns(DeprecationWarning):
impl1 = resp.start(req)
impl2 = resp.start(req)
assert impl1 is impl2
3
Example 19
def test_handle_session_closing(self):
trans = self.make_transport()
trans.send = self.make_fut(1)
trans.session.interrupted = False
trans.session.state = protocol.STATE_CLOSING
trans.session._remote_closed = self.make_fut(1)
trans.response = web.StreamResponse()
self.loop.run_until_complete(trans.handle_session())
trans.session._remote_closed.assert_called_with()
trans.send.assert_called_with('c[3000,"Go away!"]')
3
Example 20
@asyncio.coroutine
def test_get_cork_prepared():
resp = StreamResponse()
writer = mock.Mock()
writer.tcp_cork = False
req = make_request('GET', '/', writer=writer)
yield from resp.prepare(req)
assert not resp.tcp_cork
3
Example 21
@asyncio.coroutine
def test_compression_default_coding():
req = make_request(
'GET', '/',
headers=CIMultiDict({hdrs.ACCEPT_ENCODING: 'gzip, deflate'}))
resp = StreamResponse()
assert not resp.chunked
assert not resp.compression
resp.enable_compression()
assert resp.compression
with mock.patch('aiohttp.web_reqrep.ResponseImpl'):
msg = yield from resp.prepare(req)
msg.add_compression_filter.assert_called_with('deflate')
assert 'deflate' == resp.headers.get(hdrs.CONTENT_ENCODING)
assert msg.filter is not None
3
Example 22
@asyncio.coroutine
def test_force_compression_deflate():
req = make_request(
'GET', '/',
headers=CIMultiDict({hdrs.ACCEPT_ENCODING: 'gzip, deflate'}))
resp = StreamResponse()
resp.enable_compression(ContentCoding.deflate)
assert resp.compression
with mock.patch('aiohttp.web_reqrep.ResponseImpl'):
msg = yield from resp.prepare(req)
msg.add_compression_filter.assert_called_with('deflate')
assert 'deflate' == resp.headers.get(hdrs.CONTENT_ENCODING)
3
Example 23
def test_handle_session_closed(self):
trans = self.make_transport()
trans.send = self.make_fut(1)
trans.session.interrupted = False
trans.session.state = protocol.STATE_CLOSED
trans.session._remote_closed = self.make_fut(1)
trans.response = web.StreamResponse()
self.loop.run_until_complete(trans.handle_session())
trans.session._remote_closed.assert_called_with()
trans.send.assert_called_with('c[3000,"Go away!"]')
3
Example 24
@asyncio.coroutine
def test_force_compression_gzip():
req = make_request(
'GET', '/',
headers=CIMultiDict({hdrs.ACCEPT_ENCODING: 'gzip, deflate'}))
resp = StreamResponse()
resp.enable_compression(ContentCoding.gzip)
assert resp.compression
with mock.patch('aiohttp.web_reqrep.ResponseImpl'):
msg = yield from resp.prepare(req)
msg.add_compression_filter.assert_called_with('gzip')
assert 'gzip' == resp.headers.get(hdrs.CONTENT_ENCODING)
3
Example 25
def test_last_modified_string():
resp = StreamResponse()
dt = datetime.datetime(1990, 1, 2, 3, 4, 5, 0, datetime.timezone.utc)
resp.last_modified = 'Mon, 2 Jan 1990 03:04:05 GMT'
assert resp.last_modified == dt
3
Example 26
@asyncio.coroutine
def test_cannot_write_eof_before_headers():
resp = StreamResponse()
with pytest.raises(RuntimeError):
yield from resp.write_eof()
3
Example 27
@asyncio.coroutine
def test_write_non_byteish():
resp = StreamResponse()
yield from resp.prepare(make_request('GET', '/'))
with pytest.raises(AssertionError):
resp.write(123)
3
Example 28
def test_last_modified_datetime():
resp = StreamResponse()
dt = datetime.datetime(2001, 2, 3, 4, 5, 6, 0, datetime.timezone.utc)
resp.last_modified = dt
assert resp.last_modified == dt
3
Example 29
@asyncio.coroutine
def test_write_returns_drain():
resp = StreamResponse()
yield from resp.prepare(make_request('GET', '/'))
assert () == resp.write(b'data')
3
Example 30
async def hello(request):
resp = StreamResponse()
name = request.match_info.get('name', 'Anonymous')
answer = ('Hello, ' + name).encode('utf8')
resp.content_length = len(answer)
resp.content_type = 'text/plain'
await resp.prepare(request)
resp.write(answer)
await resp.write_eof()
return resp
3
Example 31
def test_force_close():
resp = StreamResponse()
assert resp.keep_alive is None
resp.force_close()
assert resp.keep_alive is False
3
Example 32
@asyncio.coroutine
def test_start():
req = make_request('GET', '/')
resp = StreamResponse()
assert resp.keep_alive is None
with mock.patch('aiohttp.web_reqrep.ResponseImpl'):
msg = yield from resp.prepare(req)
assert msg.send_headers.called
msg2 = yield from resp.prepare(req)
assert msg is msg2
assert resp.keep_alive
req2 = make_request('GET', '/')
with pytest.raises(RuntimeError):
yield from resp.prepare(req2)
3
Example 33
def test_cookie_set_after_del():
resp = StreamResponse()
resp.del_cookie('name')
resp.set_cookie('name', 'val')
# check for Max-Age dropped
expected = 'Set-Cookie: name=val; Path=/'
assert str(resp.cookies) == expected
3
Example 34
def test_handle_session_interrupted(self):
trans = self.make_transport()
trans.session.interrupted = True
trans.send = self.make_fut(1)
trans.response = web.StreamResponse()
self.loop.run_until_complete(trans.handle_session())
trans.send.assert_called_with('c[1002,"Connection interrupted"]')
3
Example 35
@asyncio.coroutine
def test_start_force_close():
req = make_request('GET', '/')
resp = StreamResponse()
resp.force_close()
assert not resp.keep_alive
msg = yield from resp.prepare(req)
assert not resp.keep_alive
assert msg.closing
3
Example 36
@asyncio.coroutine
def test_chunk_size():
req = make_request('GET', '/')
resp = StreamResponse()
assert not resp.chunked
resp.enable_chunked_encoding(chunk_size=8192)
assert resp.chunked
with mock.patch('aiohttp.web_reqrep.ResponseImpl'):
msg = yield from resp.prepare(req)
assert msg.chunked
msg.add_chunking_filter.assert_called_with(8192)
assert msg.filter is not None
3
Example 37
@asyncio.coroutine
def test_keep_alive_http10_default():
req = make_request('GET', '/', version=HttpVersion10)
resp = StreamResponse()
yield from resp.prepare(req)
assert not resp.keep_alive
3
Example 38
def test_set_content_length_to_None_on_non_set():
resp = StreamResponse()
resp.content_length = None
assert 'Content-Length' not in resp.headers
resp.content_length = None
assert 'Content-Length' not in resp.headers
3
Example 39
@asyncio.coroutine
def test_keep_alive_http09():
headers = CIMultiDict(Connection='keep-alive')
req = make_request('GET', '/', version=HttpVersion(0, 9), headers=headers)
resp = StreamResponse()
yield from resp.prepare(req)
assert not resp.keep_alive
3
Example 40
@asyncio.coroutine
def test_compression_no_accept():
req = make_request('GET', '/')
resp = StreamResponse()
assert not resp.chunked
assert not resp.compression
resp.enable_compression()
assert resp.compression
with mock.patch('aiohttp.web_reqrep.ResponseImpl'):
msg = yield from resp.prepare(req)
assert not msg.add_compression_filter.called
3
Example 41
@asyncio.coroutine
def test_prepare_calls_signal():
app = mock.Mock()
req = make_request('GET', '/', app=app)
resp = StreamResponse()
sig = mock.Mock()
app.on_response_prepare.append(sig)
yield from resp.prepare(req)
sig.assert_called_with(req, resp)
3
Example 42
async def binary(self, data, type='application/octet-stream'):
self.response = web.StreamResponse()
self.response.content_length = len(data)
self.response.content_type = type
await self.response.prepare(self.request)
self.response.write(data)
3
Example 43
def test_set_nodelay_prepared():
resp = StreamResponse()
writer = mock.Mock()
req = make_request('GET', '/', writer=writer)
yield from resp.prepare(req)
resp.set_tcp_nodelay(True)
writer.set_tcp_nodelay.assert_called_with(True)
3
Example 44
@asyncio.coroutine
def test_force_compression_false_backwards_compat():
req = make_request('GET', '/')
resp = StreamResponse()
assert not resp.compression
resp.enable_compression(force=False)
assert resp.compression
with mock.patch('aiohttp.web_reqrep.ResponseImpl'):
msg = yield from resp.prepare(req)
assert not msg.add_compression_filter.called
3
Example 45
def test_reset_charset():
resp = StreamResponse()
resp.content_type = 'text/html'
resp.charset = None
assert resp.charset is None
3
Example 46
@staticmethod
def _to_backend_resp(resp):
if resp.is_stream:
return aweb.StreamResponse(
)
else:
return aweb.Response(
body=resp.content,
status=resp.status,
headers=resp.headers,
)
3
Example 47
@asyncio.coroutine
def process(self):
headers = list(
((hdrs.CONTENT_TYPE, 'text/event-stream; charset=UTF-8'),
(hdrs.CACHE_CONTROL,
'no-store, no-cache, must-revalidate, max-age=0')) +
session_cookie(self.request))
# open sequence (sockjs protocol)
resp = self.response = web.StreamResponse(headers=headers)
yield from resp.prepare(self.request)
resp.write(b'\r\n')
# handle session
yield from self.handle_session()
return resp
3
Example 48
async def intro(request):
txt = textwrap.dedent("""\
Type {url}/hello/John {url}/simple or {url}/change_body
in browser url bar
""").format(url='127.0.0.1:8080')
binary = txt.encode('utf8')
resp = StreamResponse()
resp.content_length = len(binary)
resp.content_type = 'text/plain'
await resp.prepare(request)
resp.write(binary)
return resp
3
Example 49
def test_last_modified_timestamp():
resp = StreamResponse()
dt = datetime.datetime(1970, 1, 1, 0, 0, 0, 0, datetime.timezone.utc)
resp.last_modified = 0
assert resp.last_modified == dt
resp.last_modified = 0.0
assert resp.last_modified == dt
3
Example 50
@asyncio.coroutine
def test_cannot_write_after_eof():
resp = StreamResponse()
writer = mock.Mock()
yield from resp.prepare(make_request('GET', '/', writer=writer))
resp.write(b'data')
writer.drain.return_value = ()
yield from resp.write_eof()
writer.write.reset_mock()
with pytest.raises(RuntimeError):
resp.write(b'next data')
assert not writer.write.called