def test_get_form_urlencoded(self): req = tutils.treq("foobar") assert req.get_form_urlencoded() == odict.ODict() req.headers["Content-Type"] = semantics.HDR_FORM_URLENCODED assert req.get_form_urlencoded() == odict.ODict( utils.urldecode(req.body))
def test_get_form_multipart(self): req = tutils.treq("foobar") assert req.get_form_multipart() == odict.ODict() req.headers["Content-Type"] = semantics.HDR_FORM_MULTIPART assert req.get_form_multipart() == odict.ODict( utils.multipartdecode(req.headers, req.body))
def test_get_cookies_twocookies(self): resp = tutils.tresp() resp.headers = http.Headers([["Set-Cookie", "cookiename=cookievalue"], ["Set-Cookie", "othercookie=othervalue"]]) result = resp.get_cookies() assert len(result) == 2 assert "cookiename" in result assert result["cookiename"][0] == ["cookievalue", odict.ODict()] assert "othercookie" in result assert result["othercookie"][0] == ["othervalue", odict.ODict()]
def test_get_cookies_twocookies(self): h = odict.ODictCaseless() h["Set-Cookie"] = ["cookiename=cookievalue", "othercookie=othervalue"] resp = tutils.tresp() resp.headers = h result = resp.get_cookies() assert len(result) == 2 assert "cookiename" in result assert result["cookiename"][0] == ["cookievalue", odict.ODict()] assert "othercookie" in result assert result["othercookie"][0] == ["othervalue", odict.ODict()]
def test_getset_state(self): od = odict.ODict() od.add("foo", 1) od.add("foo", 2) od.add("bar", 3) state = od.get_state() nd = odict.ODict.from_state(state) assert nd == od b = odict.ODict() b.set_state(state) assert b == od
def test_getset_form_urlencoded(self): d = odict.ODict([("one", "two"), ("three", "four")]) r = tutils.treq(content=utils.urlencode(d.lst)) r.headers["content-type"] = [protocol.http.HDR_FORM_URLENCODED] assert r.get_form_urlencoded() == d d = odict.ODict([("x", "y")]) r.set_form_urlencoded(d) assert r.get_form_urlencoded() == d r.headers["content-type"] = ["foo"] assert not r.get_form_urlencoded()
def test_getset_form_urlencoded(self): d = odict.ODict([("one", "two"), ("three", "four")]) r = HTTPRequest.wrap(netlib.tutils.treq(content=netlib.utils.urlencode(d.lst))) r.headers["content-type"] = [HDR_FORM_URLENCODED] assert r.get_form_urlencoded() == d d = odict.ODict([("x", "y")]) r.set_form_urlencoded(d) assert r.get_form_urlencoded() == d r.headers["content-type"] = ["foo"] assert not r.get_form_urlencoded()
def test_getset_form_urlencoded(self): d = odict.ODict([("one", "two"), ("three", "four")]) r = HTTPRequest.wrap( netlib.tutils.treq(content=netlib.utils.urlencode(d.lst))) r.headers["content-type"] = "application/x-www-form-urlencoded" assert r.get_form_urlencoded() == d d = odict.ODict([("x", "y")]) r.set_form_urlencoded(d) assert r.get_form_urlencoded() == d r.headers["content-type"] = "foo" assert not r.get_form_urlencoded()
def test_copy(self): od = odict.ODict() od.add("foo", 1) od.add("foo", 2) od.add("bar", 3) assert od == od.copy() assert not od != od.copy()
def test_get_cookies_simple(self): resp = tutils.tresp() resp.headers = http.Headers(set_cookie="cookiename=cookievalue") result = resp.get_cookies() assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0] == ["cookievalue", odict.ODict()]
def test_del(self): od = odict.ODict() od.add("foo", 1) od.add("Foo", 2) od.add("bar", 3) del od["foo"] assert len(od.lst) == 2
def test_match_re(self): h = odict.ODict() h.add("one", "uno") h.add("two", "due") h.add("two", "tre") assert h.match_re("uno") assert h.match_re("two: due") assert not h.match_re("nonono")
def test_in_any(self): od = odict.ODict() od["one"] = ["atwoa", "athreea"] assert od.in_any("one", "two") assert od.in_any("one", "three") assert not od.in_any("one", "four") assert not od.in_any("nonexistent", "foo") assert not od.in_any("one", "TWO") assert od.in_any("one", "TWO", True)
def test_replace(self): od = odict.ODict() od.add("one", "two") od.add("two", "one") assert od.replace("one", "vun") == 2 assert od.lst == [ ["vun", "two"], ["two", "vun"], ]
def test_keys(self): od = odict.ODict() assert not od.keys() od.add("foo", 1) assert od.keys() == ["foo"] od.add("foo", 2) assert od.keys() == ["foo"] od.add("bar", 2) assert len(od.keys()) == 2
def test_get_form_with_url_encoded(self, mock_method_urlencoded, mock_method_multipart): req = tutils.treq() assert req.get_form() == odict.ODict() req = tutils.treq() req.body = "foobar" req.headers["Content-Type"] = [semantics.HDR_FORM_URLENCODED] req.get_form() assert req.get_form_urlencoded.called assert not req.get_form_multipart.called
def test_getset_state(self): self.od.add("foo", 1) self.od.add("foo", 2) self.od.add("bar", 3) state = self.od.get_state() nd = odict.ODict.from_state(state) assert nd == self.od b = odict.ODict() b.load_state(state) assert b == self.od
def test_getset_query(self): r = HTTPRequest.wrap(netlib.tutils.treq()) r.path = "/foo?x=y&a=b" q = r.get_query() assert q.lst == [("x", "y"), ("a", "b")] r.path = "/" q = r.get_query() assert not q r.path = "/?adsfa" q = r.get_query() assert q.lst == [("adsfa", "")] r.path = "/foo?x=y&a=b" assert r.get_query() r.set_query(odict.ODict([])) assert not r.get_query() qv = odict.ODict([("a", "b"), ("c", "d")]) r.set_query(qv) assert r.get_query() == qv
def test_add_order(self): od = odict.ODict([ ["one", "uno"], ["two", "due"], ["three", "tre"], ]) od["two"] = ["foo", "bar"] assert od.lst == [ ["one", "uno"], ["two", "foo"], ["three", "tre"], ["two", "bar"], ]
def test_getset_query(self): h = odict.ODictCaseless() r = tutils.treq() r.path = "/foo?x=y&a=b" q = r.get_query() assert q.lst == [("x", "y"), ("a", "b")] r.path = "/" q = r.get_query() assert not q r.path = "/?adsfa" q = r.get_query() assert q.lst == [("adsfa", "")] r.path = "/foo?x=y&a=b" assert r.get_query() r.set_query(odict.ODict([])) assert not r.get_query() qv = odict.ODict([("a", "b"), ("c", "d")]) r.set_query(qv) assert r.get_query() == qv
def test_multipartdecode(): boundary = 'somefancyboundary' headers = odict.ODict([('content-type', ('multipart/form-data; boundary=%s' % boundary))]) content = "--{0}\n" \ "Content-Disposition: form-data; name=\"field1\"\n\n" \ "value1\n" \ "--{0}\n" \ "Content-Disposition: form-data; name=\"field2\"\n\n" \ "value2\n" \ "--{0}--".format(boundary) form = utils.multipartdecode(headers, content) assert len(form) == 2 assert form[0] == ('field1', 'value1') assert form[1] == ('field2', 'value2')
def test_path_components(self): r = HTTPRequest.wrap(netlib.tutils.treq()) r.path = "/" assert r.get_path_components() == [] r.path = "/foo/bar" assert r.get_path_components() == ["foo", "bar"] q = odict.ODict() q["test"] = ["123"] r.set_query(q) assert r.get_path_components() == ["foo", "bar"] r.set_path_components([]) assert r.get_path_components() == [] r.set_path_components(["foo"]) assert r.get_path_components() == ["foo"] r.set_path_components(["/oo"]) assert r.get_path_components() == ["/oo"] assert "%2F" in r.path
def __call__(self, data, **metadata): try: img = Image.open(StringIO(data)) except IOError: return None parts = [ ("Format", str(img.format_description)), ("Size", "%s x %s px" % img.size), ("Mode", str(img.mode)), ] for i in sorted(img.info.keys()): if i != "exif": parts.append((str(i), str(img.info[i]))) if hasattr(img, "_getexif"): ex = img._getexif() if ex: for i in sorted(ex.keys()): tag = ExifTags.TAGS.get(i, i) parts.append((str(tag), str(ex[i]))) fmt = format_dict(odict.ODict(parts)) return "%s image" % img.format, fmt
def test_extend(self): a = odict.ODict([["a", "b"], ["c", "d"]]) b = odict.ODict([["a", "b"], ["e", "f"]]) a.extend(b) assert len(a) == 4 assert a["a"] == ["b", "b"]
def test_get_first(self): od = odict.ODict() od.add("one", "two") od.add("one", "three") assert od.get_first("one") == "two" assert od.get_first("two") is None
def test_get(self): od = odict.ODict() od.add("one", "two") assert od.get("one") == ["two"] assert od.get("two") is None
def test_set_query(self): req = tutils.treq() req.set_query(odict.ODict([]))
def test_str_err(self): h = odict.ODict() with tutils.raises(ValueError): h["key"] = u"foo" with tutils.raises(ValueError): h["key"] = b"foo"
def test_repr(self): h = odict.ODict() h["one"] = ["two"] assert repr(h)
def test_iter(self): od = odict.ODict() assert not [i for i in od] od.add("foo", 1) assert [i for i in od]