use of org.concordion.api.Element in project timbuctoo by HuygensING.
the class RequestCommand method formatRequest.
private void formatRequest(Element origRequestElement, HttpRequest httpRequest) {
Element caption = null;
if (addCaptions) {
caption = new Element("div").addAttribute("class", "requestCaption").appendText("Request:");
if (httpRequest.server != null) {
caption.appendChild(new Element("small").appendText(" (to " + httpRequest.server + ")"));
}
}
Element newRequestElement = replaceWithEmptyElement(origRequestElement, name, namespace, caption);
addClass(newRequestElement, "requestContent");
Element requestPre = new Element("pre");
newRequestElement.appendChild(requestPre);
requestPre.appendChild(new Element("span").appendText(httpRequest.method + " "));
requestPre.appendChild(new Element("b").appendText(httpRequest.getPathAndQuery() + " "));
requestPre.appendChild(new Element("span").addAttribute("class", "defaultValue").appendText("HTTP/1.1"));
for (Map.Entry<String, String> header : httpRequest.headers.entries()) {
requestPre.appendText("\n" + header.getKey() + ": " + header.getValue());
}
if (httpRequest.body != null) {
requestPre.appendText("\n\n");
requestPre.appendChild(new Element("span").appendText(httpRequest.body).addAttribute("class", "reqBody"));
}
}
use of org.concordion.api.Element in project timbuctoo by HuygensING.
the class ResponseCommand method verify.
@Override
public void verify(CommandCall commandCall, Evaluator evaluator, ResultRecorder resultRecorder) {
ValidationResult validationResult;
if (!StringUtils.isBlank(verificationMethod)) {
evaluator.setVariable("#nl_knaw_huygens_httpcommand_result", requestCommand.getActualResult());
evaluator.setVariable("#nl_knaw_huygens_httpcommand_expectation", expectation);
validationResult = (ValidationResult) evaluator.evaluate(verificationMethod + "(#nl_knaw_huygens_httpcommand_expectation, #nl_knaw_huygens_httpcommand_result)");
evaluator.setVariable("#nl_knaw_huygens_httpcommand_result", null);
evaluator.setVariable("#nl_knaw_huygens_httpcommand_expectation", null);
} else {
validationResult = defaultValidator.validate(expectation, requestCommand.getActualResult());
}
Element caption = null;
if (addCaptions) {
caption = new Element("div").addAttribute("class", "responseCaption").appendText("Response:");
}
Element resultElement = replaceWithEmptyElement(commandCall.getElement(), name, namespace, caption);
addClass(resultElement, "responseContent");
try {
Builder builder = new Builder();
Document document = builder.build(new StringReader(validationResult.getMessage()));
// new Element() creates a deepcopy not attached to the doc
nu.xom.Element rootElement = new nu.xom.Element(document.getRootElement());
resultElement.appendChild(new Element(rootElement));
resultRecorder.record(validationResult.isSucceeded() ? Result.SUCCESS : Result.FAILURE);
} catch (ParsingException | IOException e) {
resultRecorder.record(Result.FAILURE);
if (e instanceof ParsingException) {
resultElement.appendText("Error at line " + ((ParsingException) e).getLineNumber() + ", column: " + ((ParsingException) e).getColumnNumber());
resultElement.appendText(validationResult.getMessage());
}
}
}
use of org.concordion.api.Element in project timbuctoo by HuygensING.
the class Utils method replaceWithEmptyElement.
static Element replaceWithEmptyElement(Element origElement, String name, String namespace, Element caption) {
Element anchor;
if (caption != null) {
origElement.appendSister(caption);
anchor = caption;
} else {
anchor = origElement;
}
Element resultElement = new Element(origElement.getLocalName());
anchor.appendSister(resultElement);
origElement.moveAttributesTo(resultElement);
resultElement.removeAttribute(name, namespace);
origElement.getParentElement().removeChild(origElement);
return resultElement;
}
Aggregations