Skip to content
Myr

Merge pull request #3082 from tzickel/master

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Added a test to show a faulty behaviour when posting binary data for an object with no __iter__

authored by Ian Cordasco · committed by Ian Cordasco · parents b5d3754, 219490e · Browse files · 2 files+46 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+46 added−2 removed

  • tests:+42 added−0 removed
  • other:+4 added−2 removed

2 files+46 added−2 removed

modified:requests/models.py+4 added-2 removed
463 unmodified lines
464 self.body = body465466 def prepare_content_length(self, body):467 if hasattr(body, 'seek') and hasattr(body, 'tell'):468 curr_pos = body.tell()469 body.seek(0, 2)470 end_pos = body.tell()469 self.headers['Content-Length'] = builtin_str(body.tell())470 body.seek(0, 0)471 self.headers['Content-Length'] = builtin_str(max(0, end_pos - curr_pos))472 body.seek(curr_pos, 0)473 elif body is not None:474 l = super_len(body)475 if l:476 self.headers['Content-Length'] = builtin_str(l)
379 unmodified lines
modified:tests/test_requests.py+42 added
480 unmodified lines
481482 with pytest.raises(ValueError):483 requests.post(url, files=['bad file data'])484485 def test_POSTBIN_SEEKED_OBJECT_WITH_NO_ITER(self, httpbin):486487 class TestStream(object):488 def __init__(self, data):489 self.data = data.encode()490 self.length = len(self.data)491 self.index = 0492493 def __len__(self):494 return self.length495496 def read(self, size=None):497 if size:498 ret = self.data[self.index:self.index + size]499 self.index += size500 else:501 ret = self.data[self.index:]502 self.index = self.length503 return ret504505 def tell(self):506 return self.index507508 def seek(self, offset, where=0):509 if where == 0:510 self.index = offset511 elif where == 1:512 self.index += offset513 elif where == 2:514 self.index = self.length + offset515516 test = TestStream('test')517 post1 = requests.post(httpbin('post'), data=test)518 assert post1.status_code == 200519 assert post1.json()['data'] == 'test'520521 test = TestStream('test')522 test.seek(2)523 post2 = requests.post(httpbin('post'), data=test)524 assert post2.status_code == 200525 assert post2.json()['data'] == 'st'526527 def test_POSTBIN_GET_POST_FILES_WITH_DATA(self, httpbin):528529 url = httpbin('post')530 requests.post(url).raise_for_status()
1047 unmodified lines