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
|
# -*- coding: utf-8 -*-
from webob import headers
from nose.tools import ok_, assert_raises, eq_
class TestError(Exception):
pass
def test_ResponseHeaders_delitem_notpresent():
"""Deleting a missing key from ResponseHeaders should raise a KeyError"""
d = headers.ResponseHeaders()
assert_raises(KeyError, d.__delitem__, 'b')
def test_ResponseHeaders_delitem_present():
"""
Deleting a present key should not raise an error at all
"""
d = headers.ResponseHeaders(a=1)
del d['a']
ok_('a' not in d)
def test_ResponseHeaders_setdefault():
"""Testing set_default for ResponseHeaders"""
d = headers.ResponseHeaders(a=1)
res = d.setdefault('b', 1)
assert res == d['b'] == 1
res = d.setdefault('b', 10)
assert res == d['b'] == 1
res = d.setdefault('B', 10)
assert res == d['b'] == d['B'] == 1
def test_ResponseHeader_pop():
"""Testing if pop return TypeError when more than len(*args)>1 plus other
assorted tests"""
d = headers.ResponseHeaders(a=1, b=2, c=3, d=4)
assert_raises(TypeError, d.pop, 'a', 'z', 'y')
eq_(d.pop('a'), 1)
ok_('a' not in d)
eq_(d.pop('B'), 2)
ok_('b' not in d)
eq_(d.pop('c', 'u'), 3)
ok_('c' not in d)
eq_(d.pop('e', 'u'), 'u')
ok_('e' not in d)
assert_raises(KeyError, d.pop, 'z')
def test_ResponseHeaders_getitem_miss():
d = headers.ResponseHeaders()
assert_raises(KeyError, d.__getitem__, 'a')
def test_ResponseHeaders_getall():
d = headers.ResponseHeaders()
d.add('a', 1)
d.add('a', 2)
result = d.getall('a')
eq_(result, [1,2])
def test_ResponseHeaders_mixed():
d = headers.ResponseHeaders()
d.add('a', 1)
d.add('a', 2)
d['b'] = 1
result = d.mixed()
eq_(result, {'a':[1,2], 'b':1})
def test_ResponseHeaders_setitem_scalar_replaces_seq():
d = headers.ResponseHeaders()
d.add('a', 2)
d['a'] = 1
result = d.getall('a')
eq_(result, [1])
def test_ResponseHeaders_contains():
d = headers.ResponseHeaders()
d['a'] = 1
ok_('a' in d)
ok_(not 'b' in d)
def test_EnvironHeaders_delitem():
d = headers.EnvironHeaders({'CONTENT_LENGTH': '10'})
del d['CONTENT-LENGTH']
assert not d
assert_raises(KeyError, d.__delitem__, 'CONTENT-LENGTH')
def test_EnvironHeaders_getitem():
d = headers.EnvironHeaders({'CONTENT_LENGTH': '10'})
eq_(d['CONTENT-LENGTH'], '10')
def test_EnvironHeaders_setitem():
d = headers.EnvironHeaders({})
d['abc'] = '10'
eq_(d['abc'], '10')
def test_EnvironHeaders_contains():
d = headers.EnvironHeaders({})
d['a'] = '10'
ok_('a' in d)
ok_(not 'b' in d)
def test__trans_key_not_basestring():
result = headers._trans_key(None)
eq_(result, None)
def test__trans_key_not_a_header():
result = headers._trans_key('')
eq_(result, None)
def test__trans_key_key2header():
result = headers._trans_key('CONTENT_TYPE')
eq_(result, 'Content-Type')
def test__trans_key_httpheader():
result = headers._trans_key('HTTP_FOO_BAR')
eq_(result, 'Foo-Bar')
|