aboutsummaryrefslogtreecommitdiffstats
path: root/test/netlib
diff options
context:
space:
mode:
Diffstat (limited to 'test/netlib')
-rw-r--r--test/netlib/http/http1/test_read.py8
-rw-r--r--test/netlib/http/http2/test_connections.py6
-rw-r--r--test/netlib/http/test_cookies.py18
-rw-r--r--test/netlib/http/test_headers.py109
-rw-r--r--test/netlib/http/test_request.py65
-rw-r--r--test/netlib/http/test_response.py34
-rw-r--r--test/netlib/test_multidict.py217
7 files changed, 289 insertions, 168 deletions
diff --git a/test/netlib/http/http1/test_read.py b/test/netlib/http/http1/test_read.py
index 90234070..d8106904 100644
--- a/test/netlib/http/http1/test_read.py
+++ b/test/netlib/http/http1/test_read.py
@@ -261,7 +261,7 @@ class TestReadHeaders(object):
b"\r\n"
)
headers = self._read(data)
- assert headers.fields == [[b"Header", b"one"], [b"Header2", b"two"]]
+ assert headers.fields == ((b"Header", b"one"), (b"Header2", b"two"))
def test_read_multi(self):
data = (
@@ -270,7 +270,7 @@ class TestReadHeaders(object):
b"\r\n"
)
headers = self._read(data)
- assert headers.fields == [[b"Header", b"one"], [b"Header", b"two"]]
+ assert headers.fields == ((b"Header", b"one"), (b"Header", b"two"))
def test_read_continued(self):
data = (
@@ -280,7 +280,7 @@ class TestReadHeaders(object):
b"\r\n"
)
headers = self._read(data)
- assert headers.fields == [[b"Header", b"one\r\n two"], [b"Header2", b"three"]]
+ assert headers.fields == ((b"Header", b"one\r\n two"), (b"Header2", b"three"))
def test_read_continued_err(self):
data = b"\tfoo: bar\r\n"
@@ -300,7 +300,7 @@ class TestReadHeaders(object):
def test_read_empty_value(self):
data = b"bar:"
headers = self._read(data)
- assert headers.fields == [[b"bar", b""]]
+ assert headers.fields == ((b"bar", b""),)
def test_read_chunked():
req = treq(content=None)
diff --git a/test/netlib/http/http2/test_connections.py b/test/netlib/http/http2/test_connections.py
index 7b003067..7d240c0e 100644
--- a/test/netlib/http/http2/test_connections.py
+++ b/test/netlib/http/http2/test_connections.py
@@ -312,7 +312,7 @@ class TestReadRequest(tservers.ServerTestBase):
req = protocol.read_request(NotImplemented)
assert req.stream_id
- assert req.headers.fields == [[b':method', b'GET'], [b':path', b'/'], [b':scheme', b'https']]
+ assert req.headers.fields == ((b':method', b'GET'), (b':path', b'/'), (b':scheme', b'https'))
assert req.content == b'foobar'
@@ -418,7 +418,7 @@ class TestReadResponse(tservers.ServerTestBase):
assert resp.http_version == "HTTP/2.0"
assert resp.status_code == 200
assert resp.reason == ''
- assert resp.headers.fields == [[b':status', b'200'], [b'etag', b'foobar']]
+ assert resp.headers.fields == ((b':status', b'200'), (b'etag', b'foobar'))
assert resp.content == b'foobar'
assert resp.timestamp_end
@@ -445,7 +445,7 @@ class TestReadEmptyResponse(tservers.ServerTestBase):
assert resp.http_version == "HTTP/2.0"
assert resp.status_code == 200
assert resp.reason == ''
- assert resp.headers.fields == [[b':status', b'200'], [b'etag', b'foobar']]
+ assert resp.headers.fields == ((b':status', b'200'), (b'etag', b'foobar'))
assert resp.content == b''
diff --git a/test/netlib/http/test_cookies.py b/test/netlib/http/test_cookies.py
index da28850f..6f84c4ce 100644
--- a/test/netlib/http/test_cookies.py
+++ b/test/netlib/http/test_cookies.py
@@ -128,10 +128,10 @@ def test_cookie_roundtrips():
]
for s, lst in pairs:
ret = cookies.parse_cookie_header(s)
- assert ret.lst == lst
+ assert ret == lst
s2 = cookies.format_cookie_header(ret)
ret = cookies.parse_cookie_header(s2)
- assert ret.lst == lst
+ assert ret == lst
def test_parse_set_cookie_pairs():
@@ -197,24 +197,28 @@ def test_parse_set_cookie_header():
],
[
"one=uno",
- ("one", "uno", [])
+ ("one", "uno", ())
],
[
"one=uno; foo=bar",
- ("one", "uno", [["foo", "bar"]])
- ]
+ ("one", "uno", (("foo", "bar"),))
+ ],
+ [
+ "one=uno; foo=bar; foo=baz",
+ ("one", "uno", (("foo", "bar"), ("foo", "baz")))
+ ],
]
for s, expected in vals:
ret = cookies.parse_set_cookie_header(s)
if expected:
assert ret[0] == expected[0]
assert ret[1] == expected[1]
- assert ret[2].lst == expected[2]
+ assert ret[2].items(multi=True) == expected[2]
s2 = cookies.format_set_cookie_header(*ret)
ret2 = cookies.parse_set_cookie_header(s2)
assert ret2[0] == expected[0]
assert ret2[1] == expected[1]
- assert ret2[2].lst == expected[2]
+ assert ret2[2].items(multi=True) == expected[2]
else:
assert ret is None
diff --git a/test/netlib/http/test_headers.py b/test/netlib/http/test_headers.py
index 8c1db9dc..cd2ca9d1 100644
--- a/test/netlib/http/test_headers.py
+++ b/test/netlib/http/test_headers.py
@@ -5,10 +5,10 @@ from netlib.tutils import raises
class TestHeaders(object):
def _2host(self):
return Headers(
- [
- [b"Host", b"example.com"],
- [b"host", b"example.org"]
- ]
+ (
+ (b"Host", b"example.com"),
+ (b"host", b"example.org")
+ )
)
def test_init(self):
@@ -38,20 +38,10 @@ class TestHeaders(object):
assert headers["Host"] == "example.com"
assert headers["Accept"] == "text/plain"
- with raises(ValueError):
+ with raises(TypeError):
Headers([[b"Host", u"not-bytes"]])
- def test_getitem(self):
- headers = Headers(Host="example.com")
- assert headers["Host"] == "example.com"
- assert headers["host"] == "example.com"
- with raises(KeyError):
- _ = headers["Accept"]
-
- headers = self._2host()
- assert headers["Host"] == "example.com, example.org"
-
- def test_str(self):
+ def test_bytes(self):
headers = Headers(Host="example.com")
assert bytes(headers) == b"Host: example.com\r\n"
@@ -64,93 +54,6 @@ class TestHeaders(object):
headers = Headers()
assert bytes(headers) == b""
- def test_setitem(self):
- headers = Headers()
- headers["Host"] = "example.com"
- assert "Host" in headers
- assert "host" in headers
- assert headers["Host"] == "example.com"
-
- headers["host"] = "example.org"
- assert "Host" in headers
- assert "host" in headers
- assert headers["Host"] == "example.org"
-
- headers["accept"] = "text/plain"
- assert len(headers) == 2
- assert "Accept" in headers
- assert "Host" in headers
-
- headers = self._2host()
- assert len(headers.fields) == 2
- headers["Host"] = "example.com"
- assert len(headers.fields) == 1
- assert "Host" in headers
-
- def test_delitem(self):
- headers = Headers(Host="example.com")
- assert len(headers) == 1
- del headers["host"]
- assert len(headers) == 0
- try:
- del headers["host"]
- except KeyError:
- assert True
- else:
- assert False
-
- headers = self._2host()
- del headers["Host"]
- assert len(headers) == 0
-
- def test_keys(self):
- headers = Headers(Host="example.com")
- assert list(headers.keys()) == ["Host"]
-
- headers = self._2host()
- assert list(headers.keys()) == ["Host"]
-
- def test_eq_ne(self):
- headers1 = Headers(Host="example.com")
- headers2 = Headers(host="example.com")
- assert not (headers1 == headers2)
- assert headers1 != headers2
-
- headers1 = Headers(Host="example.com")
- headers2 = Headers(Host="example.com")
- assert headers1 == headers2
- assert not (headers1 != headers2)
-
- assert headers1 != 42
-
- def test_get_all(self):
- headers = self._2host()
- assert headers.get_all("host") == ["example.com", "example.org"]
- assert headers.get_all("accept") == []
-
- def test_set_all(self):
- headers = Headers(Host="example.com")
- headers.set_all("Accept", ["text/plain"])
- assert len(headers) == 2
- assert "accept" in headers
-
- headers = self._2host()
- headers.set_all("Host", ["example.org"])
- assert headers["host"] == "example.org"
-
- headers.set_all("Host", ["example.org", "example.net"])
- assert headers["host"] == "example.org, example.net"
-
- def test_state(self):
- headers = self._2host()
- assert len(headers.get_state()) == 2
- assert headers == Headers.from_state(headers.get_state())
-
- headers2 = Headers()
- assert headers != headers2
- headers2.set_state(headers.get_state())
- assert headers == headers2
-
def test_replace_simple(self):
headers = Headers(Host="example.com", Accept="text/plain")
replacements = headers.replace("Host: ", "X-Host: ")
diff --git a/test/netlib/http/test_request.py b/test/netlib/http/test_request.py
index 7ed6bd0f..eefdc091 100644
--- a/test/netlib/http/test_request.py
+++ b/test/netlib/http/test_request.py
@@ -12,7 +12,7 @@ from .test_message import _test_decoded_attr, _test_passthrough_attr
class TestRequestData(object):
def test_init(self):
- with raises(ValueError if six.PY2 else TypeError):
+ with raises(ValueError):
treq(headers="foobar")
assert isinstance(treq(headers=None).headers, Headers)
@@ -158,16 +158,17 @@ class TestRequestUtils(object):
def test_get_query(self):
request = treq()
- assert request.query is None
+ assert not request.query
request.url = "http://localhost:80/foo?bar=42"
- assert request.query.lst == [("bar", "42")]
+ assert dict(request.query) == {"bar": "42"}
def test_set_query(self):
- request = treq(host=b"foo", headers = Headers(host=b"bar"))
- request.query = ODict([])
- assert request.host == "foo"
- assert request.headers["host"] == "bar"
+ request = treq()
+ assert not request.query
+ request.query["foo"] = "bar"
+ assert request.query["foo"] == "bar"
+ assert request.path == "/path?foo=bar"
def test_get_cookies_none(self):
request = treq()
@@ -177,47 +178,50 @@ class TestRequestUtils(object):
def test_get_cookies_single(self):
request = treq()
request.headers = Headers(cookie="cookiename=cookievalue")
- result = request.cookies
- assert len(result) == 1
- assert result['cookiename'] == ['cookievalue']
+ assert len(request.cookies) == 1
+ assert request.cookies['cookiename'] == 'cookievalue'
def test_get_cookies_double(self):
request = treq()
request.headers = Headers(cookie="cookiename=cookievalue;othercookiename=othercookievalue")
result = request.cookies
assert len(result) == 2
- assert result['cookiename'] == ['cookievalue']
- assert result['othercookiename'] == ['othercookievalue']
+ assert result['cookiename'] == 'cookievalue'
+ assert result['othercookiename'] == 'othercookievalue'
def test_get_cookies_withequalsign(self):
request = treq()
request.headers = Headers(cookie="cookiename=coo=kievalue;othercookiename=othercookievalue")
result = request.cookies
assert len(result) == 2
- assert result['cookiename'] == ['coo=kievalue']
- assert result['othercookiename'] == ['othercookievalue']
+ assert result['cookiename'] == 'coo=kievalue'
+ assert result['othercookiename'] == 'othercookievalue'
def test_set_cookies(self):
request = treq()
request.headers = Headers(cookie="cookiename=cookievalue")
result = request.cookies
- result["cookiename"] = ["foo"]
- request.cookies = result
- assert request.cookies["cookiename"] == ["foo"]
+ result["cookiename"] = "foo"
+ assert request.cookies["cookiename"] == "foo"
def test_get_path_components(self):
request = treq(path=b"/foo/bar")
- assert request.path_components == ["foo", "bar"]
+ assert request.path_components == ("foo", "bar")
def test_set_path_components(self):
- request = treq(host=b"foo", headers = Headers(host=b"bar"))
+ request = treq()
request.path_components = ["foo", "baz"]
assert request.path == "/foo/baz"
+
request.path_components = []
assert request.path == "/"
- request.query = ODict([])
- assert request.host == "foo"
- assert request.headers["host"] == "bar"
+
+ request.path_components = ["foo", "baz"]
+ request.query["hello"] = "hello"
+ assert request.path_components == ("foo", "baz")
+
+ request.path_components = ["abc"]
+ assert request.path == "/abc?hello=hello"
def test_anticache(self):
request = treq()
@@ -246,26 +250,21 @@ class TestRequestUtils(object):
assert "gzip" in request.headers["Accept-Encoding"]
def test_get_urlencoded_form(self):
- request = treq(content="foobar")
- assert request.urlencoded_form is None
+ request = treq(content="foobar=baz")
+ assert not request.urlencoded_form
request.headers["Content-Type"] = "application/x-www-form-urlencoded"
- assert request.urlencoded_form == ODict(utils.urldecode(request.content))
+ assert list(request.urlencoded_form.items()) == [("foobar", "baz")]
def test_set_urlencoded_form(self):
request = treq()
- request.urlencoded_form = ODict([('foo', 'bar'), ('rab', 'oof')])
+ request.urlencoded_form = [('foo', 'bar'), ('rab', 'oof')]
assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"
assert request.content
def test_get_multipart_form(self):
request = treq(content="foobar")
- assert request.multipart_form is None
+ assert not request.multipart_form
request.headers["Content-Type"] = "multipart/form-data"
- assert request.multipart_form == ODict(
- utils.multipartdecode(
- request.headers,
- request.content
- )
- )
+ assert list(request.multipart_form.items()) == []
diff --git a/test/netlib/http/test_response.py b/test/netlib/http/test_response.py
index 5440176c..cfd093d4 100644
--- a/test/netlib/http/test_response.py
+++ b/test/netlib/http/test_response.py
@@ -6,6 +6,7 @@ import six
import time
from netlib.http import Headers
+from netlib.http.cookies import CookieAttrs
from netlib.odict import ODict, ODictCaseless
from netlib.tutils import raises, tresp
from .test_message import _test_passthrough_attr, _test_decoded_attr
@@ -13,7 +14,7 @@ from .test_message import _test_passthrough_attr, _test_decoded_attr
class TestResponseData(object):
def test_init(self):
- with raises(ValueError if six.PY2 else TypeError):
+ with raises(ValueError):
tresp(headers="foobar")
assert isinstance(tresp(headers=None).headers, Headers)
@@ -56,7 +57,7 @@ class TestResponseUtils(object):
result = resp.cookies
assert len(result) == 1
assert "cookiename" in result
- assert result["cookiename"][0] == ["cookievalue", ODict()]
+ assert result["cookiename"] == ("cookievalue", CookieAttrs())
def test_get_cookies_with_parameters(self):
resp = tresp()
@@ -64,13 +65,13 @@ class TestResponseUtils(object):
result = resp.cookies
assert len(result) == 1
assert "cookiename" in result
- assert result["cookiename"][0][0] == "cookievalue"
- attrs = result["cookiename"][0][1]
+ assert result["cookiename"][0] == "cookievalue"
+ attrs = result["cookiename"][1]
assert len(attrs) == 4
- assert attrs["domain"] == ["example.com"]
- assert attrs["expires"] == ["Wed Oct 21 16:29:41 2015"]
- assert attrs["path"] == ["/"]
- assert attrs["httponly"] == [None]
+ assert attrs["domain"] == "example.com"
+ assert attrs["expires"] == "Wed Oct 21 16:29:41 2015"
+ assert attrs["path"] == "/"
+ assert attrs["httponly"] is None
def test_get_cookies_no_value(self):
resp = tresp()
@@ -78,8 +79,8 @@ class TestResponseUtils(object):
result = resp.cookies
assert len(result) == 1
assert "cookiename" in result
- assert result["cookiename"][0][0] == ""
- assert len(result["cookiename"][0][1]) == 2
+ assert result["cookiename"][0] == ""
+ assert len(result["cookiename"][1]) == 2
def test_get_cookies_twocookies(self):
resp = tresp()
@@ -90,19 +91,16 @@ class TestResponseUtils(object):
result = resp.cookies
assert len(result) == 2
assert "cookiename" in result
- assert result["cookiename"][0] == ["cookievalue", ODict()]
+ assert result["cookiename"] == ("cookievalue", CookieAttrs())
assert "othercookie" in result
- assert result["othercookie"][0] == ["othervalue", ODict()]
+ assert result["othercookie"] == ("othervalue", CookieAttrs())
def test_set_cookies(self):
resp = tresp()
- v = resp.cookies
- v.add("foo", ["bar", ODictCaseless()])
- resp.cookies = v
+ resp.cookies["foo"] = ("bar", {})
- v = resp.cookies
- assert len(v) == 1
- assert v["foo"] == [["bar", ODictCaseless()]]
+ assert len(resp.cookies) == 1
+ assert resp.cookies["foo"] == ("bar", CookieAttrs())
def test_refresh(self):
r = tresp()
diff --git a/test/netlib/test_multidict.py b/test/netlib/test_multidict.py
new file mode 100644
index 00000000..ceea3806
--- /dev/null
+++ b/test/netlib/test_multidict.py
@@ -0,0 +1,217 @@
+from netlib import tutils
+from netlib.multidict import MultiDict, ImmutableMultiDict
+
+
+class _TMulti(object):
+ @staticmethod
+ def _reduce_values(values):
+ return values[0]
+
+ @staticmethod
+ def _kconv(key):
+ return key.lower()
+
+
+class TMultiDict(_TMulti, MultiDict):
+ pass
+
+
+class TImmutableMultiDict(_TMulti, ImmutableMultiDict):
+ pass
+
+
+class TestMultiDict(object):
+ @staticmethod
+ def _multi():
+ return TMultiDict((
+ ("foo", "bar"),
+ ("bar", "baz"),
+ ("Bar", "bam")
+ ))
+
+ def test_init(self):
+ md = TMultiDict()
+ assert len(md) == 0
+
+ md = TMultiDict([("foo", "bar")])
+ assert len(md) == 1
+ assert md.fields == (("foo", "bar"),)
+
+ def test_repr(self):
+ assert repr(self._multi()) == (
+ "TMultiDict[('foo', 'bar'), ('bar', 'baz'), ('Bar', 'bam')]"
+ )
+
+ def test_getitem(self):
+ md = TMultiDict([("foo", "bar")])
+ assert "foo" in md
+ assert "Foo" in md
+ assert md["foo"] == "bar"
+
+ with tutils.raises(KeyError):
+ _ = md["bar"]
+
+ md_multi = TMultiDict(
+ [("foo", "a"), ("foo", "b")]
+ )
+ assert md_multi["foo"] == "a"
+
+ def test_setitem(self):
+ md = TMultiDict()
+ md["foo"] = "bar"
+ assert md.fields == (("foo", "bar"),)
+
+ md["foo"] = "baz"
+ assert md.fields == (("foo", "baz"),)
+
+ md["bar"] = "bam"
+ assert md.fields == (("foo", "baz"), ("bar", "bam"))
+
+ def test_delitem(self):
+ md = self._multi()
+ del md["foo"]
+ assert "foo" not in md
+ assert "bar" in md
+
+ with tutils.raises(KeyError):
+ del md["foo"]
+
+ del md["bar"]
+ assert md.fields == ()
+
+ def test_iter(self):
+ md = self._multi()
+ assert list(md.__iter__()) == ["foo", "bar"]
+
+ def test_len(self):
+ md = TMultiDict()
+ assert len(md) == 0
+
+ md = self._multi()
+ assert len(md) == 2
+
+ def test_eq(self):
+ assert TMultiDict() == TMultiDict()
+ assert not (TMultiDict() == 42)
+
+ md1 = self._multi()
+ md2 = self._multi()
+ assert md1 == md2
+ md1.fields = md1.fields[1:] + md1.fields[:1]
+ assert not (md1 == md2)
+
+ def test_ne(self):
+ assert not TMultiDict() != TMultiDict()
+ assert TMultiDict() != self._multi()
+ assert TMultiDict() != 42
+
+ def test_get_all(self):
+ md = self._multi()
+ assert md.get_all("foo") == ["bar"]
+ assert md.get_all("bar") == ["baz", "bam"]
+ assert md.get_all("baz") == []
+
+ def test_set_all(self):
+ md = TMultiDict()
+ md.set_all("foo", ["bar", "baz"])
+ assert md.fields == (("foo", "bar"), ("foo", "baz"))
+
+ md = TMultiDict((
+ ("a", "b"),
+ ("x", "x"),
+ ("c", "d"),
+ ("X", "x"),
+ ("e", "f"),
+ ))
+ md.set_all("x", ["1", "2", "3"])
+ assert md.fields == (
+ ("a", "b"),
+ ("x", "1"),
+ ("c", "d"),
+ ("x", "2"),
+ ("e", "f"),
+ ("x", "3"),
+ )
+ md.set_all("x", ["4"])
+ assert md.fields == (
+ ("a", "b"),
+ ("x", "4"),
+ ("c", "d"),
+ ("e", "f"),
+ )
+
+ def test_add(self):
+ md = self._multi()
+ md.add("foo", "foo")
+ assert md.fields == (
+ ("foo", "bar"),
+ ("bar", "baz"),
+ ("Bar", "bam"),
+ ("foo", "foo")
+ )
+
+ def test_insert(self):
+ md = TMultiDict([("b", "b")])
+ md.insert(0, "a", "a")
+ md.insert(2, "c", "c")
+ assert md.fields == (("a", "a"), ("b", "b"), ("c", "c"))
+
+ def test_keys(self):
+ md = self._multi()
+ assert list(md.keys()) == ["foo", "bar"]
+ assert list(md.keys(multi=True)) == ["foo", "bar", "Bar"]
+
+ def test_values(self):
+ md = self._multi()
+ assert list(md.values()) == ["bar", "baz"]
+ assert list(md.values(multi=True)) == ["bar", "baz", "bam"]
+
+ def test_items(self):
+ md = self._multi()
+ assert list(md.items()) == [("foo", "bar"), ("bar", "baz")]
+ assert list(md.items(multi=True)) == [("foo", "bar"), ("bar", "baz"), ("Bar", "bam")]
+
+ def test_to_dict(self):
+ md = self._multi()
+ assert md.to_dict() == {
+ "foo": "bar",
+ "bar": ["baz", "bam"]
+ }
+
+ def test_state(self):
+ md = self._multi()
+ assert len(md.get_state()) == 3
+ assert md == TMultiDict.from_state(md.get_state())
+
+ md2 = TMultiDict()
+ assert md != md2
+ md2.set_state(md.get_state())
+ assert md == md2
+
+
+class TestImmutableMultiDict(object):
+ def test_modify(self):
+ md = TImmutableMultiDict()
+ with tutils.raises(TypeError):
+ md["foo"] = "bar"
+
+ with tutils.raises(TypeError):
+ del md["foo"]
+
+ with tutils.raises(TypeError):
+ md.add("foo", "bar")
+
+ def test_with_delitem(self):
+ md = TImmutableMultiDict([("foo", "bar")])
+ assert md.with_delitem("foo").fields == ()
+ assert md.fields == (("foo", "bar"),)
+
+ def test_with_set_all(self):
+ md = TImmutableMultiDict()
+ assert md.with_set_all("foo", ["bar"]).fields == (("foo", "bar"),)
+ assert md.fields == ()
+
+ def test_with_insert(self):
+ md = TImmutableMultiDict()
+ assert md.with_insert(0, "foo", "bar").fields == (("foo", "bar"),)
+ assert md.fields == () \ No newline at end of file