Add unit tests for _encode_meta_headers
Also move it to a module level function. Change-Id: I2da64876815619a6db7ff208061df2191767806c
This commit is contained in:
parent
202bbab2d0
commit
0f2b0dc300
@ -23,6 +23,10 @@ import sys
|
|||||||
import logging
|
import logging
|
||||||
import warnings
|
import warnings
|
||||||
import functools
|
import functools
|
||||||
|
try:
|
||||||
|
from simplejson import loads as json_loads
|
||||||
|
except ImportError:
|
||||||
|
from json import loads as json_loads
|
||||||
|
|
||||||
from distutils.version import StrictVersion
|
from distutils.version import StrictVersion
|
||||||
from requests.exceptions import RequestException, SSLError
|
from requests.exceptions import RequestException, SSLError
|
||||||
@ -38,6 +42,8 @@ from swiftclient.utils import LengthWrapper
|
|||||||
AUTH_VERSIONS_V1 = ('1.0', '1', 1)
|
AUTH_VERSIONS_V1 = ('1.0', '1', 1)
|
||||||
AUTH_VERSIONS_V2 = ('2.0', '2', 2)
|
AUTH_VERSIONS_V2 = ('2.0', '2', 2)
|
||||||
AUTH_VERSIONS_V3 = ('3.0', '3', 3)
|
AUTH_VERSIONS_V3 = ('3.0', '3', 3)
|
||||||
|
USER_METADATA_TYPE = tuple('x-%s-meta-' % type_ for type_ in
|
||||||
|
('container', 'account', 'object'))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from logging import NullHandler
|
from logging import NullHandler
|
||||||
@ -119,16 +125,22 @@ def encode_utf8(value):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
# look for a real json parser first
|
def encode_meta_headers(headers):
|
||||||
try:
|
"""Only encode metadata headers keys"""
|
||||||
# simplejson is popular and pretty good
|
ret = {}
|
||||||
from simplejson import loads as json_loads
|
for header, value in headers.items():
|
||||||
except ImportError:
|
value = encode_utf8(value)
|
||||||
# 2.6 will have a json module in the stdlib
|
header = header.lower()
|
||||||
from json import loads as json_loads
|
|
||||||
|
if (isinstance(header, six.string_types)
|
||||||
|
and header.startswith(USER_METADATA_TYPE)):
|
||||||
|
header = encode_utf8(header)
|
||||||
|
|
||||||
|
ret[header] = value
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
class HTTPConnection:
|
class HTTPConnection(object):
|
||||||
def __init__(self, url, proxy=None, cacert=None, insecure=False,
|
def __init__(self, url, proxy=None, cacert=None, insecure=False,
|
||||||
ssl_compression=False, default_user_agent=None):
|
ssl_compression=False, default_user_agent=None):
|
||||||
"""
|
"""
|
||||||
@ -184,27 +196,12 @@ class HTTPConnection:
|
|||||||
""" Final wrapper before requests call, to be patched in tests """
|
""" Final wrapper before requests call, to be patched in tests """
|
||||||
return self.request_session.request(*arg, **kwarg)
|
return self.request_session.request(*arg, **kwarg)
|
||||||
|
|
||||||
def _encode_meta_headers(self, items):
|
|
||||||
"""Only encode metadata headers keys"""
|
|
||||||
ret = {}
|
|
||||||
for header, value in items:
|
|
||||||
value = encode_utf8(value)
|
|
||||||
header = header.lower()
|
|
||||||
if isinstance(header, six.string_types):
|
|
||||||
for target_type in 'container', 'account', 'object':
|
|
||||||
prefix = 'x-%s-meta-' % target_type
|
|
||||||
if header.startswith(prefix):
|
|
||||||
header = encode_utf8(header)
|
|
||||||
break
|
|
||||||
ret[header] = value
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def request(self, method, full_path, data=None, headers=None, files=None):
|
def request(self, method, full_path, data=None, headers=None, files=None):
|
||||||
""" Encode url and header, then call requests.request """
|
""" Encode url and header, then call requests.request """
|
||||||
if headers is None:
|
if headers is None:
|
||||||
headers = {}
|
headers = {}
|
||||||
else:
|
else:
|
||||||
headers = self._encode_meta_headers(headers.items())
|
headers = encode_meta_headers(headers)
|
||||||
|
|
||||||
# set a default User-Agent header if it wasn't passed in
|
# set a default User-Agent header if it wasn't passed in
|
||||||
if 'user-agent' not in headers:
|
if 'user-agent' not in headers:
|
||||||
@ -350,7 +347,7 @@ def get_auth_keystone(auth_url, user, key, os_options, **kwargs):
|
|||||||
except exceptions.EndpointNotFound:
|
except exceptions.EndpointNotFound:
|
||||||
raise ClientException('Endpoint for %s not found - '
|
raise ClientException('Endpoint for %s not found - '
|
||||||
'have you specified a region?' % service_type)
|
'have you specified a region?' % service_type)
|
||||||
return (endpoint, _ksclient.auth_token)
|
return endpoint, _ksclient.auth_token
|
||||||
|
|
||||||
|
|
||||||
def get_auth(auth_url, user, key, **kwargs):
|
def get_auth(auth_url, user, key, **kwargs):
|
||||||
@ -463,9 +460,8 @@ def get_account(url, token, marker=None, limit=None, prefix=None,
|
|||||||
listing = rv[1]
|
listing = rv[1]
|
||||||
while listing:
|
while listing:
|
||||||
marker = listing[-1]['name']
|
marker = listing[-1]['name']
|
||||||
listing = \
|
listing = get_account(url, token, marker, limit, prefix,
|
||||||
get_account(url, token, marker, limit, prefix,
|
end_marker, http_conn)[1]
|
||||||
end_marker, http_conn)[1]
|
|
||||||
if listing:
|
if listing:
|
||||||
rv[1].extend(listing)
|
rv[1].extend(listing)
|
||||||
return rv
|
return rv
|
||||||
@ -1195,8 +1191,8 @@ class Connection(object):
|
|||||||
self.retry_on_ratelimit = retry_on_ratelimit
|
self.retry_on_ratelimit = retry_on_ratelimit
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
if self.http_conn and type(self.http_conn) is tuple\
|
if (self.http_conn and isinstance(self.http_conn, tuple)
|
||||||
and len(self.http_conn) > 1:
|
and len(self.http_conn) > 1):
|
||||||
conn = self.http_conn[1]
|
conn = self.http_conn[1]
|
||||||
if hasattr(conn, 'close') and callable(conn.close):
|
if hasattr(conn, 'close') and callable(conn.close):
|
||||||
conn.close()
|
conn.close()
|
||||||
|
@ -34,6 +34,7 @@ from .utils import MockHttpTest, fake_get_auth_keystone
|
|||||||
|
|
||||||
from swiftclient import client as c
|
from swiftclient import client as c
|
||||||
import swiftclient.utils
|
import swiftclient.utils
|
||||||
|
import swiftclient
|
||||||
|
|
||||||
|
|
||||||
class TestClientException(testtools.TestCase):
|
class TestClientException(testtools.TestCase):
|
||||||
@ -160,6 +161,25 @@ class TestHttpHelpers(MockHttpTest):
|
|||||||
url = 'ftp://www.test.com'
|
url = 'ftp://www.test.com'
|
||||||
self.assertRaises(c.ClientException, c.http_connection, url)
|
self.assertRaises(c.ClientException, c.http_connection, url)
|
||||||
|
|
||||||
|
def test_encode_meta_headers(self):
|
||||||
|
headers = {'abc': '123',
|
||||||
|
u'x-container-meta-\u0394': '123',
|
||||||
|
u'x-account-meta-\u0394': '123',
|
||||||
|
u'x-object-meta-\u0394': '123'}
|
||||||
|
|
||||||
|
encoded_str_type = type(''.encode())
|
||||||
|
r = swiftclient.encode_meta_headers(headers)
|
||||||
|
|
||||||
|
self.assertEqual(len(headers), len(r))
|
||||||
|
# ensure non meta headers are not encoded
|
||||||
|
self.assertTrue('abc' in r)
|
||||||
|
self.assertTrue(isinstance(r['abc'], encoded_str_type))
|
||||||
|
del r['abc']
|
||||||
|
|
||||||
|
for k, v in r.items():
|
||||||
|
self.assertTrue(isinstance(k, encoded_str_type))
|
||||||
|
self.assertTrue(isinstance(v, encoded_str_type))
|
||||||
|
|
||||||
def test_set_user_agent_default(self):
|
def test_set_user_agent_default(self):
|
||||||
_junk, conn = c.http_connection('http://www.example.com')
|
_junk, conn = c.http_connection('http://www.example.com')
|
||||||
req_headers = {}
|
req_headers = {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user