示例#1
0
def test_load_scripts():
    example_dir = utils.Data(__name__).path("../../examples")
    scripts = glob.glob("%s/*.py" % example_dir)

    tmaster = tservers.TestMaster(config.ProxyConfig())

    for f in scripts:
        if "har_extractor" in f:
            continue
        if "flowwriter" in f:
            f += " -"
        if "iframe_injector" in f:
            f += " foo"  # one argument required
        if "filt" in f:
            f += " ~a"
        if "modify_response_body" in f:
            f += " foo bar"  # two arguments required
        try:
            s = script.Script(
                f, script.ScriptContext(tmaster))  # Loads the script file.
        except Exception as v:
            if "ImportError" not in str(v):
                raise
        else:
            s.unload()
示例#2
0
def test_concurrent2():
    s = flow.State()
    fm = flow.FlowMaster(None, s)
    s = script.Script(tutils.test_data.path("scripts/concurrent_decorator.py"),
                      script.ScriptContext(fm))
    s.load()
    m = mock.Mock()

    class Dummy:
        def __init__(self):
            self.response = self
            self.error = self
            self.reply = m

    t_start = time.time()

    for hook in ("clientconnect", "serverconnect", "response", "error",
                 "clientconnect"):
        d = Dummy()
        s.run(hook, d)
        d.reply()
    while (time.time() - t_start) < 20 and m.call_count <= 5:
        if m.call_count == 5:
            return
        time.sleep(0.001)
    assert False
示例#3
0
    def load_script(self, command, use_reloader=False):
        """
            Loads a script.

            Raises:
                ScriptException
        """
        s = script.Script(command, script.ScriptContext(self))
        s.load()
        if use_reloader:
            script.reloader.watch(s, lambda: self.event_queue.put(("script_change", s)))
        self.scripts.append(s)
示例#4
0
def test_simple():
    s = flow.State()
    fm = flow.FlowMaster(None, s)
    sp = tutils.test_data.path("scripts/a.py")
    p = script.Script("%s --var 40" % sp, script.ScriptContext(fm))

    assert "here" in p.ns
    assert p.run("here") == 41
    assert p.run("here") == 42

    tutils.raises(script.ScriptException, p.run, "errargs")

    # Check reload
    p.load()
    assert p.run("here") == 41
示例#5
0
def test_err():
    s = flow.State()
    fm = flow.FlowMaster(None, s)
    sc = script.ScriptContext(fm)

    tutils.raises("not found", script.Script, "nonexistent", sc)

    tutils.raises("not a file", script.Script,
                  tutils.test_data.path("scripts"), sc)

    tutils.raises(script.ScriptException, script.Script,
                  tutils.test_data.path("scripts/syntaxerr.py"), sc)

    tutils.raises(script.ScriptException, script.Script,
                  tutils.test_data.path("scripts/loaderr.py"), sc)

    scr = script.Script(tutils.test_data.path("scripts/unloaderr.py"), sc)
    tutils.raises(script.ScriptException, scr.unload)
示例#6
0
    def run_script_once(self, command, f):
        if not command:
            return
        signals.add_log("Running script on flow: %s" % command, "debug")

        try:
            s = script.Script(command)
            s.load()
        except script.ScriptException as e:
            signals.status_message.send(
                message='Error loading "{}".'.format(command))
            signals.add_log('Error loading "{}":\n{}'.format(command, e),
                            "error")
            return

        if f.request:
            self._run_script_method("request", s, f)
        if f.response:
            self._run_script_method("response", s, f)
        if f.error:
            self._run_script_method("error", s, f)
        s.unload()
        signals.flow_change.send(self, flow=f)
示例#7
0
def example(command):
    command = os.path.join(example_dir, command)
    ctx = DummyContext()
    with script.Script(command, ctx) as s:
        yield s
示例#8
0
def example(command):
    command = os.path.join(example_dir, command)
    ctx = DummyContext()
    s = script.Script(command, ctx)
    yield s
    s.unload()
示例#9
0
def test_command_parsing():
    s = flow.State()
    fm = flow.FlowMaster(None, s)
    absfilepath = os.path.normcase(tutils.test_data.path("scripts/a.py"))
    s = script.Script(absfilepath, script.ScriptContext(fm))
    assert os.path.isfile(s.args[0])