Search in sources :

Example 16 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class URLRewritingModifier method process.

@Override
public void process() {
    JMeterContext ctx = getThreadContext();
    Sampler sampler = ctx.getCurrentSampler();
    if (!(sampler instanceof HTTPSamplerBase)) {
        // Ignore non-HTTP samplers
        return;
    }
    SampleResult responseText = ctx.getPreviousResult();
    if (responseText == null) {
        return;
    }
    initRegex(getArgumentName());
    String text = responseText.getResponseDataAsString();
    Perl5Matcher matcher = JMeterUtils.getMatcher();
    String value = "";
    if (isPathExtension() && isPathExtensionNoEquals() && isPathExtensionNoQuestionmark()) {
        if (matcher.contains(text, pathExtensionNoEqualsNoQuestionmarkRegexp)) {
            MatchResult result = matcher.getMatch();
            value = result.group(1);
        }
    } else if (// && !isPathExtensionNoQuestionmark()
    isPathExtension() && isPathExtensionNoEquals()) {
        if (matcher.contains(text, pathExtensionNoEqualsQuestionmarkRegexp)) {
            MatchResult result = matcher.getMatch();
            value = result.group(1);
        }
    } else if (// && !isPathExtensionNoEquals()
    isPathExtension() && isPathExtensionNoQuestionmark()) {
        if (matcher.contains(text, pathExtensionEqualsNoQuestionmarkRegexp)) {
            MatchResult result = matcher.getMatch();
            value = result.group(1);
        }
    } else if (// && !isPathExtensionNoEquals() && !isPathExtensionNoQuestionmark()
    isPathExtension()) {
        if (matcher.contains(text, pathExtensionEqualsQuestionmarkRegexp)) {
            MatchResult result = matcher.getMatch();
            value = result.group(1);
        }
    } else // if ! isPathExtension()
    {
        if (matcher.contains(text, parameterRegexp)) {
            MatchResult result = matcher.getMatch();
            for (int i = 1; i < result.groups(); i++) {
                value = result.group(i);
                if (value != null) {
                    break;
                }
            }
        }
    }
    // Bug 15025 - save session value across samplers
    if (shouldCache()) {
        if (value == null || value.length() == 0) {
            value = savedValue;
        } else {
            savedValue = value;
        }
    }
    modify((HTTPSamplerBase) sampler, value);
}
Also used : JMeterContext(org.apache.jmeter.threads.JMeterContext) Sampler(org.apache.jmeter.samplers.Sampler) SampleResult(org.apache.jmeter.samplers.SampleResult) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult) HTTPSamplerBase(org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)

Example 17 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class TestJSONPostProcessor method testBug59609.

@Test
public void testBug59609() throws ParseException {
    JMeterContext context = JMeterContextService.getContext();
    JSONPostProcessor processor = setupProcessor(context, "0", false);
    String innerValue = "{\"a\":\"one\",\"b\":\"two\"}";
    String data = "{\"context\":" + innerValue + "}";
    SampleResult result = new SampleResult();
    result.setResponseData(data.getBytes(StandardCharsets.UTF_8));
    JMeterVariables vars = new JMeterVariables();
    context.setVariables(vars);
    context.setPreviousResult(result);
    processor.setJsonPathExpressions("$.context");
    processor.process();
    JSONParser parser = new JSONParser(0);
    Object expectedValue = parser.parse(innerValue);
    assertThat(parser.parse(vars.get(VAR_NAME)), CoreMatchers.is(expectedValue));
    assertThat(vars.get(VAR_NAME + "_matchNr"), CoreMatchers.is(CoreMatchers.nullValue()));
    assertThat(vars.get(VAR_NAME + "_1"), CoreMatchers.is(CoreMatchers.nullValue()));
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JSONPostProcessor(org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor) SampleResult(org.apache.jmeter.samplers.SampleResult) JSONParser(net.minidev.json.parser.JSONParser) Test(org.junit.Test)

Example 18 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class TestJSONPostProcessor method testPR235CaseEmptyResponse.

@Test
public void testPR235CaseEmptyResponse() {
    JMeterContext context = JMeterContextService.getContext();
    JSONPostProcessor processor = setupProcessor(context, "-1", true);
    JMeterVariables vars = new JMeterVariables();
    processor.setDefaultValues("NONE");
    processor.setJsonPathExpressions("$[*]");
    processor.setRefNames("varname");
    processor.setScopeVariable("contentvar");
    context.setVariables(vars);
    vars.put("contentvar", "[\"one\", \"two\"]");
    processor.process();
    assertThat(vars.get("varname_1"), CoreMatchers.is("one"));
    assertThat(vars.get("varname_2"), CoreMatchers.is("two"));
    assertThat(vars.get("varname_matchNr"), CoreMatchers.is("2"));
    vars.put("contentvar", "");
    processor.process();
    assertThat(vars.get("varname_matchNr"), CoreMatchers.is(CoreMatchers.nullValue()));
    assertThat(vars.get("varname_1"), CoreMatchers.is(CoreMatchers.nullValue()));
    assertThat(vars.get("varname_2"), CoreMatchers.is(CoreMatchers.nullValue()));
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JSONPostProcessor(org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor) Test(org.junit.Test)

Example 19 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class JavaScript method executeWithRhino.

/**
     * @param previousResult {@link SampleResult}
     * @param currentSampler {@link Sampler}
     * @param jmctx {@link JMeterContext}
     * @param vars {@link JMeterVariables}
     * @param script Javascript code
     * @param varName variable name
     * @return result as String
     * @throws InvalidVariableException
     */
private String executeWithRhino(SampleResult previousResult, Sampler currentSampler, JMeterContext jmctx, JMeterVariables vars, String script, String varName) throws InvalidVariableException {
    Context cx = Context.enter();
    String resultStr = null;
    try {
        Scriptable scope = cx.initStandardObjects(null);
        // Set up some objects for the script to play with
        //$NON-NLS-1$
        scope.put("log", scope, log);
        //$NON-NLS-1$
        scope.put("ctx", scope, jmctx);
        //$NON-NLS-1$
        scope.put("vars", scope, vars);
        //$NON-NLS-1$
        scope.put("props", scope, JMeterUtils.getJMeterProperties());
        // Previously mis-spelt as theadName
        //$NON-NLS-1$
        scope.put("threadName", scope, Thread.currentThread().getName());
        //$NON-NLS-1$
        scope.put("sampler", scope, currentSampler);
        //$NON-NLS-1$
        scope.put("sampleResult", scope, previousResult);
        //$NON-NLS-1$
        Object result = cx.evaluateString(scope, script, "<cmd>", 1, null);
        resultStr = Context.toString(result);
        if (varName != null && vars != null) {
            // vars can be null if run from TestPlan
            vars.put(varName, resultStr);
        }
    } catch (RhinoException e) {
        log.error("Error processing Javascript: [" + script + "]\n", e);
        throw new InvalidVariableException("Error processing Javascript: [" + script + "]", e);
    } finally {
        Context.exit();
    }
    return resultStr;
}
Also used : Context(org.mozilla.javascript.Context) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) JMeterContext(org.apache.jmeter.threads.JMeterContext) RhinoException(org.mozilla.javascript.RhinoException) Scriptable(org.mozilla.javascript.Scriptable)

Example 20 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class Jexl2Function method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    //$NON-NLS-1$
    String str = "";
    CompoundVariable var = (CompoundVariable) values[0];
    String exp = var.execute();
    //$NON-NLS-1$
    String varName = "";
    if (values.length > 1) {
        varName = ((CompoundVariable) values[1]).execute().trim();
    }
    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();
    try {
        JexlContext jc = new MapContext();
        //$NON-NLS-1$
        jc.set("log", log);
        //$NON-NLS-1$
        jc.set("ctx", jmctx);
        //$NON-NLS-1$
        jc.set("vars", vars);
        //$NON-NLS-1$
        jc.set("props", JMeterUtils.getJMeterProperties());
        // Previously mis-spelt as theadName
        //$NON-NLS-1$
        jc.set("threadName", Thread.currentThread().getName());
        //$NON-NLS-1$ (may be null)
        jc.set("sampler", currentSampler);
        //$NON-NLS-1$ (may be null)
        jc.set("sampleResult", previousResult);
        //$NON-NLS-1$
        jc.set("OUT", System.out);
        // Now evaluate the script, getting the result
        Expression e = getJexlEngine().createExpression(exp);
        Object o = e.evaluate(jc);
        if (o != null) {
            str = o.toString();
        }
        if (vars != null && varName.length() > 0) {
            // vars will be null on TestPlan
            vars.put(varName, str);
        }
    } catch (Exception e) {
        log.error("An error occurred while evaluating the expression \"" + exp + "\"\n", e);
    }
    return str;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext)

Aggregations

JMeterContext (org.apache.jmeter.threads.JMeterContext)47 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)33 SampleResult (org.apache.jmeter.samplers.SampleResult)21 Test (org.junit.Test)11 Sampler (org.apache.jmeter.samplers.Sampler)9 JSONPostProcessor (org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor)8 Before (org.junit.Before)8 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)7 Properties (java.util.Properties)3 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)3 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)3 JMeterException (org.apache.jorphan.util.JMeterException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ReplaceStringWithFunctions (org.apache.jmeter.engine.util.ReplaceStringWithFunctions)2 HTTPSamplerBase (org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)2 StringProperty (org.apache.jmeter.testelement.property.StringProperty)2 BeanShellInterpreter (org.apache.jmeter.util.BeanShellInterpreter)2 MatchResult (org.apache.oro.text.regex.MatchResult)2