def tflow(client_conn=True, server_conn=True, req=True, resp=None, err=None): """ @type client_conn: bool | None | mitmproxy.proxy.connection.ClientConnection @type server_conn: bool | None | mitmproxy.proxy.connection.ServerConnection @type req: bool | None | mitmproxy.protocol.http.HTTPRequest @type resp: bool | None | mitmproxy.protocol.http.HTTPResponse @type err: bool | None | mitmproxy.protocol.primitives.Error @return: mitmproxy.protocol.http.HTTPFlow """ if client_conn is True: client_conn = tclient_conn() if server_conn is True: server_conn = tserver_conn() if req is True: req = netlib.tutils.treq() if resp is True: resp = netlib.tutils.tresp() if err is True: err = terr() if req: req = HTTPRequest.wrap(req) if resp: resp = HTTPResponse.wrap(resp) f = HTTPFlow(client_conn, server_conn) f.request = req f.response = resp f.error = err f.reply = controller.DummyReply() return f
def test_anticache(self): r = HTTPRequest.wrap(netlib.tutils.treq()) r.headers = Headers() r.headers["if-modified-since"] = "test" r.headers["if-none-match"] = "test" r.anticache() assert not "if-modified-since" in r.headers assert not "if-none-match" in r.headers
def test_replace(self): r = HTTPRequest.wrap(netlib.tutils.treq()) r.path = "path/foo" r.headers["Foo"] = "fOo" r.content = b"afoob" assert r.replace("foo(?i)", "boo") == 4 assert r.path == "path/boo" assert b"foo" not in r.content assert r.headers["boo"] == "boo"
def test_app_registry(): ar = flow.AppRegistry() ar.add("foo", "domain", 80) r = HTTPRequest.wrap(netlib.tutils.treq()) r.host = "domain" r.port = 80 assert ar.get(r) r.port = 81 assert not ar.get(r) r = HTTPRequest.wrap(netlib.tutils.treq()) r.host = "domain2" r.port = 80 assert not ar.get(r) r.headers["host"] = "domain" assert ar.get(r)
def test_constrain_encoding(self): r = HTTPRequest.wrap(netlib.tutils.treq()) r.headers["accept-encoding"] = "gzip, oink" r.constrain_encoding() assert "oink" not in r.headers["accept-encoding"] r.headers.set_all("accept-encoding", ["gzip", "oink"]) r.constrain_encoding() assert "oink" not in r.headers["accept-encoding"]
def test_get_decoded_content(self): r = HTTPRequest.wrap(netlib.tutils.treq()) r.content = None r.headers["content-encoding"] = "identity" assert r.get_decoded_content() is None r.content = "falafel" r.encode("gzip") assert r.get_decoded_content() == "falafel"
def test_replace(self): r = HTTPRequest.wrap(netlib.tutils.treq()) r.path = "path/foo" r.headers["Foo"] = "fOo" r.content = "afoob" assert r.replace("foo(?i)", "boo") == 4 assert r.path == "path/boo" assert not "foo" in r.content assert r.headers["boo"] == "boo"
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_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_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 test_all(self): s = flow.State() fm = flow.FlowMaster(None, None, s) f = tutils.tflow(req=None) fm.clientconnect(f.client_conn) f.request = HTTPRequest.wrap(netlib.tutils.treq()) fm.request(f) assert s.flow_count() == 1 f.response = HTTPResponse.wrap(netlib.tutils.tresp()) fm.response(f) assert s.flow_count() == 1 fm.clientdisconnect(f.client_conn) f.error = Error("msg") fm.error(f) fm.shutdown()
def test_get_url(self): r = HTTPRequest.wrap(netlib.tutils.treq()) assert r.url == "http://address:22/path" r.scheme = "https" assert r.url == "https://address:22/path" r.host = "host" r.port = 42 assert r.url == "https://host:42/path" r.host = "address" r.port = 22 assert r.url == "https://address:22/path" assert r.pretty_url == "https://address:22/path" r.headers["Host"] = "foo.com:22" assert r.url == "https://address:22/path" assert r.pretty_url == "https://foo.com:22/path"
def test_all(self): s = flow.State() fm = flow.FlowMaster(None, None, s) f = tutils.tflow(req=None) fm.clientconnect(f.client_conn) f.request = HTTPRequest.wrap(netlib.tutils.treq()) fm.request(f) assert s.flow_count() == 1 f.response = HTTPResponse.wrap(netlib.tutils.tresp()) fm.response(f) assert s.flow_count() == 1 fm.clientdisconnect(f.client_conn) f.error = Error("msg") f.error.reply = controller.DummyReply() fm.error(f) fm.load_script(tutils.test_data.path("data/scripts/a.py")) fm.shutdown()
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_all(self): s = flow.State() fm = flow.FlowMaster(None, s) fm.anticache = True fm.anticomp = True f = tutils.tflow(req=None) fm.clientconnect(f.client_conn) f.request = HTTPRequest.wrap(netlib.tutils.treq()) fm.request(f) assert s.flow_count() == 1 f.response = HTTPResponse.wrap(netlib.tutils.tresp()) fm.response(f) assert s.flow_count() == 1 fm.clientdisconnect(f.client_conn) f.error = Error("msg") f.error.reply = controller.DummyReply() fm.error(f) fm.load_script(tutils.test_data.path("data/scripts/a.py")) fm.shutdown()