Add tests for encodings

The patch adds tests for headers encodings, incoming data decoding,
outgoing meta encoding and filters encoding.

Fixes bug: #1187013

Change-Id: I7e59bf6c44b9d188bd3faf32fc09f309f3ad67d3
This commit is contained in:
Flaper Fesp
2013-06-03 16:36:07 +02:00
parent 7d48783434
commit 81e88344fb
3 changed files with 41 additions and 1 deletions

View File

@@ -84,6 +84,11 @@ class TestClient(testtools.TestCase):
headers=headers)
self.assertEqual(resp, fake)
def test_headers_encoding(self):
headers = {"test": u'ni\xf1o'}
encoded = self.client.encode_headers(headers)
self.assertEqual(encoded["test"], "ni\xc3\xb1o")
def test_connection_refused_raw_request(self):
"""
Should receive a CommunicationError if connection refused.

View File

@@ -230,7 +230,8 @@ fixtures = {
'/v1/images/3': {
'HEAD': (
{
'x-image-meta-id': '3'
'x-image-meta-id': '3',
'x-image-meta-name': "ni\xc3\xb1o"
},
None,
),
@@ -313,6 +314,11 @@ class ImageManagerTest(testtools.TestCase):
self.assertEqual(image.deleted, False)
self.assertEqual(image.properties, {u'arch': u'x86_64'})
def test_get_encoding(self):
image = self.mgr.get('3')
expect = [('HEAD', '/v1/images/3', {}, None)]
self.assertEqual(image.name, u"ni\xf1o")
def test_data(self):
data = ''.join([b for b in self.mgr.data('1', do_checksum=False)])
expect = [('GET', '/v1/images/1', {}, None)]
@@ -454,6 +460,22 @@ class ImageManagerTest(testtools.TestCase):
expect = [('PUT', '/v1/images/1', expect_headers, None)]
self.assertEqual(self.api.calls, expect)
def test_image_meta_to_headers_encoding(self):
# NOTE(flaper87): This doesn't make much sense
# _image_meta_to_headers decodes headers that will
# then be encoded before sending the request. If
# everything works as expected, there shouldn't be
# any need to enforce unicode in headers at this step.
# Will get rid of that in a separate patch.
fields = {"name": "ni\xc3\xb1o"}
headers = self.mgr._image_meta_to_headers(fields)
self.assertEqual(headers["x-image-meta-name"], u"ni\xf1o")
def test_image_meta_from_headers_encoding(self):
fields = {"x-image-meta-name": "ni\xc3\xb1o"}
headers = self.mgr._image_meta_from_headers(fields)
self.assertEqual(headers["name"], u"ni\xf1o")
class ImageTest(testtools.TestCase):
def setUp(self):

View File

@@ -255,6 +255,19 @@ class TestController(testtools.TestCase):
images = list(self.controller.list(**filters))
self.assertEqual(images[0].id, _EVERYTHING_ID)
def test_list_images_filters_encoding(self):
filters = {"owner": u"ni\xf1o"}
try:
list(self.controller.list(filters=filters))
except KeyError:
# NOTE(flaper87): It raises KeyError because there's
# no fixture supporting this query:
# /v2/images?owner=ni%C3%B1o&limit=20
# We just want to make sure filters are correctly encoded.
pass
self.assertEqual(filters["owner"], "ni\xc3\xb1o")
def test_get_image(self):
image = self.controller.get('3a4560a1-e585-443e-9b39-553b46ec92d1')
self.assertEqual(image.id, '3a4560a1-e585-443e-9b39-553b46ec92d1')