Flaper Fesp 55cb4f4473 Decode input and encode output
Currently glanceclient doesn't support non-ASCII characters for images
names and properties (names and values as well). This patch introduces 2
functions (utils.py) that will help encoding and decoding strings in a
more "secure" way.

About the ensure_(str|unicode) functions:

    They both try to use first the encoding used in stdin (or python's
    default encoding if that's None) and fallback to utf-8 if those
    encodings fail to decode a given text.

About the changes in glanceclient:

    The major change is that all inputs will be decoded and will kept as
    such inside the client's functions and will then be encoded before
    being printed / sent out the client.

    There are other small changes, all related to encoding to str,
    around in order to avoid fails during some conversions. i.e: quoting
    url encoded parameters.

Fixes bug: 1061150

Change-Id: I5c3ea93a716edfe284d19f6291d4e36028f91eb2
2013-02-13 21:53:11 +01:00

92 lines
3.3 KiB
Python

# Copyright 2012 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import httplib
import socket
import StringIO
import testtools
import mox
from glanceclient import exc
from glanceclient.common import http
from tests import utils
class TestClient(testtools.TestCase):
def setUp(self):
super(TestClient, self).setUp()
self.mock = mox.Mox()
self.mock.StubOutWithMock(httplib.HTTPConnection, 'request')
self.mock.StubOutWithMock(httplib.HTTPConnection, 'getresponse')
self.endpoint = 'http://example.com:9292'
self.client = http.HTTPClient(self.endpoint, token=u'abc123')
def tearDown(self):
super(TestClient, self).tearDown()
self.mock.UnsetStubs()
def test_connection_refused(self):
"""
Should receive a CommunicationError if connection refused.
And the error should list the host and port that refused the
connection
"""
httplib.HTTPConnection.request(
mox.IgnoreArg(),
mox.IgnoreArg(),
headers=mox.IgnoreArg(),
).AndRaise(socket.error())
self.mock.ReplayAll()
try:
self.client.json_request('GET', '/v1/images/detail?limit=20')
#NOTE(alaski) We expect exc.CommunicationError to be raised
# so we should never reach this point. try/except is used here
# rather than assertRaises() so that we can check the body of
# the exception.
self.fail('An exception should have bypassed this line.')
except exc.CommunicationError, comm_err:
fail_msg = ("Exception message '%s' should contain '%s'" %
(comm_err.message, self.endpoint))
self.assertTrue(self.endpoint in comm_err.message, fail_msg)
def test_http_encoding(self):
httplib.HTTPConnection.request(
mox.IgnoreArg(),
mox.IgnoreArg(),
headers=mox.IgnoreArg())
# Lets fake the response
# returned by httplib
expected_response = 'Ok'
fake = utils.FakeResponse({}, StringIO.StringIO(expected_response))
httplib.HTTPConnection.getresponse().AndReturn(fake)
self.mock.ReplayAll()
headers = {"test": u'ni\xf1o'}
resp, body = self.client.raw_request('GET', '/v1/images/detail',
headers=headers)
self.assertEqual(resp, fake)
class TestResponseBodyIterator(testtools.TestCase):
def test_iter_default_chunk_size_64k(self):
resp = utils.FakeResponse({}, StringIO.StringIO('X' * 98304))
iterator = http.ResponseBodyIterator(resp)
chunks = list(iterator)
self.assertEqual(chunks, ['X' * 65536, 'X' * 32768])