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);
}
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()));
}
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()));
}
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;
}
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;
}
Aggregations