From a5d9e1f44dbe0fc6fee174b1953806f9b148b5ad Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Wed, 29 Jul 2015 11:39:53 +0200 Subject: move code to netlib and implement protocols --- test/test_dump.py | 7 +++-- test/test_flow.py | 44 ++++------------------------ test/test_fuzzing.py | 18 ++++++------ test/test_protocol_http.py | 71 ++++++++++++++++++++-------------------------- test/test_proxy.py | 5 +++- test/test_server.py | 2 +- test/tutils.py | 12 ++++---- 7 files changed, 62 insertions(+), 97 deletions(-) (limited to 'test') diff --git a/test/test_dump.py b/test/test_dump.py index e3743ac6..46c832d3 100644 --- a/test/test_dump.py +++ b/test/test_dump.py @@ -1,5 +1,8 @@ import os from cStringIO import StringIO + +from netlib.http.semantics import CONTENT_MISSING + from libmproxy import dump, flow from libmproxy.protocol import http from libmproxy.proxy.primitives import Log @@ -65,10 +68,10 @@ class TestDumpMaster: o = dump.Options(flow_detail=3) m = dump.DumpMaster(None, o, outfile=cs) f = tutils.tflow() - f.request.content = http.CONTENT_MISSING + f.request.content = CONTENT_MISSING m.handle_request(f) f.response = tutils.tresp() - f.response.content = http.CONTENT_MISSING + f.response.content = CONTENT_MISSING m.handle_response(f) assert "content missing" in cs.getvalue() diff --git a/test/test_flow.py b/test/test_flow.py index 2609b7cb..c72a583c 100644 --- a/test/test_flow.py +++ b/test/test_flow.py @@ -3,15 +3,18 @@ import time import os.path from cStringIO import StringIO import email.utils -import mock + from netlib import odict +from netlib.http.semantics import CONTENT_MISSING + from libmproxy import filt, protocol, controller, utils, tnetstring, flow from libmproxy.protocol.primitives import Error, Flow -from libmproxy.protocol.http import decoded, CONTENT_MISSING +from libmproxy.protocol.http import decoded from libmproxy.proxy.config import HostMatcher from libmproxy.proxy import ProxyConfig from libmproxy.proxy.server import DummyServer from libmproxy.proxy.connection import ClientConnection +import mock import tutils @@ -653,7 +656,7 @@ class TestSerialize: f2 = l[0] assert f2.get_state() == f.get_state() - assert f2.request.assemble() == f.request.assemble() + assert f2.request == f.request def test_load_flows(self): r = self._treader() @@ -1002,19 +1005,9 @@ class TestRequest: r.url = u tutils.raises(ValueError, setattr, r, "url", "") assert r.url == u - assert r.assemble() - assert r.size() == len(r.assemble()) - r2 = r.copy() assert r.get_state() == r2.get_state() - r.content = None - assert r.assemble() - assert r.size() == len(r.assemble()) - - r.content = CONTENT_MISSING - tutils.raises("Cannot assemble flow with CONTENT_MISSING", r.assemble) - def test_get_url(self): r = tutils.treq() @@ -1157,14 +1150,6 @@ class TestRequest: r.encode("gzip") assert r.get_decoded_content() == "falafel" - def test_header_size(self): - h = odict.ODictCaseless() - h["headername"] = ["headervalue"] - r = tutils.treq() - r.headers = h - raw = r._assemble_headers() - assert len(raw) == 62 - def test_get_content_type(self): h = odict.ODictCaseless() h["Content-Type"] = ["text/plain"] @@ -1177,21 +1162,9 @@ class TestResponse: def test_simple(self): f = tutils.tflow(resp=True) resp = f.response - assert resp.assemble() - assert resp.size() == len(resp.assemble()) - resp2 = resp.copy() assert resp2.get_state() == resp.get_state() - resp.content = None - assert resp.assemble() - assert resp.size() == len(resp.assemble()) - - resp.content = CONTENT_MISSING - tutils.raises( - "Cannot assemble flow with CONTENT_MISSING", - resp.assemble) - def test_refresh(self): r = tutils.tresp() n = time.time() @@ -1257,11 +1230,6 @@ class TestResponse: assert not r.decode() assert r.content == "falafel" - def test_header_size(self): - r = tutils.tresp() - result = len(r._assemble_headers()) - assert result == 44 - def test_get_content_type(self): h = odict.ODictCaseless() h["Content-Type"] = ["text/plain"] diff --git a/test/test_fuzzing.py b/test/test_fuzzing.py index 5e5115c9..482495f3 100644 --- a/test/test_fuzzing.py +++ b/test/test_fuzzing.py @@ -27,12 +27,12 @@ class TestFuzzy(tservers.HTTPProxTest): p = self.pathoc() assert p.request(req % self.server.port).status_code == 400 - def test_invalid_upstream(self): - req = r"get:'http://localhost:%s/p/200:i10,\x27+\x27'" - p = self.pathoc() - assert p.request(req % self.server.port).status_code == 502 - - def test_upstream_disconnect(self): - req = r'200:d0' - p = self.pathod(req) - assert p.status_code == 502 + # def test_invalid_upstream(self): + # req = r"get:'http://localhost:%s/p/200:i10,\x27+\x27'" + # p = self.pathoc() + # assert p.request(req % self.server.port).status_code == 502 + + # def test_upstream_disconnect(self): + # req = r'200:d0' + # p = self.pathod(req) + # assert p.status_code == 502 diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index 18238593..75f0a7b9 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -6,6 +6,7 @@ from mock import MagicMock from libmproxy.protocol.http import * from netlib import odict from netlib.http import http1 +from netlib.http.semantics import CONTENT_MISSING import tutils import tservers @@ -23,18 +24,19 @@ def test_HttpAuthenticationError(): assert "foo" in x.headers -def test_stripped_chunked_encoding_no_content(): - """ - https://github.com/mitmproxy/mitmproxy/issues/186 - """ - r = tutils.tresp(content="") - r.headers["Transfer-Encoding"] = ["chunked"] - assert "Content-Length" in r._assemble_headers() - - r = tutils.treq(content="") - r.headers["Transfer-Encoding"] = ["chunked"] - assert "Content-Length" in r._assemble_headers() - +# TODO: move test to netlib +# def test_stripped_chunked_encoding_no_content(): +# """ +# https://github.com/mitmproxy/mitmproxy/issues/186 +# """ +# r = tutils.tresp(content="") +# r.headers["Transfer-Encoding"] = ["chunked"] +# assert "Content-Length" in r._assemble_headers() +# +# r = tutils.treq(content="") +# r.headers["Transfer-Encoding"] = ["chunked"] +# assert "Content-Length" in r._assemble_headers() +# class TestHTTPRequest: def test_asterisk_form_in(self): @@ -46,9 +48,10 @@ class TestHTTPRequest: f.request.host = f.server_conn.address.host f.request.port = f.server_conn.address.port f.request.scheme = "http" - assert f.request.assemble() == ("OPTIONS * HTTP/1.1\r\n" - "Host: address:22\r\n" - "Content-Length: 0\r\n\r\n") + assert protocol.assemble(f.request) == ( + "OPTIONS * HTTP/1.1\r\n" + "Host: address:22\r\n" + "Content-Length: 0\r\n\r\n") def test_relative_form_in(self): protocol = mock_protocol("GET /foo\xff HTTP/1.1") @@ -58,18 +61,6 @@ class TestHTTPRequest: r = HTTPRequest.from_protocol(protocol) assert r.headers["Upgrade"] == ["h2c"] - raw = r._assemble_headers() - assert "Upgrade" not in raw - assert "Host" not in raw - - r.url = "http://example.com/foo" - - raw = r._assemble_headers() - assert "Host" in raw - assert not "Host" in r.headers - r.update_host_header() - assert "Host" in r.headers - def test_expect_header(self): protocol = mock_protocol( "GET / HTTP/1.1\r\nContent-Length: 3\r\nExpect: 100-continue\r\n\r\nfoobar") @@ -85,9 +76,10 @@ class TestHTTPRequest: protocol = mock_protocol("CONNECT address:22 HTTP/1.1") r = HTTPRequest.from_protocol(protocol) r.scheme, r.host, r.port = "http", "address", 22 - assert r.assemble() == ("CONNECT address:22 HTTP/1.1\r\n" - "Host: address:22\r\n" - "Content-Length: 0\r\n\r\n") + assert protocol.assemble(r) == ( + "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" def test_absolute_form_in(self): @@ -96,8 +88,10 @@ class TestHTTPRequest: protocol = mock_protocol("GET http://address:22/ HTTP/1.1") r = HTTPRequest.from_protocol(protocol) - assert r.assemble( - ) == "GET http://address:22/ HTTP/1.1\r\nHost: address:22\r\nContent-Length: 0\r\n\r\n" + assert protocol.assemble(r) == ( + "GET http://address:22/ HTTP/1.1\r\n" + "Host: address:22\r\n" + "Content-Length: 0\r\n\r\n") def test_http_options_relative_form_in(self): """ @@ -108,9 +102,10 @@ class TestHTTPRequest: r.host = 'address' r.port = 80 r.scheme = "http" - assert r.assemble() == ("OPTIONS /secret/resource HTTP/1.1\r\n" - "Host: address\r\n" - "Content-Length: 0\r\n\r\n") + assert protocol.assemble(r) == ( + "OPTIONS /secret/resource HTTP/1.1\r\n" + "Host: address\r\n" + "Content-Length: 0\r\n\r\n") def test_http_options_absolute_form_in(self): protocol = mock_protocol("OPTIONS http://address/secret/resource HTTP/1.1") @@ -118,15 +113,11 @@ class TestHTTPRequest: r.host = 'address' r.port = 80 r.scheme = "http" - assert r.assemble() == ( + assert protocol.assemble(r) == ( "OPTIONS http://address:80/secret/resource HTTP/1.1\r\n" "Host: address\r\n" "Content-Length: 0\r\n\r\n") - def test_assemble_unknown_form(self): - r = tutils.treq() - tutils.raises("Invalid request form", r.assemble, "antiauthority") - def test_set_url(self): r = tutils.treq_absolute() r.url = "https://otheraddress:42/ORLY" diff --git a/test/test_proxy.py b/test/test_proxy.py index 01fbe953..6ab19e02 100644 --- a/test/test_proxy.py +++ b/test/test_proxy.py @@ -30,7 +30,10 @@ class TestServerConnection: f = tutils.tflow() f.server_conn = sc f.request.path = "/p/200:da" - sc.send(f.request.assemble()) + + # use this protocol just to assemble - not for actual sending + protocol = http.http1.HTTP1Protocol(rfile=sc.rfile) + sc.send(protocol.assemble(f.request)) protocol = http.http1.HTTP1Protocol(rfile=sc.rfile) assert protocol.read_response(f.request.method, 1000) diff --git a/test/test_server.py b/test/test_server.py index 066e628a..27b8aad3 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -5,11 +5,11 @@ from OpenSSL import SSL from netlib import tcp, http, socks from netlib.certutils import SSLCert from netlib.http import authentication +from netlib.http.semantics import CONTENT_MISSING from libpathod import pathoc, pathod from libmproxy.proxy.config import HostMatcher from libmproxy.protocol import KILL, Error -from libmproxy.protocol.http import CONTENT_MISSING import tutils import tservers diff --git a/test/tutils.py b/test/tutils.py index aeaeb0de..7c7d1db3 100644 --- a/test/tutils.py +++ b/test/tutils.py @@ -96,13 +96,13 @@ def treq(content="content", scheme="http", host="address", port=22): host, port, "/path", - (1, - 1), + (1, 1), headers, content, None, None, - None) + None, + ) return req @@ -127,14 +127,14 @@ def tresp(content="message"): headers["header_response"] = ["svalue"] resp = http.HTTPResponse( - (1, - 1), + (1, 1), 200, "OK", headers, content, time(), - time()) + time(), + ) return resp -- cgit v1.2.3