def test_from_httplib_py2(self): msg = """ Server: nginx Content-Type: text/html; charset=windows-1251 Connection: keep-alive X-Some-Multiline: asdf asdf\t \t asdf Set-Cookie: bb_lastvisit=1348253375; expires=Sat, 21-Sep-2013 18:49:35 GMT; path=/ Set-Cookie: bb_lastactivity=0; expires=Sat, 21-Sep-2013 18:49:35 GMT; path=/ www-authenticate: asdf www-authenticate: bla """ buffer = six.moves.StringIO(msg.lstrip().replace('\n', '\r\n')) msg = six.moves.http_client.HTTPMessage(buffer) d = HTTPHeaderDict.from_httplib(msg) assert d['server'] == 'nginx' cookies = d.getlist('set-cookie') assert len(cookies) == 2 assert cookies[0].startswith("bb_lastvisit") assert cookies[1].startswith("bb_lastactivity") assert d['x-some-multiline'] == 'asdf asdf asdf' assert d['www-authenticate'] == 'asdf, bla' assert d.getlist('www-authenticate') == ['asdf', 'bla'] with_invalid_multiline = """\tthis-is-not-a-header: but it has a pretend value Authorization: Bearer 123 """ buffer = six.moves.StringIO( with_invalid_multiline.replace('\n', '\r\n')) msg = six.moves.http_client.HTTPMessage(buffer) with pytest.raises(InvalidHeader): HTTPHeaderDict.from_httplib(msg)
def test_dict_conversion(self, d): # Also tested in connectionpool, needs to preserve case hdict = { 'Content-Length': '0', 'Content-type': 'text/plain', 'Server': 'TornadoServer/1.2.3' } h = dict(HTTPHeaderDict(hdict).items()) assert hdict == h assert hdict == dict(HTTPHeaderDict(hdict))
def test_create_from_list(self): headers = [('ab', 'A'), ('cd', 'B'), ('cookie', 'C'), ('cookie', 'D'), ('cookie', 'E')] h = HTTPHeaderDict(headers) assert len(h) == 3 assert 'ab' in h clist = h.getlist('cookie') assert len(clist) == 3 assert clist[0] == 'C' assert clist[-1] == 'E'
def test_create_from_headerdict(self): headers = [('ab', 'A'), ('cd', 'B'), ('cookie', 'C'), ('cookie', 'D'), ('cookie', 'E')] org = HTTPHeaderDict(headers) h = HTTPHeaderDict(org) assert len(h) == 3 assert 'ab' in h clist = h.getlist('cookie') assert len(clist) == 3 assert clist[0] == 'C' assert clist[-1] == 'E' assert h is not org assert h == org
def test_string_enforcement(self, d): # This currently throws AttributeError on key.lower(), should # probably be something nicer with pytest.raises(Exception): d[3] = 5 with pytest.raises(Exception): d.add(3, 4) with pytest.raises(Exception): del d[3] with pytest.raises(Exception): HTTPHeaderDict({3: 3})
def test_not_equal(self, d): b = HTTPHeaderDict(cookie='foo, bar') c = NonMappingHeaderContainer(cookie='foo, bar') assert not (d != b) assert not (d != c) assert d != 2
def test_equal(self, d): b = HTTPHeaderDict(cookie='foo, bar') c = NonMappingHeaderContainer(cookie='foo, bar') assert d == b assert d == c assert d != 2
def test_getlist_after_copy(self, d): assert d.getlist('cookie') == HTTPHeaderDict(d).getlist('cookie')
def test_extend_from_headerdict(self, d): h = HTTPHeaderDict(Cookie='foo', e='foofoo') d.extend(h) assert d['cookie'] == 'foo, bar, foo' assert d['e'] == 'foofoo' assert len(d) == 2
def test_create_from_iterator(self): teststr = 'urllib3ontherocks' h = HTTPHeaderDict((c, c * 5) for c in teststr) assert len(h) == len(set(teststr))
def test_create_from_dict(self): h = HTTPHeaderDict(dict(ab=1, cd=2, ef=3, gh=4)) assert len(h) == 4 assert 'ab' in h
def d(): header_dict = HTTPHeaderDict(Cookie='foo') header_dict.add('cookie', 'bar') return header_dict