package nl.hsac.fitnesse.fixture.slim;

import nl.hsac.fitnesse.fixture.util.XmlHttpResponse;

/**
 * Fixture to make HTTP requests returning XNL using Slim scripts and/or scenarios.
 * Example use: make SOAP calls using #postTo().
 */
public class XmlHttpTest extends HttpTest {
    /**
     * Register a prefix to use in XPath expressions.
     * @param prefix prefix to be used in xPath expressions.
     * @param namespace XML namespace the prefix should point to.
     */
    public void registerPrefixForNamespace(String prefix, String namespace) {
        getEnvironment().registerNamespace(prefix, getUrl(namespace));
    }

    /**
     * @return request sent last time postTo() or getFrom() was called.
     */
    @Override
    public String request() {
        return formatValue(super.request());
    }

    /**
     * @return response received last time postTo() or getFrom() was called.
     */
    @Override
    public String response() {
        return formatValue(super.response());
    }

    private String formatValue(String value) {
        String result;
        try {
            result = getEnvironment().getHtmlForXml(value);
        } catch (Exception e) {
            result = value;
        }
        return result;
    }

    /**
     * Gets XPath value, without ensuring response was valid.
     * @param xPathExpr expression to evaluate.
     * @return result of expression evaluation against last response received.
     */
    public String rawXPath(String xPathExpr) {
        return getResponse().getRawXPath(xPathExpr);
    }

    /**
     * @param xPathExpr expression to evaluate.
     * @return result of expression evaluation against last response received.
     * @throws RuntimeException if no valid response was available or XPath could not be evaluated.
     */
    public String xPath(String xPathExpr) {
        return getResponse().getXPath(xPathExpr);
    }

    /**
     * @param xPathExpr expression to evaluate.
     * @return result of expression evaluation against last response received.
     * @throws RuntimeException if no valid response was available or XPath could not be evaluated.
     */
    public Double xPathDouble(String xPathExpr) {
        return getResponse().getXPathDouble(xPathExpr);
    }

    /**
     * @param xPathExpr expression to evaluate.
     * @return result of expression evaluation against last response received.
     * @throws RuntimeException if no valid response was available or XPath could not be evaluated.
     */
    public Double xPathInt(String xPathExpr) {
        return getResponse().getXPathDouble(xPathExpr);
    }

    @Override
    protected boolean postProcessResponse() {
        // always called after post or get, so easy place to ensure namespaces are registered
        getResponse().setNamespaceContext(getEnvironment().getNamespaceContext());
        boolean result = super.postProcessResponse();
        return result;
    }

    @Override
    public XmlHttpResponse getResponse() {
        return (XmlHttpResponse) super.getResponse();
    }

    @Override
    protected XmlHttpResponse createResponse() {
        return new XmlHttpResponse();
    }
}

@@ -88,16 +88,40 @@
 ponse;%0A%0A
+import java.util.List;%0A%0A
 /**%0A * F
@@ -1261,32 +1261,965 @@
  result;%0A    %7D%0A%0A
+    /**%0A     * Gets a HTML list with all matches to the supplied XPath.%0A     * @param xPathExpr expression to evaluate.%0A     * @return list containing all results of expression evaluation against last response received, null if there were no matches.%0A     * @throws RuntimeException if no valid response was available or XPath could not be evaluated.%0A     */%0A    public String allXPathMatches(String xPathExpr) %7B%0A        String result = null;%0A        List%3CString%3E allXPath = getResponse().getAllXPath(xPathExpr);%0A        if (allXPath != null && !allXPath.isEmpty()) %7B%0A            StringBuilder sb = new StringBuilder();%0A            sb.append(%22%3Cdiv%3E%3Cul%3E%22);%0A            for (String match : allXPath) %7B%0A                sb.append(%22%3Cli%3E%22);%0A                sb.append(match);%0A                sb.append(%22%3C/li%3E%22);%0A            %7D%0A            sb.append(%22%3C/ul%3E%3C/div%3E%22);%0A            result = sb.toString();%0A        %7D%0A        return result;%0A    %7D%0A%0A
     /**%0A     * G

Add method to show all XPath matches