Skip to content
Myr

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

1 file+101 added−0 removed

modified:tests/test_requests.py+101 added
1384 unmodified lines
1385 pass1386 r3 = next(rg)1387 assert not r3.is_redirect13881389 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 None13941395 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'14011402 # the data has all been read1403 assert prep.body.read() == b''14041405 # rewind it back1406 requests.utils.rewind_body(prep)1407 assert prep.body.read() == b'the data'14081409 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'14161417 # the data has all been read1418 assert prep.body.read() == b''14191420 # rewind it back1421 requests.utils.rewind_body(prep)1422 assert prep.body.read() == b'data'14231424 def test_rewind_body_no_seek(self):1425 class BadFileObj:1426 def __init__(self, data):1427 self.data = data14281429 def tell(self):1430 return 014311432 def __iter__(self):1433 return14341435 data = BadFileObj('the data')1436 s = requests.Session()1437 prep = requests.Request('GET', 'http://example.com', data=data).prepare()1438 assert prep._body_position == 014391440 with pytest.raises(UnrewindableBodyError) as e:1441 requests.utils.rewind_body(prep)14421443 assert 'Unable to rewind request body' in str(e)14441445 def test_rewind_body_failed_seek(self):1446 class BadFileObj:1447 def __init__(self, data):1448 self.data = data14491450 def tell(self):1451 return 014521453 def seek(self, pos):1454 raise OSError()14551456 def __iter__(self):1457 return14581459 data = BadFileObj('the data')1460 s = requests.Session()1461 prep = requests.Request('GET', 'http://example.com', data=data).prepare()1462 assert prep._body_position == 014631464 with pytest.raises(UnrewindableBodyError) as e:1465 requests.utils.rewind_body(prep)14661467 assert 'error occured when rewinding request body' in str(e)14681469 def test_rewind_body_failed_tell(self):1470 class BadFileObj:1471 def __init__(self, data):1472 self.data = data14731474 def tell(self):1475 raise OSError()14761477 def __iter__(self):1478 return14791480 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 None14841485 with pytest.raises(UnrewindableBodyError) as e:1486 requests.utils.rewind_body(prep)14871488 assert 'Unable to rewind request body' in str(e)14891490 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 = False
571 unmodified lines