mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
add some parametrized tests for iter_lines()
Write a list of different chunk splits and their expected results to test against, using ianepperson's breakdown as specification: https://github.com/kennethreitz/requests/pull/2431#issuecomment-72333964
This commit is contained in:
+35
-6
@@ -1318,10 +1318,7 @@ class TestRequests:
|
||||
]
|
||||
mock_data = ''.join(mock_chunks)
|
||||
|
||||
def mock_iter_content(*args, **kwargs):
|
||||
'''Fake difficult data.'''
|
||||
for chunk in mock_chunks:
|
||||
yield chunk
|
||||
mock_iter_content = lambda *args, **kwargs: (e for e in mock_chunks)
|
||||
|
||||
r = requests.Response()
|
||||
r._content_consumed = True
|
||||
@@ -1332,13 +1329,45 @@ class TestRequests:
|
||||
# Because '\n' is a single line-end, when `iter_lines()` receives
|
||||
# the chunks containing a single '\n', it emits '' as a line -- whereas
|
||||
# `.splitlines()` combines with the '\r' and splits on `\r\n`.
|
||||
assert list(r.iter_lines()) != mock_data.splitlines()
|
||||
|
||||
result = list(r.iter_lines())
|
||||
assert result != mock_data.splitlines()
|
||||
assert result[2] == ''
|
||||
assert result[4] == ''
|
||||
# If we change all the line breaks to `\r`, we should be okay.
|
||||
mock_chunks = [chunk.replace('\n', '\r') for chunk in mock_chunks]
|
||||
mock_data = ''.join(mock_chunks)
|
||||
assert list(r.iter_lines()) == mock_data.splitlines()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'content, expected_no_delimiter, expected_delimiter', (
|
||||
([''], [], []),
|
||||
(['line\n'], ['line'], ['line\n']),
|
||||
(['line', '\n'], ['line'], ['line\n']),
|
||||
(['line\r\n'], ['line'], ['line', '']),
|
||||
(['line', '\r\n'], ['line'], ['line', '']),
|
||||
(['a\r', '\nb\r'], ['a', '', 'b'], ['a', 'b\r']),
|
||||
(['a\n', '\nb'], ['a', '', 'b'], ['a\n\nb']),
|
||||
(['a\r\n','\rb\n'], ['a', '', 'b'], ['a', '\rb\n']),
|
||||
(['a\nb', 'c'], ['a', 'bc'], ['a\nbc']),
|
||||
(['a\n', '\rb', '\r\nc'], ['a', '', 'b', 'c'], ['a\n\rb', 'c'])
|
||||
))
|
||||
def test_response_lines_parametrized(self, content, expected_no_delimiter, expected_delimiter):
|
||||
"""
|
||||
Test a lot of potential chunk splits to ensure consistency of
|
||||
iter_lines(delimiter=x), as well as the legacy behavior of
|
||||
iter_lines() without delimiter
|
||||
https://github.com/kennethreitz/requests/pull/2431#issuecomment-72333964
|
||||
"""
|
||||
mock_chunks = content
|
||||
mock_iter_content = lambda *args, **kwargs: (e for e in mock_chunks)
|
||||
|
||||
r = requests.Response()
|
||||
r._content_consumed = True
|
||||
r.iter_content = mock_iter_content
|
||||
assert list(r.iter_lines()) == expected_no_delimiter
|
||||
assert list(r.iter_lines(delimiter='\r\n')) == expected_delimiter
|
||||
|
||||
def test_prepared_request_is_pickleable(self, httpbin):
|
||||
p = requests.Request('GET', httpbin('get')).prepare()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user