test _body_position and rewind_body
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nate Prewitt · committed by Nate Prewitt · parent 40402cd · Browse files · 1 file·+101 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+101 added−0 removed
- tests:+101 added−0 removed
Provenance
- Commit
9849c27- Tree
c7ad1c4- Author
Nate Prewitt <Nate.Prewitt@gmail.com>- Committer
Nate Prewitt <Nate.Prewitt@gmail.com>- Parent
40402cd
1 file changed+101 added−0 removed
modified:tests/test_requests.py+101 added
1384 unmodified lines1385 pass1386 r3 = next(rg)1387 assert not r3.is_redirect1388 1389+ def test_prepare_body_position_non_stream(self):1390+ data = b'the data'1391+ s = requests.Session()1392+ prep = requests.Request('GET', 'http://example.com', data=data).prepare()1393+ assert prep._body_position is None1394+1395+ def test_rewind_body(self):1396+ data = io.BytesIO(b'the data')1397+ s = requests.Session()1398+ prep = requests.Request('GET', 'http://example.com', data=data).prepare()1399+ assert prep._body_position == 01400+ assert prep.body.read() == b'the data'1401+1402+ # the data has all been read1403+ assert prep.body.read() == b''1404+1405+ # rewind it back1406+ requests.utils.rewind_body(prep)1407+ assert prep.body.read() == b'the data'1408+1409+ def test_rewind_partially_read_body(self):1410+ data = io.BytesIO(b'the data')1411+ s = requests.Session()1412+ data.read(4) # read some data1413+ prep = requests.Request('GET', 'http://example.com', data=data).prepare()1414+ assert prep._body_position == 41415+ assert prep.body.read() == b'data'1416+1417+ # the data has all been read1418+ assert prep.body.read() == b''1419+1420+ # rewind it back1421+ requests.utils.rewind_body(prep)1422+ assert prep.body.read() == b'data'1423+1424+ def test_rewind_body_no_seek(self):1425+ class BadFileObj:1426+ def __init__(self, data):1427+ self.data = data1428+1429+ def tell(self):1430+ return 01431+1432+ def __iter__(self):1433+ return1434+1435+ data = BadFileObj('the data')1436+ s = requests.Session()1437+ prep = requests.Request('GET', 'http://example.com', data=data).prepare()1438+ assert prep._body_position == 01439+1440+ with pytest.raises(UnrewindableBodyError) as e:1441+ requests.utils.rewind_body(prep)1442+1443+ assert 'Unable to rewind request body' in str(e)1444+1445+ def test_rewind_body_failed_seek(self):1446+ class BadFileObj:1447+ def __init__(self, data):1448+ self.data = data1449+1450+ def tell(self):1451+ return 01452+1453+ def seek(self, pos):1454+ raise OSError()1455+1456+ def __iter__(self):1457+ return1458+1459+ data = BadFileObj('the data')1460+ s = requests.Session()1461+ prep = requests.Request('GET', 'http://example.com', data=data).prepare()1462+ assert prep._body_position == 01463+1464+ with pytest.raises(UnrewindableBodyError) as e:1465+ requests.utils.rewind_body(prep)1466+1467+ assert 'error occured when rewinding request body' in str(e)1468+1469+ def test_rewind_body_failed_tell(self):1470+ class BadFileObj:1471+ def __init__(self, data):1472+ self.data = data1473+1474+ def tell(self):1475+ raise OSError()1476+1477+ def __iter__(self):1478+ return1479+1480+ data = BadFileObj('the data')1481+ s = requests.Session()1482+ prep = requests.Request('GET', 'http://example.com', data=data).prepare()1483+ assert prep._body_position is not None1484+1485+ with pytest.raises(UnrewindableBodyError) as e:1486+ requests.utils.rewind_body(prep)1487+1488+ assert 'Unable to rewind request body' in str(e)1489+1490 def _patch_adapter_gzipped_redirect(self, session, url):1491 adapter = session.get_adapter(url=url)1492 org_build_response = adapter.build_response1493 self._patched_response = False571 unmodified lines