use of groovy.lang.Closure in project mdw-designer by CenturyLinkCloud.
the class GroovyTestCaseScript method xpath.
/**
* Matches according to MDW XPath.
*/
public Closure<Boolean> xpath(final String condition) throws TestException {
return new Closure<Boolean>(this, this) {
@Override
public Boolean call(Object request) {
try {
XmlPath xpath = new XmlPath(condition);
String v = xpath.evaluate(TestDataFilter.parseRequest(request.toString()));
return v != null;
} catch (MbengException ex) {
getTestCaseRun().log.println("Failed to parse request as XML/JSON. Stub response: " + AdapterActivity.MAKE_ACTUAL_CALL);
return false;
}
}
};
}
use of groovy.lang.Closure in project mdw-designer by CenturyLinkCloud.
the class GroovyTestCaseScript method gpath.
/**
* Matches according to GPath.
*/
public Closure<Boolean> gpath(final String condition) throws TestException {
return new Closure<Boolean>(this, this) {
@Override
public Boolean call(Object request) {
try {
GPathResult gpathRequest = new XmlSlurper().parseText(request.toString());
Binding binding = getBinding();
binding.setVariable("request", gpathRequest);
return (Boolean) new GroovyShell(binding).evaluate(condition);
} catch (Exception ex) {
getTestCaseRun().log.println("Failed to parse request as XML/JSON. Stub response: " + AdapterActivity.MAKE_ACTUAL_CALL);
return false;
}
}
};
}
use of groovy.lang.Closure in project rest-assured by rest-assured.
the class EncoderRegistry method encodeXML.
/**
* Encode the content as XML. The argument may be either an object whose
* <code>toString</code> produces valid markup, or a Closure which will be
* interpreted as a builder definition.
*
* @param xml data that defines the XML structure
* @return an {@link HttpEntity} encapsulating this request data
* @throws UnsupportedEncodingException
*/
public HttpEntity encodeXML(Object contentType, Object xml) throws UnsupportedEncodingException {
String contentTypeAsString = contentTypeToString(contentType);
if (xml instanceof Closure) {
StreamingMarkupBuilder smb = new StreamingMarkupBuilder();
xml = smb.bind(xml);
} else if (xml instanceof File) {
xml = toString((File) xml, contentTypeAsString);
}
return createEntity(contentTypeAsString, xml);
}
use of groovy.lang.Closure in project rest-assured by rest-assured.
the class EncoderRegistry method encodeText.
/**
* Default handler used for a plain text content-type. Acceptable argument
* types are:
* <ul>
* <li>Closure</li>
* <li>Writable</li>
* <li>Reader</li>
* </ul>
* For Closure argument, a {@link PrintWriter} is passed as the single
* argument to the closure. Any data sent to the writer from the
* closure will be sent to the request content body.
*
* @param data
* @return an {@link HttpEntity} encapsulating this request data
* @throws IOException
*/
public HttpEntity encodeText(Object contentType, Object data) throws IOException {
String contentTypeAsString = contentTypeToString(contentType);
if (data instanceof Closure) {
StringWriter out = new StringWriter();
PrintWriter writer = new PrintWriter(out);
((Closure) data).call(writer);
writer.close();
out.flush();
data = out;
} else if (data instanceof Writable) {
StringWriter out = new StringWriter();
((Writable) data).writeTo(out);
out.flush();
data = out;
} else if (data instanceof Reader && !(data instanceof BufferedReader)) {
data = new BufferedReader((Reader) data);
} else if (data instanceof File) {
data = toString((File) data, contentTypeAsString);
}
if (data instanceof BufferedReader) {
StringWriter out = new StringWriter();
DefaultGroovyMethods.leftShift(out, (BufferedReader) data);
data = out;
}
// if data is a String, we are already covered.
return createEntity(contentTypeAsString, data);
}
use of groovy.lang.Closure in project rest-assured by rest-assured.
the class EncoderRegistry method buildDefaultEncoderMap.
/**
* Returns a map of default encoders. Override this method to change
* what encoders are registered by default. You can of course call
* <code>super.buildDefaultEncoderMap()</code> and then add or remove
* from that result as well.
*/
protected Map<String, Closure> buildDefaultEncoderMap() {
Map<String, Closure> encoders = new HashMap<String, Closure>();
encoders.put(ContentType.BINARY.toString(), new MethodClosure(this, "encodeStream"));
encoders.put(ContentType.TEXT.toString(), new MethodClosure(this, "encodeText"));
encoders.put(ContentType.URLENC.toString(), new MethodClosure(this, "encodeForm"));
Closure encClosure = new MethodClosure(this, "encodeXML");
for (String ct : ContentType.XML.getContentTypeStrings()) encoders.put(ct, encClosure);
encoders.put(ContentType.HTML.toString(), encClosure);
encClosure = new MethodClosure(this, "encodeJSON");
for (String ct : ContentType.JSON.getContentTypeStrings()) encoders.put(ct, encClosure);
return encoders;
}
Aggregations