use of org.apache.jmeter.samplers.SampleResult in project jmeter-plugins by undera.
the class JSONPathExtractorTest method testReported2.
@Ignore
// FIXME: we need to solve this one day
@Test
public void testReported2() {
System.out.println("process reported");
JMeterContext context = JMeterContextService.getContext();
SampleResult res = new SampleResult();
res.setResponseData(json3.getBytes());
context.setPreviousResult(res);
JSONPathExtractor instance = new JSONPathExtractor();
instance.setVar("var");
instance.setJsonPath("$.data[?(@.attr.value>0)][0].attr");
instance.setDefaultValue("NOTFOUND");
instance.process();
JMeterVariables vars = context.getVariables();
assertNotEquals("NOTFOUND", vars.get("var"));
assertEquals("{value=1}", vars.get("var"));
}
use of org.apache.jmeter.samplers.SampleResult in project jmeter-plugins by undera.
the class JSONPathExtractorTest method testProcess_chinese.
@Test
public void testProcess_chinese() {
JMeterContext context = JMeterContextService.getContext();
SampleResult res = new SampleResult();
String chinese = "{\"carBrandName\":\"大众\"}";
res.setResponseData(chinese.getBytes());
context.setPreviousResult(res);
JSONPathExtractor instance = new JSONPathExtractor();
instance.setDefaultValue("DEFAULT");
instance.setVar("test");
instance.setJsonPath("$.carBrandName");
instance.process();
JMeterVariables vars = context.getVariables();
// freaking "static final" DEFAULT_ENCODING field in SampleResult does not allow us to assert this
// assertEquals("大众", vars.get("test"));
}
use of org.apache.jmeter.samplers.SampleResult in project jmeter-plugins by undera.
the class JSONPathExtractorTest method testProcess.
@Test
public void testProcess() {
System.out.println("process");
JMeterContext context = JMeterContextService.getContext();
SampleResult res = new SampleResult();
res.setResponseData(json.getBytes());
context.setPreviousResult(res);
JSONPathExtractor instance = new JSONPathExtractor();
instance.setDefaultValue("DEFAULT");
instance.setVar("test");
instance.setJsonPath("$.store.book[*].author");
instance.process();
JMeterVariables vars = context.getVariables();
assertEquals("[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]", vars.get("test"));
}
use of org.apache.jmeter.samplers.SampleResult in project jmeter-plugins by undera.
the class OAuthSampler method sample.
@Override
public SampleResult sample() {
HttpMethodBase httpMethod = null;
HttpClient client = null;
InputStream instream = null;
SampleResult res = new SampleResult();
try {
res.setSuccessful(false);
res.setResponseCode("000");
res.setSampleLabel(getName());
res.setURL(getUrl());
res.setDataEncoding("UTF-8");
res.setDataType("text/xml");
res.setSamplerData(getRequestBody());
res.setMonitor(isMonitor());
res.sampleStart();
String urlStr = getUrl().toString();
log.debug("Start : sample " + urlStr);
log.debug("method " + getMethod());
httpMethod = createHttpMethod(getMethod(), urlStr);
setDefaultRequestHeaders(httpMethod);
client = setupConnection(getUrl(), httpMethod);
if (httpMethod instanceof EntityEnclosingMethod) {
((EntityEnclosingMethod) httpMethod).setRequestEntity(new StringRequestEntity(getRequestBody(), "text/xml", "UTF-8"));
}
overrideHeaders(httpMethod, urlStr, getMethod());
res.setRequestHeaders(getConnectionHeaders(httpMethod));
int statusCode = -1;
try {
statusCode = client.executeMethod(httpMethod);
} catch (RuntimeException e) {
log.error("Exception when executing '" + httpMethod + "'", e);
throw e;
}
instream = httpMethod.getResponseBodyAsStream();
if (instream != null) {
Header responseHeader = httpMethod.getResponseHeader(HTTPConstantsInterface.HEADER_CONTENT_ENCODING);
if (responseHeader != null && HTTPConstantsInterface.ENCODING_GZIP.equals(responseHeader.getValue())) {
instream = new GZIPInputStream(instream);
}
res.setResponseData(readResponse(res, instream, (int) httpMethod.getResponseContentLength()));
}
res.sampleEnd();
res.setResponseCode(Integer.toString(statusCode));
res.setSuccessful(isSuccessCode(statusCode));
res.setResponseMessage(httpMethod.getStatusText());
String ct = null;
org.apache.commons.httpclient.Header h = httpMethod.getResponseHeader(HTTPConstantsInterface.HEADER_CONTENT_TYPE);
if (h != null) {
ct = h.getValue();
res.setContentType(ct);
res.setEncodingAndType(ct);
}
String responseHeaders = getResponseHeaders(httpMethod);
res.setResponseHeaders(responseHeaders);
log.debug("End : sample");
httpMethod.releaseConnection();
return res;
} catch (MalformedURLException e) {
res.sampleEnd();
log.warn(e.getMessage());
res.setResponseMessage(e.getMessage());
return res;
} catch (IllegalArgumentException e) {
res.sampleEnd();
log.warn(e.getMessage());
res.setResponseMessage(e.getMessage());
return res;
} catch (IOException e) {
res.sampleEnd();
log.warn(e.getMessage());
res.setResponseMessage(e.getMessage());
return res;
} finally {
JOrphanUtils.closeQuietly(instream);
if (httpMethod != null) {
httpMethod.releaseConnection();
return res;
}
}
}
use of org.apache.jmeter.samplers.SampleResult in project jmeter-plugins by undera.
the class WebDriverSampler method sample.
@Override
public SampleResult sample(Entry e) {
if (getWebDriver() == null) {
throw new IllegalArgumentException("Browser has not been configured. Please ensure at least 1 WebDriverConfig is created for a ThreadGroup.");
}
SampleResult res;
try {
res = sampleResultClass.newInstance();
} catch (InstantiationException e1) {
LOGGER.warn("Class " + sampleResultClass + " failed to instantiate, defaulted to " + SampleResult.class.getCanonicalName(), e1);
res = new SampleResult();
} catch (IllegalAccessException e1) {
LOGGER.warn("Class " + sampleResultClass + " failed to instantiate, defaulted to " + SampleResult.class.getCanonicalName(), e1);
res = new SampleResult();
}
res.setSampleLabel(getName());
res.setSamplerData(toString());
res.setDataType(SampleResult.TEXT);
res.setContentType("text/plain");
res.setDataEncoding("UTF-8");
res.setSuccessful(true);
LOGGER.debug("Current thread name: '" + getThreadName() + "', has browser: '" + getWebDriver() + "'");
try {
final ScriptEngine scriptEngine = createScriptEngineWith(res);
scriptEngine.eval(getScript());
// setup the data in the SampleResult
res.setResponseData(getWebDriver().getPageSource(), null);
res.setURL(new URL(getWebDriver().getCurrentUrl()));
if (StringUtils.isEmpty(res.getResponseCode())) {
res.setResponseCode(res.isSuccessful() ? "200" : "500");
}
if (res.isSuccessful()) {
res.setResponseMessageOK();
}
} catch (Exception ex) {
LOGGER.error(ex.getMessage());
res.setResponseMessage(ex.getMessage());
res.setResponseData((ex.toString() + "\r\n" + JMeterPluginsUtils.getStackTrace(ex)).getBytes());
res.setResponseCode("500");
res.setSuccessful(false);
if (res.getStartTime() == 0) {
res.sampleStart();
}
if (res.getEndTime() == 0) {
res.sampleEnd();
}
}
return res;
}
Aggregations