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_headerdict(self): default_headers = HTTPHeaderDict(a="b") proxy_headers = HTTPHeaderDict() proxy_headers.add("foo", "bar") with proxy_from_url(self.proxy_url, headers=default_headers, proxy_headers=proxy_headers) as http: request_headers = HTTPHeaderDict(baz="quux") r = http.request("GET", "%s/headers" % self.http_url, headers=request_headers) returned_headers = json.loads(r.data.decode()) assert returned_headers.get("Foo") == "bar" assert returned_headers.get("Baz") == "quux"
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 = "hipontherocks" 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