Fixing User-agent header problem introduced with Python 2.7.1
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by robmadole · committed by robmadole · parent be22804 · Browse files · 2 files·+22 added−11 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+22 added−11 removed
- other:+22 added−11 removed
Provenance
- Commit
2401f14- Tree
7492a4f- Author
robmadole <robmadole@gmail.com>- Committer
robmadole <robmadole@gmail.com>- Parent
be22804
2 files changed+22 added−11 removed
modified:requests/core.py+14 added-5 removed
120 unmodified lines121 auth_manager.add_auth(self.url, self.auth)122 123 _handlers.append(self.auth.handler)124 125- if _handlers:126- _handlers.extend(get_handlers())127- opener = urllib2.build_opener(*_handlers)128- return opener.open129- 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)366 unmodified linesmodified:test_requests.py+8 added-6 removed
27 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):32-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 38-37 def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):40-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):68-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):80-84 bin = requests.post('http://www.postbin.org/')85 86 self.assertEqual(bin.status_code, 200)87 1 unmodified line89 self.assertEqual(post2.status_code, 201)90 91 92 def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self):90-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')},37 unmodified lines