From: Cory Benfield Date: Wed, 6 Feb 2013 18:48:47 +0000 (+0000) Subject: Guard against users sending unprepared requests. X-Git-Tag: v1.2.0~50^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b0657cf163b2ed3781896eb1712a8ae1b14125ab;p=services%2Fpython-requests.git Guard against users sending unprepared requests. --- diff --git a/requests/sessions.py b/requests/sessions.py index 411d1bb..9dff255 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -365,6 +365,11 @@ class Session(SessionRedirectMixin): def send(self, request, **kwargs): """Send a given PreparedRequest.""" + # It's possible that users might accidentally send a Request object. + # Guard against that specific failure case. + if getattr(request, 'prepare', None): + raise ValueError('You can only send PreparedRequests.') + # Set up variables needed for resolve_redirects and dispatching of # hooks allow_redirects = kwargs.pop('allow_redirects', True) diff --git a/test_requests.py b/test_requests.py index 4ba9a43..0355a4a 100644 --- a/test_requests.py +++ b/test_requests.py @@ -341,5 +341,9 @@ class RequestsTestCase(unittest.TestCase): self.assertEqual(('user', 'pass'), requests.utils.get_auth_from_url(url)) + def test_cannot_send_unprepared_requests(self): + r = requests.Request(url=HTTPBIN) + self.assertRaises(ValueError, requests.Session().send, r) + if __name__ == '__main__': unittest.main()