aiohttp.web.StreamResponse

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 7

Page 1 Selected Page 2

Example 1

Project: aiohttp
License: View license
Source File: test_web_response.py
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

Example 2

Project: aiohttp
License: View license
Source File: test_web_response.py
@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

Example 3

Project: aiohttp
License: View license
Source File: test_web_response.py
@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

Example 4

Project: aiohttp
License: View license
Source File: test_web_response.py
@asyncio.coroutine
def test_write_returns_empty_tuple_on_empty_data():
    resp = StreamResponse()
    yield from resp.prepare(make_request('GET', '/'))

    assert () == resp.write(b'')

Example 5

Project: aiohttp
License: View license
Source File: test_web_response.py
@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)

Example 6

Project: aiohttp
License: View license
Source File: test_web_response.py
@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

Example 7

Project: aiohttp
License: View license
Source File: test_web_response.py
@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

Example 8

Project: aiohttp
License: View license
Source File: test_web_response.py
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']

Example 9

Project: aiohttp
License: View license
Source File: test_web_response.py
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)

Example 10

Project: aiohttp
License: View license
Source File: test_web_response.py
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

Example 11

Project: aiohttp
License: View license
Source File: test_web_response.py
@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

Example 12

Project: aiohttp
License: View license
Source File: test_web_response.py
@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)

Example 13

Project: aiohttp
License: View license
Source File: test_web_response.py
def test_last_modified_reset():
    resp = StreamResponse()

    resp.last_modified = 0
    resp.last_modified = None
    assert resp.last_modified is None

Example 14

Project: aiohttp
License: View license
Source File: test_web_response.py
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))

Example 15

Project: aiohttp
License: View license
Source File: test_web_response.py
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

Example 16

Project: aiohttp
License: View license
Source File: test_web_response.py
@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)

Example 17

Project: aiohttp
License: View license
Source File: test_web_response.py
@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))

Example 18

Project: aiohttp
License: View license
Source File: test_web_response.py
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

Example 19

Project: sockjs
License: View license
Source File: test_transport.py
    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!"]')

Example 20

Project: aiohttp
License: View license
Source File: test_web_response.py
@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

Example 21

Project: aiohttp
License: View license
Source File: test_web_response.py
@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

Example 22

Project: aiohttp
License: View license
Source File: test_web_response.py
@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)

Example 23

Project: sockjs
License: View license
Source File: test_transport.py
    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!"]')

Example 24

Project: aiohttp
License: View license
Source File: test_web_response.py
@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)

Example 25

Project: aiohttp
License: View license
Source File: test_web_response.py
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

Example 26

Project: aiohttp
License: View license
Source File: test_web_response.py
@asyncio.coroutine
def test_cannot_write_eof_before_headers():
    resp = StreamResponse()

    with pytest.raises(RuntimeError):
        yield from resp.write_eof()

Example 27

Project: aiohttp
License: View license
Source File: test_web_response.py
@asyncio.coroutine
def test_write_non_byteish():
    resp = StreamResponse()
    yield from resp.prepare(make_request('GET', '/'))

    with pytest.raises(AssertionError):
        resp.write(123)

Example 28

Project: aiohttp
License: View license
Source File: test_web_response.py
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

Example 29

Project: aiohttp
License: View license
Source File: test_web_response.py
@asyncio.coroutine
def test_write_returns_drain():
    resp = StreamResponse()
    yield from resp.prepare(make_request('GET', '/'))

    assert () == resp.write(b'data')

Example 30

Project: aiohttp
License: View license
Source File: web_srv.py
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

Example 31

Project: aiohttp
License: View license
Source File: test_web_response.py
def test_force_close():
    resp = StreamResponse()

    assert resp.keep_alive is None
    resp.force_close()
    assert resp.keep_alive is False

Example 32

Project: aiohttp
License: View license
Source File: test_web_response.py
@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)

Example 33

Project: aiohttp
License: View license
Source File: test_web_response.py
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

Example 34

Project: sockjs
License: View license
Source File: test_transport.py
    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"]')

Example 35

Project: aiohttp
License: View license
Source File: test_web_response.py
@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

Example 36

Project: aiohttp
License: View license
Source File: test_web_response.py
@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

Example 37

Project: aiohttp
License: View license
Source File: test_web_response.py
@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

Example 38

Project: aiohttp
License: View license
Source File: test_web_response.py
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

Example 39

Project: aiohttp
License: View license
Source File: test_web_response.py
@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

Example 40

Project: aiohttp
License: View license
Source File: test_web_response.py
@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

Example 41

Project: aiohttp
License: View license
Source File: test_web_response.py
@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)

Example 42

Project: vj4
License: View license
Source File: base.py
  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)

Example 43

Project: aiohttp
License: View license
Source File: test_web_response.py
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)

Example 44

Project: aiohttp
License: View license
Source File: test_web_response.py
@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

Example 45

Project: aiohttp
License: View license
Source File: test_web_response.py
def test_reset_charset():
    resp = StreamResponse()

    resp.content_type = 'text/html'
    resp.charset = None
    assert resp.charset is None

Example 46

Project: hammock
License: View license
Source File: _aweb.py
    @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,
            )

Example 47

Project: sockjs
License: View license
Source File: eventsource.py
    @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

Example 48

Project: aiohttp
License: View license
Source File: web_srv.py
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

Example 49

Project: aiohttp
License: View license
Source File: test_web_response.py
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

Example 50

Project: aiohttp
License: View license
Source File: test_web_response.py
@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
See More Examples - Go to Next Page
Page 1 Selected Page 2