Exemple #1
0
    def test_decodeencode(self):
        r = tutils.tresp()
        r.headers["content-encoding"] = ["identity"]
        r.content = "falafel"
        assert r.decode()
        assert not r.headers["content-encoding"]
        assert r.content == "falafel"

        r = tutils.tresp()
        r.headers["content-encoding"] = ["identity"]
        r.content = "falafel"
        r.encode("identity")
        assert r.headers["content-encoding"] == ["identity"]
        assert r.content == "falafel"

        r = tutils.tresp()
        r.headers["content-encoding"] = ["identity"]
        r.content = "falafel"
        r.encode("gzip")
        assert r.headers["content-encoding"] == ["gzip"]
        assert r.content != "falafel"
        assert r.decode()
        assert not r.headers["content-encoding"]
        assert r.content == "falafel"

        r.headers["content-encoding"] = ["gzip"]
        assert not r.decode()
        assert r.content == "falafel"
    def test_flow(self):
        """
            normal flow:

                connect -> request -> response
        """
        bc = tutils.tclient_conn()
        c = flow.State()

        req = tutils.treq(bc)
        f = c.add_request(req)
        assert f
        assert c.flow_count() == 1
        assert c.active_flow_count() == 1

        newreq = tutils.treq()
        assert c.add_request(newreq)
        assert c.active_flow_count() == 2

        resp = tutils.tresp(req)
        assert c.add_response(resp)
        assert c.flow_count() == 2
        assert c.active_flow_count() == 1

        unseen_resp = tutils.tresp()
        unseen_resp.flow = None
        assert not c.add_response(unseen_resp)
        assert c.active_flow_count() == 1

        resp = tutils.tresp(newreq)
        assert c.add_response(resp)
        assert c.active_flow_count() == 0
    def test_all(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        fm.anticache = True
        fm.anticomp = True
        req = tutils.treq()
        fm.handle_clientconnect(req.flow.client_conn)

        f = fm.handle_request(req)
        assert s.flow_count() == 1

        resp = tutils.tresp(req)
        fm.handle_response(resp)
        assert s.flow_count() == 1

        rx = tutils.tresp()
        rx.flow = None
        assert not fm.handle_response(rx)

        fm.handle_clientdisconnect(req.flow.client_conn)

        f.error = Error("msg")
        f.error.reply = controller.DummyReply()
        fm.handle_error(f.error)

        fm.load_script(tutils.test_data.path("scripts/a.py"))
        fm.shutdown()
Exemple #4
0
    def test_all(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        fm.anticache = True
        fm.anticomp = True
        req = tutils.treq()
        fm.handle_clientconnect(req.client_conn)

        f = fm.handle_request(req)
        assert s.flow_count() == 1

        resp = tutils.tresp(req)
        fm.handle_response(resp)
        assert s.flow_count() == 1

        rx = tutils.tresp()
        assert not fm.handle_response(rx)

        dc = flow.ClientDisconnect(req.client_conn)
        req.client_conn.requestcount = 1
        fm.handle_clientdisconnect(dc)

        err = flow.Error(f.request, "msg")
        fm.handle_error(err)

        fm.load_script(tutils.test_data.path("scripts/a.py"))
        fm.shutdown()
Exemple #5
0
    def test_flow(self):
        """
            normal flow:

                connect -> request -> response
        """
        c = flow.State()
        f = tutils.tflow()
        c.add_request(f)
        assert f
        assert c.flow_count() == 1
        assert c.active_flow_count() == 1

        newf = tutils.tflow()
        assert c.add_request(newf)
        assert c.active_flow_count() == 2

        f.response = tutils.tresp()
        assert c.add_response(f)
        assert c.flow_count() == 2
        assert c.active_flow_count() == 1

        _ = tutils.tresp()
        assert not c.add_response(None)
        assert c.active_flow_count() == 1

        newf.response = tutils.tresp()
        assert c.add_response(newf)
        assert c.active_flow_count() == 0
Exemple #6
0
    def test_flow(self):
        """
            normal flow:

                connect -> request -> response
        """
        bc = flow.ClientConnect(("address", 22))
        c = flow.State()

        req = tutils.treq(bc)
        f = c.add_request(req)
        assert f
        assert c.flow_count() == 1
        assert c._flow_map.get(req)
        assert c.active_flow_count() == 1

        newreq = tutils.treq()
        assert c.add_request(newreq)
        assert c._flow_map.get(newreq)
        assert c.active_flow_count() == 2

        resp = tutils.tresp(req)
        assert c.add_response(resp)
        assert c.flow_count() == 2
        assert c._flow_map.get(resp.request)
        assert c.active_flow_count() == 1

        unseen_resp = tutils.tresp()
        assert not c.add_response(unseen_resp)
        assert not c._flow_map.get(unseen_resp.request)
        assert c.active_flow_count() == 1

        resp = tutils.tresp(newreq)
        assert c.add_response(resp)
        assert c.active_flow_count() == 0
Exemple #7
0
    def test_set_limit(self):
        c = flow.State()

        req = tutils.treq()
        c.clientconnect(req.client_conn)
        assert len(c.view) == 0

        f = c.add_request(req)
        assert len(c.view) == 1

        c.set_limit("~s")
        assert len(c.view) == 0
        resp = tutils.tresp(req)
        c.add_response(resp)
        assert len(c.view) == 1
        c.set_limit(None)
        assert len(c.view) == 1

        req = tutils.treq()
        c.clientconnect(req.client_conn)
        c.add_request(req)
        assert len(c.view) == 2
        c.set_limit("~q")
        assert len(c.view) == 1
        c.set_limit("~s")
        assert len(c.view) == 1

        assert "Invalid" in c.set_limit("~")
    def test_script(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
        req = tutils.treq()
        fm.handle_clientconnect(req.flow.client_conn)
        assert fm.scripts[0].ns["log"][-1] == "clientconnect"
        sc = ServerConnection((req.get_host(), req.get_port()), None)
        sc.reply = controller.DummyReply()
        fm.handle_serverconnection(sc)
        assert fm.scripts[0].ns["log"][-1] == "serverconnect"
        f = fm.handle_request(req)
        assert fm.scripts[0].ns["log"][-1] == "request"
        resp = tutils.tresp(req)
        fm.handle_response(resp)
        assert fm.scripts[0].ns["log"][-1] == "response"
        #load second script
        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
        assert len(fm.scripts) == 2
        fm.handle_clientdisconnect(sc)
        assert fm.scripts[0].ns["log"][-1] == "clientdisconnect"
        assert fm.scripts[1].ns["log"][-1] == "clientdisconnect"


        #unload first script
        fm.unload_scripts()
        assert len(fm.scripts) == 0

        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
        err = tutils.terr()
        err.reply = controller.DummyReply()
        fm.handle_error(err)
        assert fm.scripts[0].ns["log"][-1] == "error"
Exemple #9
0
 def test_script(self):
     s = flow.State()
     fm = flow.FlowMaster(None, s)
     assert not fm.load_script([tutils.test_data.path("scripts/all.py")])
     req = tutils.treq()
     fm.handle_clientconnect(req.client_conn)
     assert fm.scripts[0].ns["log"][-1] == "clientconnect"
     f = fm.handle_request(req)
     assert fm.scripts[0].ns["log"][-1] == "request"
     resp = tutils.tresp(req)
     fm.handle_response(resp)
     assert fm.scripts[0].ns["log"][-1] == "response"
     #load second script
     assert not fm.load_script([tutils.test_data.path("scripts/all.py")])
     assert len(fm.scripts) == 2
     dc = flow.ClientDisconnect(req.client_conn)
     dc.reply = controller.DummyReply()
     fm.handle_clientdisconnect(dc)
     assert fm.scripts[0].ns["log"][-1] == "clientdisconnect"
     assert fm.scripts[1].ns["log"][-1] == "clientdisconnect"
     #unload first script
     fm.unload_script(fm.scripts[0])
     assert len(fm.scripts) == 1
     err = flow.Error(f.request, "msg")
     err.reply = controller.DummyReply()
     fm.handle_error(err)
     assert fm.scripts[0].ns["log"][-1] == "error"
Exemple #10
0
 def _flowfile(self, path):
     f = open(path, "wb")
     fw = flow.FlowWriter(f)
     t = tutils.tflow_full()
     t.response = tutils.tresp(t.request)
     fw.add(t)
     f.close()
Exemple #11
0
    def test_simple(self):
        f = tutils.tflow()
        foc = ('focus', '>>')
        assert foc not in console.format_flow(f, False)
        assert foc in console.format_flow(f, True)

        assert foc not in console.format_flow(f, False, True)
        assert foc in console.format_flow(f, True, True)

        f.response = tutils.tresp()
        f.request = f.response.request
        f.backup()

        f.request._set_replay()
        f.response._set_replay()
        assert ('method', '[replay]') in console.format_flow(f, True)
        assert ('method', '[replay]') in console.format_flow(f, True, True)

        f.response.code = 404
        assert ('error', '404') in console.format_flow(f, True, True)
        f.response.headers["content-type"] = ["text/html"]
        assert ('text', ' text/html') in console.format_flow(f, True, True)

        f.response =None
        f.error = flow.Error(f.request, "error")
        assert ('error', 'error') in console.format_flow(f, True, True)
Exemple #12
0
    def test_set_limit(self):
        c = flow.State()

        req = tutils.treq()
        assert len(c.view) == 0

        c.add_request(req)
        assert len(c.view) == 1

        c.set_limit("~s")
        assert c.limit_txt == "~s"
        assert len(c.view) == 0
        resp = tutils.tresp(req)
        c.add_response(resp)
        assert len(c.view) == 1
        c.set_limit(None)
        assert len(c.view) == 1

        req = tutils.treq()
        c.add_request(req)
        assert len(c.view) == 2
        c.set_limit("~q")
        assert len(c.view) == 1
        c.set_limit("~s")
        assert len(c.view) == 1

        assert "Invalid" in c.set_limit("~")
Exemple #13
0
    def test_replay(self):
        cs = StringIO()

        o = dump.Options(server_replay="nonexistent", kill=True)
        libpry.raises(dump.DumpError, dump.DumpMaster, None, o, None, outfile=cs)

        t = self.tmpdir()
        p = os.path.join(t, "rep")
        f = open(p, "w")
        fw = flow.FlowWriter(f)
        t = tutils.tflow_full()
        t.response = tutils.tresp(t.request)
        fw.add(t)
        f.close()

        o = dump.Options(server_replay=p, kill=True)
        m = dump.DumpMaster(None, o, None, outfile=cs)
        
        self._cycle(m, "content")
        self._cycle(m, "content")

        o = dump.Options(server_replay=p, kill=False)
        m = dump.DumpMaster(None, o, None, outfile=cs)
        self._cycle(m, "nonexistent")

        o = dump.Options(client_replay=p, kill=False)
        m = dump.DumpMaster(None, o, None, outfile=cs)
Exemple #14
0
    def test_server_playback(self):
        controller.should_exit = False
        s = flow.State()

        f = tutils.tflow()
        f.response = tutils.tresp(f.request)
        pb = [f]

        fm = flow.FlowMaster(None, s)
        fm.refresh_server_playback = True
        assert not fm.do_server_playback(tutils.tflow())

        fm.start_server_playback(pb, False, [], False, False)
        assert fm.do_server_playback(tutils.tflow())

        fm.start_server_playback(pb, False, [], True, False)
        r = tutils.tflow()
        r.request.content = "gibble"
        assert not fm.do_server_playback(r)
        assert fm.do_server_playback(tutils.tflow())

        fm.start_server_playback(pb, False, [], True, False)
        q = Queue.Queue()
        fm.tick(q)
        assert controller.should_exit

        fm.stop_server_playback()
        assert not fm.server_playback
Exemple #15
0
 def test_run_script(self):
     f = tutils.tflow()
     f.response = tutils.tresp()
     f.request = f.response.request
     se = f.run_script("scripts/a")
     assert "DEBUG" == se.strip()
     assert f.request.host == "TESTOK"
Exemple #16
0
    def test_set_limit(self):
        c = flow.State()

        f = tutils.tflow()
        assert len(c.view) == 0

        c.add_request(f)
        assert len(c.view) == 1

        c.set_limit("~s")
        assert c.limit_txt == "~s"
        assert len(c.view) == 0
        f.response = tutils.tresp()
        c.add_response(f)
        assert len(c.view) == 1
        c.set_limit(None)
        assert len(c.view) == 1

        f = tutils.tflow()
        c.add_request(f)
        assert len(c.view) == 2
        c.set_limit("~q")
        assert len(c.view) == 1
        c.set_limit("~s")
        assert len(c.view) == 1

        assert "Invalid" in c.set_limit("~")
Exemple #17
0
    def test_get_cert(self):
        req = tutils.treq()
        resp = flow.Response(req, 200, "msg", flow.ODictCaseless(), "content", file("data/dercert").read())
        assert resp.get_cert()

        resp = tutils.tresp()
        assert not resp.get_cert()
Exemple #18
0
 def test_replace(self):
     r = tutils.tresp()
     r.headers["Foo"] = ["fOo"]
     r.content = "afoob"
     assert r.replace("foo(?i)", "boo") == 3
     assert not "foo" in r.content
     assert r.headers["boo"] == ["boo"]
Exemple #19
0
 def test_run_script_err(self):
     f = tutils.tflow()
     f.response = tutils.tresp()
     f.request = f.response.request
     libpry.raises("returned error", f.run_script,"scripts/err_return")
     libpry.raises("invalid response", f.run_script,"scripts/err_data")
     libpry.raises("no such file", f.run_script,"nonexistent")
     libpry.raises("permission denied", f.run_script,"scripts/nonexecutable")
Exemple #20
0
 def _cycle(self, m, content):
     req = tutils.treq()
     req.content = content
     cc = req.client_conn
     resp = tutils.tresp(req)
     resp.content = content
     m.handle_clientconnect(cc)
     m.handle_request(req)
     m.handle_response(resp)
Exemple #21
0
 def test_get_cookies_simple(self):
     h = flow.ODictCaseless()
     h["Set-Cookie"] = ["cookiename=cookievalue"]
     resp = tutils.tresp()
     resp.headers = h
     result = resp.get_cookies()
     assert len(result)==1
     assert "cookiename" in result
     assert result["cookiename"] == ("cookievalue", {})
Exemple #22
0
    def test_refresh_cookie(self):
        r = tutils.tresp()

        # Invalid expires format, sent to us by Reddit.
        c = "rfoo=bar; Domain=reddit.com; expires=Thu, 31 Dec 2037 23:59:59 GMT; Path=/"
        assert r._refresh_cookie(c, 60)

        c = "MOO=BAR; Expires=Tue, 08-Mar-2011 00:20:38 GMT; Path=foo.com; Secure"
        assert "00:21:38" in r._refresh_cookie(c, 60)
    def test_set_cookies(self):
        resp = tutils.tresp()
        v = resp.get_cookies()
        v.add("foo", ["bar", odict.ODictCaseless()])
        resp.set_cookies(v)

        v = resp.get_cookies()
        assert len(v) == 1
        assert v["foo"] == [["bar", odict.ODictCaseless()]]
Exemple #24
0
 def test_backup(self):
     f = tutils.tflow()
     f.response = tutils.tresp()
     f.request.content = "foo"
     assert not f.modified()
     f.backup()
     f.request.content = "bar"
     assert f.modified()
     f.revert()
     assert f.request.content == "foo"
Exemple #25
0
    def test_all(self):
        t = tutils.tresp()
        t._set_replay()
        dump.str_response(t)

        t = tutils.treq()
        t.client_conn = None
        t.stickycookie = True
        assert "stickycookie" in dump.str_request(t)
        assert "replay" in dump.str_request(t)
Exemple #26
0
 def test_get_cookies_no_value(self):
     h = flow.ODictCaseless()
     h["Set-Cookie"] = ["cookiename=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/"]
     resp = tutils.tresp()
     resp.headers = h
     result = resp.get_cookies()
     assert len(result)==1
     assert "cookiename" in result
     assert result["cookiename"][0] == ""
     assert len(result["cookiename"][1])==2
Exemple #27
0
    def test_match(self):
        f = tutils.tflow()
        f.response = tutils.tresp()
        f.request = f.response.request
        assert not f.match(filt.parse("~b test"))
        assert f.match(None)
        assert not f.match(filt.parse("~b test"))

        f = tutils.tflow_err()
        assert f.match(filt.parse("~e"))
Exemple #28
0
 def test_missing_content(self):
     cs = StringIO()
     o = dump.Options(flow_detail=3)
     m = dump.DumpMaster(None, o, outfile=cs)
     f = tutils.tflow()
     f.request.content = http.CONTENT_MISSING
     m.handle_request(f)
     f.response = tutils.tresp()
     f.response.content = http.CONTENT_MISSING
     m.handle_response(f)
     assert "content missing" in cs.getvalue()
Exemple #29
0
 def _cycle(self, m, content):
     req = tutils.treq()
     req.content = content
     cc = req.client_conn
     cc.connection_error = "error"
     resp = tutils.tresp(req)
     resp.content = content
     m.handle_clientconnect(cc)
     m.handle_request(req)
     m.handle_response(resp)
     m.handle_clientdisconnect(flow.ClientDisconnect(cc))
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()
 def test_repr(self):
     r = tutils.tresp()
     assert "unknown content type" in repr(r)
     r.headers["content-type"] = ["foo"]
     assert "foo" in repr(r)
     assert repr(tutils.tresp(content=CONTENT_MISSING))
Exemple #32
0
 def handle_request(self, m):
     resp = tutils.tresp()
     m.reply(resp)
Exemple #33
0
 def handle_request(self, m):
     resp = tutils.tresp()
     resp.content = flow.CONTENT_MISSING
     m.reply(resp)
Exemple #34
0
 def _add_response(self, state):
     f = tutils.tflow()
     state.add_flow(f)
     f.response = tutils.tresp()
     state.update_flow(f)
 def _add_response(self, state):
     f = self._add_request(state)
     r = tutils.tresp(f.request)
     state.add_response(r)
 def test_add_response(self):
     c = console.ConsoleState()
     f = self._add_request(c)
     r = tutils.tresp(f.request)
     c.focus = None
     c.add_response(r)
Exemple #37
0
 def test_get_cookies_none(self):
     h = flow.ODictCaseless()
     resp = tutils.tresp()
     resp.headers = h
     assert not resp.get_cookies()
Exemple #38
0
 def handle_request(self, f):
     resp = tutils.tresp()
     f.reply(resp)
Exemple #39
0
 def test_get_content_type(self):
     h = flow.ODictCaseless()
     h["Content-Type"] = ["text/plain"]
     resp = tutils.tresp()
     resp.headers = h
     assert resp.headers.get_first("content-type") == "text/plain"
Exemple #40
0
 def test_header_size(self):
     r = tutils.tresp()
     result = len(r._assemble_headers())
     assert result == 44
Exemple #41
0
 def _add_response(self, state):
     req = tutils.treq()
     state.add_request(req)
     resp = tutils.tresp(req)
     state.add_response(resp)
Exemple #42
0
 def handle_request(self, f):
     resp = tutils.tresp()
     resp.content = CONTENT_MISSING
     f.reply(resp)
Exemple #43
0
 def test_add_response(self):
     c = console.ConsoleState()
     f = self._add_request(c)
     f.response = tutils.tresp()
     c.focus = None
     c.update_flow(f)
Exemple #44
0
 def test_match(self):
     f = tutils.tflow()
     f.response = tutils.tresp()
     f.request = f.response.request
     assert not f.match(filt.parse("~b test"))
     assert not f.match(None)
Exemple #45
0
 def _add_response(self, state):
     f = self._add_request(state)
     f.response = tutils.tresp()
     state.update_flow(f)