1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
# Copyright (c) 2012 OpenStack Foundation
#
# 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 unittest
from swift.common.header_key_dict import HeaderKeyDict
from swift.common.swob import bytes_to_wsgi
class TestHeaderKeyDict(unittest.TestCase):
def test_case_insensitive(self):
headers = HeaderKeyDict()
headers['Content-Length'] = 0
headers['CONTENT-LENGTH'] = 10
headers['content-length'] = 20
self.assertEqual(headers['Content-Length'], '20')
self.assertEqual(headers['content-length'], '20')
self.assertEqual(headers['CONTENT-LENGTH'], '20')
def test_unicode(self):
def mkstr(prefix):
return bytes_to_wsgi((prefix + u'\U0001f44d').encode('utf8'))
headers = HeaderKeyDict()
headers[mkstr('x-object-meta-')] = 'ok'
self.assertIn(mkstr('x-object-meta-'), headers)
self.assertIn(mkstr('X-Object-Meta-'), headers)
self.assertIn(mkstr('X-OBJECT-META-'), headers)
keys = list(headers)
self.assertNotIn(mkstr('x-object-meta-'), keys)
self.assertIn(mkstr('X-Object-Meta-'), keys)
self.assertNotIn(mkstr('X-OBJECT-META-'), keys)
def test_setdefault(self):
headers = HeaderKeyDict()
# it gets set
headers.setdefault('x-rubber-ducky', 'the one')
self.assertEqual(headers['X-Rubber-Ducky'], 'the one')
# it has the right return value
ret = headers.setdefault('x-boat', 'dinghy')
self.assertEqual(ret, 'dinghy')
ret = headers.setdefault('x-boat', 'yacht')
self.assertEqual(ret, 'dinghy')
# shouldn't crash
headers.setdefault('x-sir-not-appearing-in-this-request', None)
def test_del_contains(self):
headers = HeaderKeyDict()
headers['Content-Length'] = 0
self.assertIn('Content-Length', headers)
del headers['Content-Length']
self.assertNotIn('Content-Length', headers)
def test_update(self):
headers = HeaderKeyDict()
headers.update({'Content-Length': '0'})
headers.update([('Content-Type', 'text/plain')])
self.assertEqual(headers['Content-Length'], '0')
self.assertEqual(headers['Content-Type'], 'text/plain')
def test_set_none(self):
headers = HeaderKeyDict()
headers['test'] = None
self.assertNotIn('test', headers)
headers['test'] = 'something'
self.assertEqual('something', headers['test']) # sanity check
headers['test'] = None
self.assertNotIn('test', headers)
def test_init_from_dict(self):
headers = HeaderKeyDict({'Content-Length': 20,
'Content-Type': 'text/plain'})
self.assertEqual('20', headers['Content-Length'])
self.assertEqual('text/plain', headers['Content-Type'])
headers = HeaderKeyDict(headers)
self.assertEqual('20', headers['Content-Length'])
self.assertEqual('text/plain', headers['Content-Type'])
def test_set(self):
# mappings = ((<tuple of input vals>, <expected output val>), ...)
mappings = (((1.618, '1.618', b'1.618', u'1.618'), '1.618'),
((20, '20', b'20', u'20'), '20'),
((True, 'True', b'True', u'True'), 'True'),
((False, 'False', b'False', u'False'), 'False'))
for vals, expected in mappings:
for val in vals:
headers = HeaderKeyDict(test=val)
actual = headers['test']
self.assertEqual(expected, actual,
'Expected %s but got %s for val %s' %
(expected, actual, val))
self.assertIsInstance(
actual, str,
'Expected type str but got %s for val %s of type %s' %
(type(actual), val, type(val)))
def test_get(self):
headers = HeaderKeyDict()
headers['content-length'] = 20
self.assertEqual(headers.get('CONTENT-LENGTH'), '20')
self.assertIsNone(headers.get('something-else'))
self.assertEqual(headers.get('something-else', True), True)
def test_keys(self):
headers = HeaderKeyDict()
headers['content-length'] = 20
headers['cOnTent-tYpe'] = 'text/plain'
headers['SomeThing-eLse'] = 'somevalue'
self.assertEqual(
set(headers.keys()),
set(('Content-Length', 'Content-Type', 'Something-Else')))
def test_pop(self):
headers = HeaderKeyDict()
headers['content-length'] = 20
headers['cOntent-tYpe'] = 'text/plain'
self.assertEqual(headers.pop('content-Length'), '20')
self.assertEqual(headers.pop('Content-type'), 'text/plain')
self.assertEqual(headers.pop('Something-Else', 'somevalue'),
'somevalue')
|