Added support and tests for unicode support in sessions.

This commit is contained in:
Jakub Roztocil 2014-04-26 18:16:30 +02:00
parent e8a1c051f9
commit a3352af1d4
4 changed files with 38 additions and 13 deletions

View File

@ -38,6 +38,15 @@ def get_response(args, config_dir):
return response return response
def encode_headers(headers):
# This allows for unicode headers which is non-standard but practical.
# See: https://github.com/jkbr/httpie/issues/212
return dict(
(name, value.encode('utf8') if isinstance(value, str) else value)
for name, value in headers.items()
)
def get_requests_kwargs(args): def get_requests_kwargs(args):
"""Translate our `args` into `requests.request` keyword arguments.""" """Translate our `args` into `requests.request` keyword arguments."""
@ -80,18 +89,11 @@ def get_requests_kwargs(args):
if args.certkey: if args.certkey:
cert = (cert, args.certkey) cert = (cert, args.certkey)
# This allows for unicode headers which is non-standard but practical.
# See: https://github.com/jkbr/httpie/issues/212
headers = dict(
(name, value.encode('utf8') if isinstance(value, str) else value)
for name, value in args.headers.items()
)
kwargs = { kwargs = {
'stream': True, 'stream': True,
'method': args.method.lower(), 'method': args.method.lower(),
'url': args.url, 'url': args.url,
'headers': headers, 'headers': encode_headers(args.headers),
'data': args.data, 'data': args.data,
'verify': { 'verify': {
'yes': True, 'yes': True,

View File

@ -19,11 +19,16 @@ class HTTPBasicAuth(requests.auth.HTTPBasicAuth):
See https://github.com/jkbr/httpie/issues/212 See https://github.com/jkbr/httpie/issues/212
""" """
credentials = u'%s:%s' % (self.username, self.password) r.headers['Authorization'] = type(self).make_header(
token = b64encode(credentials.encode('utf8')).strip().decode('latin1') self.username, self.password).encode('latin1')
r.headers['Authorization'] = 'Basic %s' % token
return r return r
@staticmethod
def make_header(username, password):
credentials = u'%s:%s' % (username, password)
token = b64encode(credentials.encode('utf8')).strip().decode('latin1')
return 'Basic %s' % token
class BasicAuthPlugin(BuiltinAuthPlugin): class BasicAuthPlugin(BuiltinAuthPlugin):

View File

@ -27,6 +27,7 @@ def get_response(session_name, requests_kwargs, config_dir, args,
aspects of the session to the request. aspects of the session to the request.
""" """
from .client import encode_headers
if os.path.sep in session_name: if os.path.sep in session_name:
path = os.path.expanduser(session_name) path = os.path.expanduser(session_name)
else: else:
@ -49,8 +50,9 @@ def get_response(session_name, requests_kwargs, config_dir, args,
request_headers = requests_kwargs.get('headers', {}) request_headers = requests_kwargs.get('headers', {})
requests_kwargs['headers'] = dict(session.headers) merged_headers = dict(session.headers)
requests_kwargs['headers'].update(request_headers) merged_headers.update(request_headers)
requests_kwargs['headers'] = encode_headers(merged_headers)
session.update_headers(request_headers) session.update_headers(request_headers)

View File

@ -1,5 +1,7 @@
# coding=utf-8
import os import os
import shutil import shutil
from httpie.plugins.builtin import HTTPBasicAuth
from tests import TestEnvironment, mk_config_dir, http, httpbin, HTTP_OK from tests import TestEnvironment, mk_config_dir, http, httpbin, HTTP_OK
@ -121,3 +123,17 @@ class TestSession(SessionTestBase):
env=self.env()) env=self.env())
assert HTTP_OK in r2 assert HTTP_OK in r2
assert r2.json['headers']['Foo'] == 'Bar' assert r2.json['headers']['Foo'] == 'Bar'
def test_session_unicode(self):
UNICODE = u'太陽'
r1 = http('--session=test', '--auth', u'test:' + UNICODE,
'GET', httpbin('/get'),
u'Test:%s' % UNICODE,
env=self.env())
assert HTTP_OK in r1
r2 = http('--session=test', 'GET', httpbin('/get'), env=self.env())
assert HTTP_OK in r2
assert (r2.json['headers']['Authorization']
== HTTPBasicAuth.make_header(u'test', UNICODE))
assert r2.json['headers']['Test'] == UNICODE