aboutsummaryrefslogtreecommitdiffstats
path: root/test/netlib/http/test_request.py
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2016-05-18 18:46:42 -0700
committerMaximilian Hils <git@maximilianhils.com>2016-05-18 18:46:42 -0700
commit44ac64aa7235362acbb96e0f12aa27534580e575 (patch)
treec03b8c3519c273a4f42b60cb2bce8cc0dd524925 /test/netlib/http/test_request.py
parent4c3fb8f5097fad2c5de96104dae3f8026b0b4666 (diff)
downloadmitmproxy-44ac64aa7235362acbb96e0f12aa27534580e575.tar.gz
mitmproxy-44ac64aa7235362acbb96e0f12aa27534580e575.tar.bz2
mitmproxy-44ac64aa7235362acbb96e0f12aa27534580e575.zip
add MultiDict
This commit introduces MultiDict, a multi-dictionary similar to ODict, but with improved semantics (as in the Headers class). MultiDict fixes a few issues that were present in the Request/Response API. In particular, `request.cookies["foo"] = "bar"` has previously been a no-op, as the cookies property returned a mutable _copy_ of the cookies.
Diffstat (limited to 'test/netlib/http/test_request.py')
-rw-r--r--test/netlib/http/test_request.py61
1 files changed, 30 insertions, 31 deletions
diff --git a/test/netlib/http/test_request.py b/test/netlib/http/test_request.py
index 7ed6bd0f..26593ee1 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,15 +250,15 @@ class TestRequestUtils(object):
assert "gzip" in request.headers["Accept-Encoding"]
def test_get_urlencoded_form(self):
- request = treq(content="foobar")
+ request = treq(content="foobar=baz")
assert request.urlencoded_form is None
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
@@ -263,9 +267,4 @@ class TestRequestUtils(object):
assert request.multipart_form is None
request.headers["Content-Type"] = "multipart/form-data"
- assert request.multipart_form == ODict(
- utils.multipartdecode(
- request.headers,
- request.content
- )
- )
+ assert list(request.multipart_form.items()) == []