def test_stream(self):
        with tutils.tmpdir() as tdir:
            p = os.path.join(tdir, "foo")

            def r():
                r = FlowReader(open(p, "rb"))
                return list(r.stream())

            s = state.State()
            o = options.Options(outfile=(p, "wb"))
            m = master.FlowMaster(o, None, s)
            sa = filestreamer.FileStreamer()

            m.addons.add(sa)
            f = tutils.tflow(resp=True)
            m.request(f)
            m.response(f)
            m.addons.remove(sa)

            assert r()[0].response

            m.options.outfile = (p, "ab")

            m.addons.add(sa)
            f = tutils.tflow()
            m.request(f)
            m.addons.remove(sa)
            assert not r()[1].response
Example #2
0
 def mk(self):
     s = state.State()
     o = options.Options(stickycookie=".*")
     m = master.FlowMaster(o, None, s)
     sc = stickycookie.StickyCookie()
     m.addons.add(o, sc)
     return s, m, sc
Example #3
0
 def mkmaster(self, **opts):
     s = state.State()
     o = options.Options(**opts)
     m = mastertest.RecordingMaster(o, None, s)
     sh = setheaders.SetHeaders()
     m.addons.add(o, sh)
     return m, sh
Example #4
0
def tscript(cmd, args=""):
    o = options.Options()
    cmd = example_dir.path(cmd) + " " + args
    m = RaiseMaster(o, None, state.State())
    sc = script.Script(cmd)
    m.addons.add(o, sc)
    return m, sc
Example #5
0
 def test_concurrent_err(self):
     s = state.State()
     m = mastertest.RecordingMaster(options.Options(), None, s)
     sc = script.Script(
         tutils.test_data.path(
             "data/addonscripts/concurrent_decorator_err.py"))
     with m.handlecontext():
         sc.start()
     assert "decorator not supported" in m.event_log[0][1]
Example #6
0
 def test_exception(self):
     s = state.State()
     o = options.Options()
     m = mastertest.RecordingMaster(o, None, s)
     sc = script.Script(tutils.test_data.path("data/addonscripts/error.py"))
     m.addons.add(o, sc)
     f = tutils.tflow(resp=True)
     m.request(f)
     assert m.event_log[0][0] == "error"
Example #7
0
 def test_addon(self):
     s = state.State()
     o = options.Options()
     m = master.FlowMaster(o, None, s)
     sc = script.Script(tutils.test_data.path("data/addonscripts/addon.py"))
     m.addons.add(sc)
     assert sc.ns.event_log == [
         'scriptstart', 'addonstart', 'addonconfigure'
     ]
Example #8
0
 def test_duplicate_flow(self):
     s = state.State()
     o = options.Options()
     fm = master.FlowMaster(o, None, s)
     fm.addons.add(
         script.Script(
             tutils.test_data.path("data/addonscripts/duplicate_flow.py")))
     f = tutils.tflow()
     fm.request(f)
     assert fm.state.flow_count() == 2
     assert not fm.state.view[0].request.is_replay
     assert fm.state.view[1].request.is_replay
Example #9
0
 def test_simple(self):
     s = state.State()
     o = options.Options(scripts=[])
     m = master.FlowMaster(o, None, s)
     sc = script.ScriptLoader()
     m.addons.add(sc)
     assert len(m.addons) == 1
     o.update(
         scripts=[tutils.test_data.path("data/addonscripts/recorder.py")])
     assert len(m.addons) == 2
     o.update(scripts=[])
     assert len(m.addons) == 1
Example #10
0
 def test_concurrent(self):
     s = state.State()
     m = master.FlowMaster(options.Options(), None, s)
     sc = script.Script(
         tutils.test_data.path("data/addonscripts/concurrent_decorator.py"))
     m.addons.add(sc)
     f1, f2 = tutils.tflow(), tutils.tflow()
     m.request(f1)
     m.request(f2)
     start = time.time()
     while time.time() - start < 5:
         if f1.reply.state == f2.reply.state == "committed":
             return
     raise ValueError("Script never acked")
Example #11
0
 def test_exception(self):
     s = state.State()
     o = options.Options()
     m = mastertest.RecordingMaster(o, None, s)
     sc = script.Script(tutils.test_data.path("data/addonscripts/error.py"))
     m.addons.add(sc)
     f = tutils.tflow(resp=True)
     m.request(f)
     assert m.event_log[0][0] == "error"
     assert len(m.event_log[0][1].splitlines()) == 6
     assert 'addonscripts/error.py", line 7, in request' in m.event_log[0][
         1]
     assert 'addonscripts/error.py", line 3, in mkerr' in m.event_log[0][1]
     assert m.event_log[0][1].endswith("ValueError: Error!\n")
Example #12
0
 def test_concurrent(self):
     s = state.State()
     m = master.FlowMaster(options.Options(), None, s)
     sc = script.Script(
         tutils.test_data.path("data/addonscripts/concurrent_decorator.py"))
     m.addons.add(m.options, sc)
     f1, f2 = tutils.tflow(), tutils.tflow()
     self.invoke(m, "request", f1)
     self.invoke(m, "request", f2)
     start = time.time()
     while time.time() - start < 5:
         if f1.reply.acked and f2.reply.acked:
             return
     raise ValueError("Script never acked")
Example #13
0
    def test_contentview(self, view_auto):
        view_auto.side_effect = exceptions.ContentViewException("")

        s = state.State()
        sio = StringIO()
        o = dump.Options(
            flow_detail=4,
            verbosity=3,
            tfile=sio,
        )
        m = mastertest.RecordingMaster(o, None, s)
        d = dumper.Dumper()
        m.addons.add(o, d)
        self.invoke(m, "response", tutils.tflow())
        assert "Content viewer failed" in m.event_log[0][1]
Example #14
0
    def test_simple(self):
        s = state.State()
        o = options.Options(anticomp=True)
        m = master.FlowMaster(o, None, s)
        sa = anticomp.AntiComp()
        m.addons.add(o, sa)

        f = tutils.tflow(resp=True)
        m.request(f)

        f = tutils.tflow(resp=True)

        f.request.headers["Accept-Encoding"] = "foobar"
        m.request(f)
        assert f.request.headers["Accept-Encoding"] == "identity"
Example #15
0
    def test_order(self):
        rec = tutils.test_data.path("data/addonscripts/recorder.py")

        s = state.State()
        o = options.Options(
            scripts = [
                "%s %s" % (rec, "a"),
                "%s %s" % (rec, "b"),
                "%s %s" % (rec, "c"),
            ]
        )
        m = mastertest.RecordingMaster(o, None, s)
        sc = script.ScriptLoader()
        m.addons.add(sc)

        debug = [(i[0], i[1]) for i in m.event_log if i[0] == "debug"]
        assert debug == [
            ('debug', 'a start'), ('debug', 'a configure'),
            ('debug', 'b start'), ('debug', 'b configure'),
            ('debug', 'c start'), ('debug', 'c configure')
        ]
        m.event_log[:] = []

        o.scripts = [
            "%s %s" % (rec, "c"),
            "%s %s" % (rec, "a"),
            "%s %s" % (rec, "b"),
        ]
        debug = [(i[0], i[1]) for i in m.event_log if i[0] == "debug"]
        assert debug == [
            ('debug', 'c configure'),
            ('debug', 'a configure'),
            ('debug', 'b configure'),
        ]
        m.event_log[:] = []

        o.scripts = [
            "%s %s" % (rec, "x"),
            "%s %s" % (rec, "a"),
        ]
        debug = [(i[0], i[1]) for i in m.event_log if i[0] == "debug"]
        assert debug == [
            ('debug', 'c done'),
            ('debug', 'b done'),
            ('debug', 'x start'),
            ('debug', 'x configure'),
            ('debug', 'a configure'),
        ]
Example #16
0
    def test_simple(self):
        s = state.State()
        o = options.Options()
        m = master.FlowMaster(o, None, s)
        sc = script.Script(
            tutils.test_data.path("data/addonscripts/recorder.py"))
        m.addons.add(sc)
        assert sc.ns.call_log == [("solo", "start", (), {}),
                                  ("solo", "configure", (o, o.keys()), {})]

        sc.ns.call_log = []
        f = tutils.tflow(resp=True)
        m.request(f)

        recf = sc.ns.call_log[0]
        assert recf[1] == "request"
Example #17
0
    def test_simple(self):
        s = state.State()
        o = options.Options(stickyauth=".*")
        m = master.FlowMaster(o, None, s)
        sa = stickyauth.StickyAuth()
        m.addons.add(sa)

        f = tutils.tflow(resp=True)
        f.request.headers["authorization"] = "foo"
        m.request(f)

        assert "address" in sa.hosts

        f = tutils.tflow(resp=True)
        m.request(f)
        assert f.request.headers["authorization"] == "foo"
Example #18
0
    def test_simple(self):
        s = state.State()
        o = options.Options(anticache = True)
        m = master.FlowMaster(o, None, s)
        sa = anticache.AntiCache()
        m.addons.add(o, sa)

        f = tutils.tflow(resp=True)
        self.invoke(m, "request", f)

        f = tutils.tflow(resp=True)
        f.request.headers["if-modified-since"] = "test"
        f.request.headers["if-none-match"] = "test"
        self.invoke(m, "request", f)
        assert "if-modified-since" not in f.request.headers
        assert "if-none-match" not in f.request.headers
Example #19
0
    def test_reload(self):
        s = state.State()
        o = options.Options()
        m = mastertest.RecordingMaster(o, None, s)
        with tutils.tmpdir():
            with open("foo.py", "w"):
                pass
            sc = script.Script("foo.py")
            m.addons.add(sc)

            for _ in range(100):
                with open("foo.py", "a") as f:
                    f.write(".")
                m.addons.invoke_with_context(sc, "tick")
                time.sleep(0.1)
                if m.event_log:
                    return
            raise AssertionError("Change event not detected.")
Example #20
0
    def test_run_once(self):
        s = state.State()
        o = options.Options(scripts=[])
        m = master.FlowMaster(o, None, s)
        sl = script.ScriptLoader()
        m.addons.add(o, sl)

        f = tutils.tflow(resp=True)
        with m.handlecontext():
            sc = sl.run_once(
                tutils.test_data.path("data/addonscripts/recorder.py"), [f])
        evts = [i[1] for i in sc.ns.call_log]
        assert evts == [
            'start', 'request', 'responseheaders', 'response', 'done'
        ]

        with m.handlecontext():
            tutils.raises("file not found", sl.run_once, "nonexistent", [f])
Example #21
0
    def test_simple(self):
        s = state.State()
        o = options.Options(replacements=[
            ("~q", "foo", "bar"),
            ("~s", "foo", "bar"),
        ])
        m = master.FlowMaster(o, None, s)
        sa = replace.Replace()
        m.addons.add(o, sa)

        f = tutils.tflow()
        f.request.content = b"foo"
        self.invoke(m, "request", f)
        assert f.request.content == b"bar"

        f = tutils.tflow(resp=True)
        f.response.content = b"foo"
        self.invoke(m, "response", f)
        assert f.response.content == b"bar"
Example #22
0
 def clear_addons(self, addons):
     self.addons.clear()
     self.state = state.State()
     self.addons.add(self.state)
     self.addons.add(*addons)
Example #23
0
 def test_dupes(self):
     s = state.State()
     o = options.Options(scripts=["one", "one"])
     m = master.FlowMaster(o, None, s)
     sc = script.ScriptLoader()
     tutils.raises(exceptions.OptionsError, m.addons.add, o, sc)