Skip to content

Commit

Permalink
merge conflict and fixes for python3
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Reitz committed Mar 9, 2012
1 parent 253cba3 commit 9f55f50
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -86,3 +86,4 @@ Patches and Suggestions
- Honza Javorek
- Brendan Maguire <maguire.brendan@gmail.com>
- Chris Dary
- Danver Braganza <danverbraganza@gmail.com>
7 changes: 5 additions & 2 deletions requests/models.py
Expand Up @@ -235,7 +235,10 @@ def build(resp):
# Facilitate non-RFC2616-compliant 'location' headers
# (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')
if not urlparse(url).netloc:
url = urljoin(r.url, url)
url = urljoin(r.url,
# Compliant with RFC3986, we percent
# encode the url.
requote_uri(url))

# http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4
if r.status_code is codes.see_other:
Expand Down Expand Up @@ -681,7 +684,7 @@ def generate_chunked():
while 1:
#XXX correct line size? (httplib has 64kb, seems insane)
pending_bytes = fp.readline(40).strip()
if pending_bytes == '':
if not len(pending_bytes):
# No content, like a HEAD request. Break out.
break
pending_bytes = int(pending_bytes, 16)
Expand Down
52 changes: 50 additions & 2 deletions tests/test_requests_ext.py
Expand Up @@ -8,6 +8,7 @@
import unittest

import requests
from requests.compat import is_py2, is_py3

try:
import omnijson as json
Expand Down Expand Up @@ -45,12 +46,23 @@ def test_ssl_hostname_session_not_ok(self):


def test_binary_post(self):
utf8_string = (u'Smörgås').encode('utf-8')
'''We need to be careful how we build the utf-8 string since
unicode literals are a syntax error in python3
'''

if is_py2:
# Blasphemy!
utf8_string = eval("u'Smörgås'.encode('utf-8')")
elif is_py3:
utf8_string = 'Smörgås'.encode('utf-8')
else:
raise EnvironmentError('Flesh out this test for your environment.')
requests.post('http://www.google.com/', data=utf8_string)



def test_unicode_error(self):
url = u'http://blip.fm/~1abvfu'
url = 'http://blip.fm/~1abvfu'
requests.get(url)


Expand All @@ -59,6 +71,42 @@ def test_chunked_head_redirect(self):
r = requests.head(url, allow_redirects=True)
self.assertEqual(r.status_code, 200)

def test_unicode_redirect(self):
'''This url redirects to a location that has a nonstandard
character in it, that breaks requests in python2.7
After some research, the cause was identified as an unintended
sideeffect of overriding of str with unicode.
In the case that the redirected url is actually a malformed
"bytes" object, i.e. a string with character c where
ord(c) > 127,
then unicode(url) breaks.
'''
r = requests.get('http://www.marketwire.com/mw/release.' +
'do?id=1628202&sourceType=3')
assert r.ok

def test_unicode_url_outright(self):
'''This url visits in my browser'''
r = requests.get('http://www.marketwire.com/press-release/' +
'jp-morgan-behauptet-sich-der-spitze-euro' +
'p%C3%A4ischer-anleihe-analysten-laut-umf' +
'rageergebnissen-1628202.htm')
assert r.ok

def test_redirect_encoding(self):
'''This url redirects to
http://www.dealipedia.com/deal_view_investment.php?r=20012'''

r = requests.get('http://feedproxy.google.com/~r/Dealipedia' +
'News/~3/BQtUJRJeZlo/deal_view_investment.' +
'php')
assert r.ok




if __name__ == '__main__':
unittest.main()

0 comments on commit 9f55f50

Please sign in to comment.