fix iteration on null responses in safe_mode
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ori Livneh · committed by Ori Livneh · parent a7223f8 · Browse files · 2 files·+33 added−19 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+33 added−19 removed
- other:+33 added−19 removed
Provenance
- Commit
716d29c- Tree
06ba2d8- Author
Ori Livneh <ori.livneh@gmail.com>- Committer
Ori Livneh <ori.livneh@gmail.com>- Parent
a7223f8
2 files changed+33 added−19 removed
modified:requests/models.py+22 added-19 removed
557 unmodified lines558 'The content for this response was already consumed'559 )560 561 def generate():562+ # self.raw can be None if we're in safe_mode and the request failed563+ if self.raw is not None:562- while 1:563- chunk = self.raw.read(chunk_size)564- if not chunk:565- break566- yield chunk564+ while 1:565+ chunk = self.raw.read(chunk_size)566+ if not chunk:567+ break568+ yield chunk569 self._content_consumed = True570 571 gen = generate()572 25 unmodified lines598 'The content for this response was already consumed'599 )600 601 def generate():602+ if self.raw is not None:600- chunk = []603+ chunk = []604 602- while 1:603- c = self.raw.read(1)604- if not c:605- break605+ while 1:606+ c = self.raw.read(1)607+ if not c:608+ break609 607- if c in newlines:610+ if c in newlines:611+ yield ''.join(chunk)612+ chunk = []613+ else:614+ chunk.append(c)615+616+ # Yield the remainder, in case the response617+ # did not terminate with a newline618+ if chunk:619 yield ''.join(chunk)609- chunk = []610- else:611- chunk.append(c)620 613- # Yield the remainder, in case the response614- # did not terminate with a newline615- if chunk:616- yield ''.join(chunk)617-621 self._content_consumed = True622 623 gen = generate()624 75 unmodified linesmodified:test_requests.py+11 added
617 unmodified lines618 # Make sure iter_lines doesn't chop the trailing bit619 lines = '\n'.join(r.iter_lines())620 self.assertEqual(lines, quote)621 622+ def test_null_response(self):623+624+ # Safe mode creates empty responses for failed requests.625+626+ # Iterating on these responses should produce empty sequences627+ r = requests.get('http://_/', config=dict(safe_mode=True))628+ self.assertEquals(list(r.iter_lines()), [])629+630+ r = requests.get('http://_/', config=dict(safe_mode=True))631+ self.assertEquals(list(r.iter_content()), [])632+633 def test_timeout(self):634 635 # When not in safe mode, should raise Timeout exception636 with self.assertRaises(requests.exceptions.Timeout):10 unmodified lines