diff options
author | Maximilian Hils <git@maximilianhils.com> | 2015-09-26 00:39:04 +0200 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2015-09-26 00:39:04 +0200 |
commit | 106f7046d3862cb0e3cbb4f38335af0330b4e7e3 (patch) | |
tree | 6a57a7c29062cad851d0b162a17e460b32ea3262 /test | |
parent | 45f2ea33b2fdb67ca89e7eedd860ebe683770497 (diff) | |
download | mitmproxy-106f7046d3862cb0e3cbb4f38335af0330b4e7e3.tar.gz mitmproxy-106f7046d3862cb0e3cbb4f38335af0330b4e7e3.tar.bz2 mitmproxy-106f7046d3862cb0e3cbb4f38335af0330b4e7e3.zip |
refactor request model
Diffstat (limited to 'test')
-rw-r--r-- | test/http/http1/test_assemble.py | 12 | ||||
-rw-r--r-- | test/http/http1/test_read.py | 8 | ||||
-rw-r--r-- | test/http/test_models.py | 75 | ||||
-rw-r--r-- | test/http/test_request.py | 3 | ||||
-rw-r--r-- | test/http/test_response.py | 3 | ||||
-rw-r--r-- | test/test_utils.py | 8 | ||||
-rw-r--r-- | test/websockets/test_websockets.py | 2 |
7 files changed, 41 insertions, 70 deletions
diff --git a/test/http/http1/test_assemble.py b/test/http/http1/test_assemble.py index 963e7549..47d11d33 100644 --- a/test/http/http1/test_assemble.py +++ b/test/http/http1/test_assemble.py @@ -20,7 +20,7 @@ def test_assemble_request(): ) with raises(HttpException): - assemble_request(treq(body=CONTENT_MISSING)) + assemble_request(treq(content=CONTENT_MISSING)) def test_assemble_request_head(): @@ -62,21 +62,21 @@ def test_assemble_body(): def test_assemble_request_line(): - assert _assemble_request_line(treq()) == b"GET /path HTTP/1.1" + assert _assemble_request_line(treq().data) == b"GET /path HTTP/1.1" - authority_request = treq(method=b"CONNECT", form_in="authority") + authority_request = treq(method=b"CONNECT", first_line_format="authority").data assert _assemble_request_line(authority_request) == b"CONNECT address:22 HTTP/1.1" - absolute_request = treq(form_in="absolute") + absolute_request = treq(first_line_format="absolute").data assert _assemble_request_line(absolute_request) == b"GET http://address:22/path HTTP/1.1" with raises(RuntimeError): - _assemble_request_line(treq(), "invalid_form") + _assemble_request_line(treq(first_line_format="invalid_form").data) def test_assemble_request_headers(): # https://github.com/mitmproxy/mitmproxy/issues/186 - r = treq(body=b"") + r = treq(content=b"") r.headers["Transfer-Encoding"] = "chunked" c = _assemble_request_headers(r) assert b"Transfer-Encoding" in c diff --git a/test/http/http1/test_read.py b/test/http/http1/test_read.py index 9eb02a24..c3f744bf 100644 --- a/test/http/http1/test_read.py +++ b/test/http/http1/test_read.py @@ -16,8 +16,8 @@ from netlib.tutils import treq, tresp, raises def test_read_request(): rfile = BytesIO(b"GET / HTTP/1.1\r\n\r\nskip") r = read_request(rfile) - assert r.method == b"GET" - assert r.body == b"" + assert r.method == "GET" + assert r.content == b"" assert r.timestamp_end assert rfile.read() == b"skip" @@ -32,7 +32,7 @@ def test_read_request_head(): rfile.reset_timestamps = Mock() rfile.first_byte_timestamp = 42 r = read_request_head(rfile) - assert r.method == b"GET" + assert r.method == "GET" assert r.headers["Content-Length"] == "4" assert r.body is None assert rfile.reset_timestamps.called @@ -283,7 +283,7 @@ class TestReadHeaders(object): def test_read_chunked(): - req = treq(body=None) + req = treq(content=None) req.headers["Transfer-Encoding"] = "chunked" data = b"1\r\na\r\n0\r\n" diff --git a/test/http/test_models.py b/test/http/test_models.py index 10e0795a..3c196847 100644 --- a/test/http/test_models.py +++ b/test/http/test_models.py @@ -39,6 +39,7 @@ class TestRequest(object): a = tutils.treq(timestamp_start=42, timestamp_end=43) b = tutils.treq(timestamp_start=42, timestamp_end=43) assert a == b + assert not a != b assert not a == 'foo' assert not b == 'foo' @@ -70,45 +71,17 @@ class TestRequest(object): req = tutils.treq() req.headers["Host"] = "" req.host = "foobar" - req.update_host_header() assert req.headers["Host"] == "foobar" - def test_get_form(self): - req = tutils.treq() - assert req.get_form() == ODict() - - @mock.patch("netlib.http.Request.get_form_multipart") - @mock.patch("netlib.http.Request.get_form_urlencoded") - def test_get_form_with_url_encoded(self, mock_method_urlencoded, mock_method_multipart): - req = tutils.treq() - assert req.get_form() == ODict() - - req = tutils.treq() - req.body = "foobar" - req.headers["Content-Type"] = HDR_FORM_URLENCODED - req.get_form() - assert req.get_form_urlencoded.called - assert not req.get_form_multipart.called - - @mock.patch("netlib.http.Request.get_form_multipart") - @mock.patch("netlib.http.Request.get_form_urlencoded") - def test_get_form_with_multipart(self, mock_method_urlencoded, mock_method_multipart): - req = tutils.treq() - req.body = "foobar" - req.headers["Content-Type"] = HDR_FORM_MULTIPART - req.get_form() - assert not req.get_form_urlencoded.called - assert req.get_form_multipart.called - def test_get_form_urlencoded(self): - req = tutils.treq(body="foobar") + req = tutils.treq(content="foobar") assert req.get_form_urlencoded() == ODict() req.headers["Content-Type"] = HDR_FORM_URLENCODED assert req.get_form_urlencoded() == ODict(utils.urldecode(req.body)) def test_get_form_multipart(self): - req = tutils.treq(body="foobar") + req = tutils.treq(content="foobar") assert req.get_form_multipart() == ODict() req.headers["Content-Type"] = HDR_FORM_MULTIPART @@ -140,7 +113,7 @@ class TestRequest(object): assert req.get_query().lst == [] req.url = "http://localhost:80/foo?bar=42" - assert req.get_query().lst == [(b"bar", b"42")] + assert req.get_query().lst == [("bar", "42")] def test_set_query(self): req = tutils.treq() @@ -148,31 +121,23 @@ class TestRequest(object): def test_pretty_host(self): r = tutils.treq() - assert r.pretty_host(True) == "address" - assert r.pretty_host(False) == "address" + assert r.pretty_host == "address" + assert r.host == "address" r.headers["host"] = "other" - assert r.pretty_host(True) == "other" - assert r.pretty_host(False) == "address" + assert r.pretty_host == "other" + assert r.host == "address" r.host = None - assert r.pretty_host(True) == "other" - assert r.pretty_host(False) is None - del r.headers["host"] - assert r.pretty_host(True) is None - assert r.pretty_host(False) is None + assert r.pretty_host is None + assert r.host is None # Invalid IDNA r.headers["host"] = ".disqus.com" - assert r.pretty_host(True) == ".disqus.com" + assert r.pretty_host == ".disqus.com" def test_pretty_url(self): - req = tutils.treq() - req.form_out = "authority" - assert req.pretty_url(True) == b"address:22" - assert req.pretty_url(False) == b"address:22" - - req.form_out = "relative" - assert req.pretty_url(True) == b"http://address:22/path" - assert req.pretty_url(False) == b"http://address:22/path" + req = tutils.treq(first_line_format="relative") + assert req.pretty_url == "http://address:22/path" + assert req.url == "http://address:22/path" def test_get_cookies_none(self): headers = Headers() @@ -212,12 +177,12 @@ class TestRequest(object): assert r.get_cookies()["cookiename"] == ["foo"] def test_set_url(self): - r = tutils.treq(form_in="absolute") + r = tutils.treq(first_line_format="absolute") r.url = b"https://otheraddress:42/ORLY" - assert r.scheme == b"https" - assert r.host == b"otheraddress" + assert r.scheme == "https" + assert r.host == "otheraddress" assert r.port == 42 - assert r.path == b"/ORLY" + assert r.path == "/ORLY" try: r.url = "//localhost:80/foo@bar" @@ -230,7 +195,7 @@ class TestRequest(object): # protocol = mock_protocol("OPTIONS * HTTP/1.1") # f.request = HTTPRequest.from_protocol(protocol) # - # assert f.request.form_in == "relative" + # assert f.request.first_line_format == "relative" # f.request.host = f.server_conn.address.host # f.request.port = f.server_conn.address.port # f.request.scheme = "http" @@ -266,7 +231,7 @@ class TestRequest(object): # "CONNECT address:22 HTTP/1.1\r\n" # "Host: address:22\r\n" # "Content-Length: 0\r\n\r\n") - # assert r.pretty_url(False) == "address:22" + # assert r.pretty_url == "address:22" # # def test_absolute_form_in(self): # protocol = mock_protocol("GET oops-no-protocol.com HTTP/1.1") diff --git a/test/http/test_request.py b/test/http/test_request.py new file mode 100644 index 00000000..02fac3df --- /dev/null +++ b/test/http/test_request.py @@ -0,0 +1,3 @@ +from __future__ import absolute_import, print_function, division + +# TODO
\ No newline at end of file diff --git a/test/http/test_response.py b/test/http/test_response.py new file mode 100644 index 00000000..02fac3df --- /dev/null +++ b/test/http/test_response.py @@ -0,0 +1,3 @@ +from __future__ import absolute_import, print_function, division + +# TODO
\ No newline at end of file diff --git a/test/test_utils.py b/test/test_utils.py index 17636cc4..b096e5bc 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -84,10 +84,10 @@ def test_parse_url(): def test_unparse_url(): - assert utils.unparse_url(b"http", b"foo.com", 99, b"") == b"http://foo.com:99" - assert utils.unparse_url(b"http", b"foo.com", 80, b"/bar") == b"http://foo.com/bar" - assert utils.unparse_url(b"https", b"foo.com", 80, b"") == b"https://foo.com:80" - assert utils.unparse_url(b"https", b"foo.com", 443, b"") == b"https://foo.com" + assert utils.unparse_url("http", "foo.com", 99, "") == "http://foo.com:99" + assert utils.unparse_url("http", "foo.com", 80, "/bar") == "http://foo.com/bar" + assert utils.unparse_url("https", "foo.com", 80, "") == "https://foo.com:80" + assert utils.unparse_url("https", "foo.com", 443, "") == "https://foo.com" def test_urlencode(): diff --git a/test/websockets/test_websockets.py b/test/websockets/test_websockets.py index 4ae4cf45..9a1e5d3d 100644 --- a/test/websockets/test_websockets.py +++ b/test/websockets/test_websockets.py @@ -68,7 +68,7 @@ class WebSocketsClient(tcp.TCPClient): self.wfile.write(bytes(headers) + b"\r\n") self.wfile.flush() - resp = read_response(self.rfile, treq(method="GET")) + resp = read_response(self.rfile, treq(method=b"GET")) server_nonce = self.protocol.check_server_handshake(resp.headers) if not server_nonce == self.protocol.create_server_nonce(self.client_nonce): |