Python 3 unicode fixes.

This commit is contained in:
Jakub Roztocil 2014-04-26 17:16:11 +02:00
parent 8ec32fe7f3
commit 467d126b6c
7 changed files with 25 additions and 22 deletions

View File

@ -83,8 +83,8 @@ def get_requests_kwargs(args):
# This allows for unicode headers which is non-standard but practical.
# See: https://github.com/jkbr/httpie/issues/212
headers = dict(
(k.encode('utf8'), v.encode('utf8') if isinstance(v, str) else v)
for k, v in args.headers.items()
(name, value.encode('utf8') if isinstance(value, str) else value)
for name, value in args.headers.items()
)
kwargs = {

View File

@ -104,7 +104,10 @@ class HTTPMessage(object):
@property
def content_type(self):
"""Return the message content type."""
return self._orig.headers.get('Content-Type', '')
ct = self._orig.headers.get('Content-Type', '')
if not isinstance(ct, str):
ct = ct.decode('utf8')
return ct
class HTTPResponse(HTTPMessage):

View File

@ -20,7 +20,7 @@ class HTTPBasicAuth(requests.auth.HTTPBasicAuth):
"""
credentials = u'%s:%s' % (self.username, self.password)
token = b64encode(credentials.encode('utf8')).strip()
token = b64encode(credentials.encode('utf8')).strip().decode('latin1')
r.headers['Authorization'] = 'Basic %s' % token
return r

View File

@ -105,7 +105,7 @@ class Session(BaseConfigDict):
"""
for name, value in request_headers.items():
value = value.decode('utf8')
if name == 'User-Agent' and value.startswith('HTTPie/'):
continue

View File

@ -65,7 +65,7 @@ class TestAutoContentTypeAndAcceptHeaders:
# JSON headers should automatically be set also for GET with data.
r = http('POST', httpbin('/post'), 'a=b')
assert HTTP_OK in r
assert '"Accept": "application/json"' in r
assert '"Accept": "application/json"' in r, r
assert '"Content-Type": "application/json; charset=utf-8' in r
def test_POST_explicit_JSON_auto_JSON_accept(self):

View File

@ -61,5 +61,5 @@ class TestHTTPie:
def test_headers(self):
r = http('GET', httpbin('/headers'), 'Foo:bar')
assert HTTP_OK in r
assert '"User-Agent": "HTTPie' in r
assert '"User-Agent": "HTTPie' in r, r
assert '"Foo": "bar"' in r

View File

@ -6,40 +6,40 @@ Various unicode handling related tests.
from tests import http, httpbin
JP_SUN = u'太陽'
UNICODE = u'太陽'
class TestUnicode:
def test_unicode_headers(self):
r = http('GET', httpbin('/headers'), u'Test:%s' % JP_SUN)
assert r.json['headers']['Test'] == JP_SUN
r = http('GET', httpbin('/headers'), u'Test:%s' % UNICODE)
assert r.json['headers']['Test'] == UNICODE
def test_unicode_form_item(self):
r = http('--form', 'POST', httpbin('/post'), u'test=%s' % JP_SUN)
assert r.json['form']['test'] == JP_SUN
r = http('--form', 'POST', httpbin('/post'), u'test=%s' % UNICODE)
assert r.json['form']['test'] == UNICODE
def test_unicode_json_item(self):
r = http('--json', 'POST', httpbin('/post'), u'test=%s' % JP_SUN)
assert r.json['json']['test'] == JP_SUN
r = http('--json', 'POST', httpbin('/post'), u'test=%s' % UNICODE)
assert r.json['json']['test'] == UNICODE
def test_unicode_raw_json_item(self):
r = http('--json', 'POST', httpbin('/post'), u'test:=["%s"]' % JP_SUN)
assert r.json['json']['test'] == [JP_SUN]
r = http('--json', 'POST', httpbin('/post'), u'test:=["%s"]' % UNICODE)
assert r.json['json']['test'] == [UNICODE]
def test_unicode_url(self):
r = http(httpbin(u'/get?test=' + JP_SUN))
assert r.json['args']['test'] == JP_SUN
r = http(httpbin(u'/get?test=' + UNICODE))
assert r.json['args']['test'] == UNICODE
def test_unicode_basic_auth(self):
# it doesn't really authenticate us because httpbin
# doesn't interpret the utf8-encoded auth
http('--verbose', '--auth', u'test:%s' % JP_SUN,
httpbin(u'/basic-auth/test/' + JP_SUN))
http('--verbose', '--auth', u'test:%s' % UNICODE,
httpbin(u'/basic-auth/test/' + UNICODE))
def test_unicode_digest_auth(self):
# it doesn't really authenticate us because httpbin
# doesn't interpret the utf8-encoded auth
http('--auth-type=digest',
'--auth', u'test:%s' % JP_SUN,
httpbin(u'/digest-auth/auth/test/' + JP_SUN))
'--auth', u'test:%s' % UNICODE,
httpbin(u'/digest-auth/auth/test/' + UNICODE))