def test_streaming_line_iterator_keepends(self): body = six.BytesIO(b'1234567890\n1234567890\n12345') stream = response.StreamingBody(body, content_length=27) self.assert_lines( stream.iter_lines(keepends=True), [b'1234567890\n', b'1234567890\n', b'12345'], )
def test_streaming_line_iterator(self): body = BytesIO(b'1234567890\n1234567890\n12345') stream = response.StreamingBody(body, content_length=27) self.assert_lines( stream.iter_lines(), [b'1234567890', b'1234567890', b'12345'], )
def test_streaming_body_readable(self): body = BytesIO(b'1234567890') stream = response.StreamingBody(body, content_length=10) self.assertTrue(stream.readable()) stream.close() with self.assertRaises(ValueError): stream.readable()
def test_streaming_body_readline(self): body = BytesIO(b'1234567890\n1234567\n12345\n') stream = response.StreamingBody(body, content_length=25) chunk = stream.readline() self.assertEqual(chunk, b'1234567890\n') chunk = stream.readline() self.assertEqual(chunk, b'1234567\n')
def test_streaming_line_iterator_ends_newline(self): body = six.BytesIO(b'1234567890\n1234567890\n12345\n') stream = response.StreamingBody(body, content_length=28) self.assert_lines( stream.iter_lines(), [b'1234567890', b'1234567890', b'12345'], )
def test_streaming_body_with_invalid_length(self): body = six.BytesIO(b'123456789') stream = response.StreamingBody(body, content_length=10) with self.assertRaises(IncompleteReadError): self.assertEqual(stream.read(9), b'123456789') # The next read will have nothing returned and raise # an IncompleteReadError because we were expectd 10 bytes, not 9. stream.read()
def test_streaming_line_iter_chunk_sizes(self): for chunk_size in range(1, 30): body = six.BytesIO(b'1234567890\n1234567890\n12345') stream = response.StreamingBody(body, content_length=27) self.assert_lines( stream.iter_lines(chunk_size), [b'1234567890', b'1234567890', b'12345'], )
def test_streaming_body_is_an_iterator(self): body = six.BytesIO(b'a' * 1024 + b'b' * 1024 + b'c' * 2) stream = response.StreamingBody(body, content_length=2050) self.assertEqual(b'a' * 1024, next(stream)) self.assertEqual(b'b' * 1024, next(stream)) self.assertEqual(b'c' * 2, next(stream)) with self.assertRaises(StopIteration): next(stream)
def test_streaming_line_abstruse_newline_standard(self): for chunk_size in range(1, 30): body = six.BytesIO(b'1234567890\r\n1234567890\r\n12345\r\n') stream = response.StreamingBody(body, content_length=31) self.assert_lines( stream.iter_lines(chunk_size), [b'1234567890', b'1234567890', b'12345'], )
def test_catches_urllib3_read_timeout(self): class TimeoutBody(object): def read(*args, **kwargs): raise URLLib3ReadTimeoutError(None, None, None) def geturl(*args, **kwargs): return 'http://example.com' stream = response.StreamingBody(TimeoutBody(), content_length=None) with self.assertRaises(ReadTimeoutError): stream.read()
def test_catches_urllib3_protocol_error(self): class ProtocolErrorBody: def read(*args, **kwargs): raise URLLib3ProtocolError(None, None, None) def geturl(*args, **kwargs): return 'http://example.com' stream = response.StreamingBody(ProtocolErrorBody(), content_length=None) with self.assertRaises(ResponseStreamingError): stream.read()
def test_streaming_wrapper_validates_content_length(self): body = six.BytesIO(b'1234567890') stream = response.StreamingBody(body, content_length=10) self.assertEqual(stream.read(), b'1234567890')
def test_default_iter_behavior(self): body = six.BytesIO(b'a' * 2048) stream = response.StreamingBody(body, content_length=2048) chunks = list(stream) self.assertEqual(len(chunks), 2) self.assertEqual(chunks, [b'a' * 1024, b'a' * 1024])
def test_streaming_body_with_zero_read(self): body = six.BytesIO(b'1234567890') stream = response.StreamingBody(body, content_length=10) chunk = stream.read(0) self.assertEqual(chunk, b'') self.assertEqual(stream.read(), b'1234567890')
def test_streaming_line_empty_body(self): stream = response.StreamingBody( six.BytesIO(b''), content_length=0, ) self.assert_lines(stream.iter_lines(), [])
def test_streaming_body_tell(self): body = BytesIO(b'1234567890') stream = response.StreamingBody(body, content_length=10) self.assertEqual(stream.tell(), 0) stream.read(5) self.assertEqual(stream.tell(), 5)
def test_streaming_body_with_single_read(self): body = six.BytesIO(b'123456789') stream = response.StreamingBody(body, content_length=10) with self.assertRaises(IncompleteReadError): stream.read()
def test_iter_chunks_single_chunk(self): body = six.BytesIO(b'abcde') stream = response.StreamingBody(body, content_length=5) chunks = list(stream.iter_chunks(chunk_size=1024)) self.assertEqual(chunks, [b'abcde'])
def test_iter_chunks_with_leftover(self): body = six.BytesIO(b'abcde') stream = response.StreamingBody(body, content_length=5) chunks = list(stream.iter_chunks(chunk_size=2)) self.assertEqual(chunks, [b'ab', b'cd', b'e'])
def test_iter_chunks_single_byte(self): body = BytesIO(b'abcde') stream = response.StreamingBody(body, content_length=5) chunks = list(stream.iter_chunks(chunk_size=1)) self.assertEqual(chunks, [b'a', b'b', b'c', b'd', b'e'])
def test_streaming_body_closes(self): body = six.BytesIO(b'1234567890') stream = response.StreamingBody(body, content_length=10) self.assertFalse(body.closed) stream.close() self.assertTrue(body.closed)
def test_streaming_body_readlines(self): body = BytesIO(b'1234567890\n1234567890\n12345') stream = response.StreamingBody(body, content_length=27) chunks = [b'1234567890\n', b'1234567890\n', b'12345'] self.assertEqual(stream.readlines(), chunks)