Skip to content
Newer
Older
100644 114 lines (95 sloc) 3 KB
d57f294 @maluke This commit aggregates most of the work done at the 2011 PyCon Pyrami…
maluke authored Mar 22, 2011
1 # -*- coding: utf-8 -*-
b79d672 @maluke Add a lot of tests.
maluke authored Feb 20, 2011
2 from webob import headers
d57f294 @maluke This commit aggregates most of the work done at the 2011 PyCon Pyrami…
maluke authored Mar 21, 2011
3 from nose.tools import ok_, assert_raises, eq_
b79d672 @maluke Add a lot of tests.
maluke authored Feb 20, 2011
4
5 class TestError(Exception):
6 pass
7
d57f294 @maluke This commit aggregates most of the work done at the 2011 PyCon Pyrami…
maluke authored Mar 21, 2011
8 def test_ResponseHeaders_delitem_notpresent():
9 """Deleting a missing key from ResponseHeaders should raise a KeyError"""
b79d672 @maluke Add a lot of tests.
maluke authored Feb 20, 2011
10 d = headers.ResponseHeaders()
11 assert_raises(KeyError, d.__delitem__, 'b')
d57f294 @maluke This commit aggregates most of the work done at the 2011 PyCon Pyrami…
maluke authored Mar 21, 2011
12
13 def test_ResponseHeaders_delitem_present():
14 """
15 Deleting a present key should not raise an error at all
16 """
b79d672 @maluke Add a lot of tests.
maluke authored Feb 20, 2011
17 d = headers.ResponseHeaders(a=1)
18 del d['a']
19 ok_('a' not in d)
20
d57f294 @maluke This commit aggregates most of the work done at the 2011 PyCon Pyrami…
maluke authored Mar 21, 2011
21 def test_ResponseHeaders_setdefault():
b79d672 @maluke Add a lot of tests.
maluke authored Feb 20, 2011
22 """Testing set_default for ResponseHeaders"""
23 d = headers.ResponseHeaders(a=1)
24 res = d.setdefault('b', 1)
25 assert res == d['b'] == 1
26 res = d.setdefault('b', 10)
27 assert res == d['b'] == 1
28 res = d.setdefault('B', 10)
29 assert res == d['b'] == d['B'] == 1
30
d57f294 @maluke This commit aggregates most of the work done at the 2011 PyCon Pyrami…
maluke authored Mar 21, 2011
31 def test_ResponseHeader_pop():
b79d672 @maluke Add a lot of tests.
maluke authored Feb 20, 2011
32 """Testing if pop return TypeError when more than len(*args)>1 plus other
33 assorted tests"""
34 d = headers.ResponseHeaders(a=1, b=2, c=3, d=4)
35 assert_raises(TypeError, d.pop, 'a', 'z', 'y')
d57f294 @maluke This commit aggregates most of the work done at the 2011 PyCon Pyrami…
maluke authored Mar 21, 2011
36 eq_(d.pop('a'), 1)
37 ok_('a' not in d)
38 eq_(d.pop('B'), 2)
39 ok_('b' not in d)
40 eq_(d.pop('c', 'u'), 3)
41 ok_('c' not in d)
42 eq_(d.pop('e', 'u'), 'u')
43 ok_('e' not in d)
b79d672 @maluke Add a lot of tests.
maluke authored Feb 20, 2011
44 assert_raises(KeyError, d.pop, 'z')
45
d57f294 @maluke This commit aggregates most of the work done at the 2011 PyCon Pyrami…
maluke authored Mar 21, 2011
46 def test_ResponseHeaders_getitem_miss():
47 d = headers.ResponseHeaders()
48 assert_raises(KeyError, d.__getitem__, 'a')
49
50 def test_ResponseHeaders_getall():
51 d = headers.ResponseHeaders()
52 d.add('a', 1)
53 d.add('a', 2)
54 result = d.getall('a')
55 eq_(result, [1,2])
56
57 def test_ResponseHeaders_mixed():
58 d = headers.ResponseHeaders()
59 d.add('a', 1)
60 d.add('a', 2)
61 d['b'] = 1
62 result = d.mixed()
63 eq_(result, {'a':[1,2], 'b':1})
64
65 def test_ResponseHeaders_setitem_scalar_replaces_seq():
66 d = headers.ResponseHeaders()
67 d.add('a', 2)
68 d['a'] = 1
69 result = d.getall('a')
70 eq_(result, [1])
71
72 def test_ResponseHeaders_contains():
73 d = headers.ResponseHeaders()
74 d['a'] = 1
75 ok_('a' in d)
76 ok_(not 'b' in d)
77
3f71d70 @mcdonc give up on trying to encode header values when request.headers is used
mcdonc authored Jan 28, 2012
78 def test_EnvironHeaders_delitem():
79 d = headers.EnvironHeaders({'CONTENT_LENGTH': '10'})
80 del d['CONTENT-LENGTH']
81 assert not d
82 assert_raises(KeyError, d.__delitem__, 'CONTENT-LENGTH')
83
84 def test_EnvironHeaders_getitem():
85 d = headers.EnvironHeaders({'CONTENT_LENGTH': '10'})
86 eq_(d['CONTENT-LENGTH'], '10')
87
88 def test_EnvironHeaders_setitem():
89 d = headers.EnvironHeaders({})
90 d['abc'] = '10'
91 eq_(d['abc'], '10')
92
93 def test_EnvironHeaders_contains():
94 d = headers.EnvironHeaders({})
95 d['a'] = '10'
96 ok_('a' in d)
97 ok_(not 'b' in d)
98
d57f294 @maluke This commit aggregates most of the work done at the 2011 PyCon Pyrami…
maluke authored Mar 21, 2011
99 def test__trans_key_not_basestring():
100 result = headers._trans_key(None)
101 eq_(result, None)
102
103 def test__trans_key_not_a_header():
104 result = headers._trans_key('')
105 eq_(result, None)
106
107 def test__trans_key_key2header():
108 result = headers._trans_key('CONTENT_TYPE')
109 eq_(result, 'Content-Type')
110
111 def test__trans_key_httpheader():
112 result = headers._trans_key('HTTP_FOO_BAR')
113 eq_(result, 'Foo-Bar')
Something went wrong with that request. Please try again.