def test_filelike(): s = asgi.BoundedStream(testing.ASGIRequestEventEmitter()) for __ in range(2): with pytest.raises(OSError): s.fileno() assert not s.isatty() assert s.readable() assert not s.seekable() assert not s.writable() s.close() assert s.closed # NOTE(kgriffs): Closing an already-closed stream is a noop. s.close() assert s.closed async def test_iteration(): with pytest.raises(ValueError): await s.read() with pytest.raises(ValueError): await s.readall() with pytest.raises(ValueError): await s.exhaust() with pytest.raises(ValueError): async for chunk in s: pass falcon.invoke_coroutine_sync(test_iteration)
async def t(): emitter = testing.ASGIRequestEventEmitter(b'123456798' * 1024, disconnect_at=(time.time() + 0.5)) s = asgi.BoundedStream(emitter) assert await s.read(1) == b'1' assert await s.read(2) == b'23' await asyncio.sleep(0.5) await s.exhaust() assert await s.read(1) == b'' assert await s.read(100) == b'' assert s.eof
async def test_exhaust(): emitter = testing.ASGIRequestEventEmitter(b'123456798' * 1024) stream = asgi.BoundedStream(emitter) assert await stream.read(1) == b'1' assert await stream.read(6) == b'234567' assert await stream.read(101) == b'98' + b'123456798' * 11 await stream.exhaust() assert await stream.read(1) == b'' assert await stream.read(6) == b'' assert await stream.read(101) == b'' assert stream.eof
async def t(): emitter = testing.ASGIRequestEventEmitter( b'123456789' * 2, # NOTE(kgriffs): This must be small enough to create several events chunk_size=3, ) s = asgi.BoundedStream(emitter) assert await s.read(1) == b'1' assert await s.read(2) == b'23' emitter.disconnect(exhaust_body=False) await s.exhaust() assert await s.read(1) == b'' assert await s.read(100) == b'' assert s.eof
async def test_iterate_streaming_request(): events = iter(( { 'type': 'http.request', 'body': b'Hello, ', 'more_body': True }, { 'type': 'http.request', 'body': b'World', 'more_body': True }, { 'type': 'http.request', 'body': b'!\n', 'more_body': True }, { 'type': 'http.request', 'body': b'', 'more_body': False }, { 'type': 'http.disconnect' }, )) async def receive(): event = next(events) assert (event['type'] != 'http.disconnect'), 'would hang until the client times out' return event s = asgi.BoundedStream(receive) assert b''.join([chunk async for chunk in s]) == b'Hello, World!\n'
def _stream(body, content_length=None): emitter = testing.ASGIRequestEventEmitter(body) return asgi.BoundedStream(emitter, content_length=content_length)