Merge branch 'develop'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 044252e, ed8ff63 · Browse files · 5 files·+76 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+76 added−22 removed
- other:+76 added−22 removed
Provenance
- Commit
cc16073- Tree
37fbaff- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
044252e- Parent 2
ed8ff63
5 files changed+76 added−22 removed
modified:requests/core.py+34 added-15 removed
22 unmodified lines23 24 25 26 __title__ = 'requests'27-__version__ = '0.3.0'28-__build__ = 0x00030027+__version__ = '0.3.1'28+__build__ = 0x00030129 __author__ = 'Kenneth Reitz'30 __license__ = 'ISC'31 __copyright__ = 'Copyright 2011 Kenneth Reitz'32 77 unmodified lines110 """Creates appropriate opener object for urllib2."""111 112 _handlers = []113 114+ if self.cookiejar is not None:115+ _handlers.append(urllib2.HTTPCookieProcessor(self.cookiejar))116+117 if self.auth:118 if not isinstance(self.auth.handler, (urllib2.AbstractBasicAuthHandler, urllib2.AbstractDigestAuthHandler)):119 auth_manager.add_password(self.auth.realm, self.url, self.auth.username, self.auth.password)120 self.auth.handler = self.auth.handler(auth_manager)121 auth_manager.add_auth(self.url, self.auth)122 123 _handlers.append(self.auth.handler)124 122- _handlers.extend(get_handlers())123- opener = urllib2.build_opener(*_handlers)124- return opener.open125- else:125+ if not _handlers:126 return urllib2.urlopen127 128+ _handlers.extend(get_handlers())129+ opener = urllib2.build_opener(*_handlers)130 131+ if self.headers:132+ # Allow default headers in the opener to be overloaded133+ normal_keys = [k.capitalize() for k in self.headers]134+ for key, val in opener.addheaders[:]:135+ if key not in normal_keys:136+ continue137+ # Remove it, we have a value to take its place138+ opener.addheaders.remove((key, val))139+140+ return opener.open141+142 def _build_response(self, resp):143 """Build internal Response object from given response."""144 145 self.response.status_code = getattr(resp, 'code', None)13 unmodified lines159 return '%s?%s' % (url, data)160 else:161 return url162 150-163 def send(self, anyway=False):164 """Sends the request. Returns True of successful, false if not.165 If there was an HTTPError during transmission,166 self.response.status_code will contain the HTTPError code.22 unmodified lines189 else:190 req = _Request(self.url, data=self._enc_data, method=self.method)191 192 if self.headers:181- req.headers = self.headers193+ req.headers.update(self.headers)194 195 if not self.sent or anyway:196 try:197 opener = self._get_opener()186- resp = opener(req)198+ resp = opener(req)199+200+ if self.cookiejar is not None:201+ self.cookiejar.extract_cookies(resp, req)202+ 203 except urllib2.HTTPError, why:204 self._build_response(why)205 self.response.error = why206 else:3 unmodified lines210 self.response.cached = False211 else:212 self.response.cached = True213 198-214 self.sent = self.response.ok215 216 return self.sent217 218 219+ def read(self):220+ return self.response.read()221 222 class Response(object):223 """The :class:`Request` object. All :class:`Request` objects contain a224 :class:`Request.response <response>` attribute, which is an instance of23 unmodified lines248 """Raises stored HTTPError if one exists."""249 if self.error:250 raise self.error251 252+ def read(self):253+ return self.content254 255 256 class AuthManager(object):257 """Authentication Manager."""183 unmodified lines441 :param cookies: (optional) CookieJar object to send with the :class:`Request`.442 :param auth: (optional) AuthObject to enable Basic HTTP Auth.443 """444 426- return request('GET', url, params=params, headers=headers, cookiejar=cookies, auth=auth)445+ return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth)446 447 448 def head(url, params={}, headers={}, cookies=None, auth=None):449 """Sends a HEAD request. Returns :class:`Response` object.4 unmodified lines454 :param cookies: (optional) CookieJar object to send with the :class:`Request`.455 :param auth: (optional) AuthObject to enable Basic HTTP Auth.456 """457 439- return request('HEAD', url, params=params, headers=headers, cookiejar=cookies, auth=auth)458+ return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth)459 460 461 def post(url, data={}, headers={}, files=None, cookies=None, auth=None):462 """Sends a POST request. Returns :class:`Response` object.5 unmodified lines468 :param cookies: (optional) CookieJar object to send with the :class:`Request`.469 :param auth: (optional) AuthObject to enable Basic HTTP Auth.470 """471 453- return request('POST', url, data=data, headers=headers, files=files, cookiejar=cookies, auth=auth)472+ return request('POST', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth)473 474 475 def put(url, data='', headers={}, files={}, cookies=None, auth=None):476 """Sends a PUT request. Returns :class:`Response` object.5 unmodified lines482 :param cookies: (optional) CookieJar object to send with the :class:`Request`.483 :param auth: (optional) AuthObject to enable Basic HTTP Auth.484 """485 467- return request('PUT', url, data=data, headers=headers, files=files, cookiejar=cookies, auth=auth)486+ return request('PUT', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth)487 488 489 def delete(url, params={}, headers={}, cookies=None, auth=None):490 """Sends a DELETE request. Returns :class:`Response` object.4 unmodified lines495 :param cookies: (optional) CookieJar object to send with the :class:`Request`.496 :param auth: (optional) AuthObject to enable Basic HTTP Auth.497 """498 480- return request('DELETE', url, params=params, headers=headers, cookiejar=cookies, auth=auth)499+ return request('DELETE', url, params=params, headers=headers, cookies=cookies, auth=auth)500 501 502 503 class RequestException(Exception):11 unmodified linesmodified:.gitignore+2 added-1 removed
1 .idea2 MANIFEST3 coverage.xml4 nosetests.xml5-pylint.txt5+pylint.txt6+*.pycmodified:AUTHORS+4 added-1 removed
11 unmodified lines12 13 - Various Pocoo Members14 - Chris Adams15 - Flavio Percoco Premoli16-- Dj Gilcrease16+- Dj Gilcrease17+- Justin Murphy18+- Rob Madole19+- Aram Dulyanmodified:HISTORY.rst+8 added
1 History2 -------3 4+0.3.1 (2011-04-01)5+++++++++++++++++++6+7+* Cookie Changes8+* Response.read() 9+* Poster fix10+11+12 0.3.0 (2011-02-25)13 ++++++++++++++++++14 15 * Automatic Authentication API Change53 unmodified linesmodified:test_requests.py+28 added-5 removed
1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3 4 import unittest5+import cookielib6 7 import requests8 9 18 unmodified lines28 r = requests.get('https://google.com')29 self.assertEqual(r.status_code, 200)30 31 def test_HTTP_200_OK_GET_WITH_PARAMS(self):31-32 heads = {'User-agent': 'Mozilla/5.0'}33 34 r = requests.get('http://www.google.com/search', params={'q': 'test'}, headers=heads)35 self.assertEqual(r.status_code, 200)36 37-37 def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):39-38 heads = {'User-agent': 'Mozilla/5.0'}39 40 r = requests.get('http://google.com/search?test=true', params={'q': 'test'}, headers=heads)41 self.assertEqual(r.status_code, 200)42+43+ def test_user_agent_transfers(self):44+ """Issue XX"""45+ heads = {'User-agent':46+ 'Mozilla/5.0 (github.com/kennethreitz/requests)'}47+48+ r = requests.get('http://whatsmyua.com', headers=heads);49+ self.assertTrue(heads['User-agent'] in r.content)50 51 def test_HTTP_200_OK_HEAD(self):52 r = requests.head('http://google.com')53 self.assertEqual(r.status_code, 200)15 unmodified lines69 # reset auto authentication70 requests.auth_manager.empty()71 72 def test_POSTBIN_GET_POST_FILES(self):67-73 bin = requests.post('http://www.postbin.org/')74 print bin.url75 self.assertEqual(bin.status_code, 200)76 3 unmodified lines80 post2 = requests.post(bin.url, files={'some': open('test_requests.py')})81 self.assertEqual(post2.status_code, 201)82 83 def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self):79-84 bin = requests.post('http://www.postbin.org/')85 86 self.assertEqual(bin.status_code, 200)87 88 post2 = requests.post(bin.url, files={'some': open('test_requests.py')}, data={'some': 'data'})89 self.assertEqual(post2.status_code, 201)90 91+92+ def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self):93+ bin = requests.post('http://www.postbin.org/')94+ self.assertEqual(bin.status_code, 200)95+96+ post2 = requests.post(bin.url, files={'some': open('test_requests.py')},97+ headers={'User-Agent': 'requests-tests'})98+99+ self.assertEqual(post2.status_code, 201)100+ 101 def test_nonzero_evaluation(self):102 r = requests.get('http://google.com/some-404-url')103 self.assertEqual(bool(r), False)104 11 unmodified lines116 r = requests.get('http://google.com/')117 self.assertFalse(r.error)118 r.raise_for_status()119 120+ def test_cookie_jar(self):121+ """122+ .. todo:: This really doesn't test to make sure the cookie is working123+ """124+ jar = cookielib.CookieJar()125+ self.assertFalse(jar)126 127+ requests.get('http://google.com', cookies=jar)128+ self.assertTrue(jar)129 130 131+132 if __name__ == '__main__':133 unittest.main()