Search in sources :

Example 6 with HeaderMatcher

use of com.axway.ats.agent.core.templateactions.model.matchers.HeaderMatcher in project ats-framework by Axway.

the class TemplateLoadClient method skipHeader.

/**
     * Skip the specified header for the specified action step
     *
     * @param actionName the name of the action
     * @param stepNumber the action step number
     * @param headerToSkip the header to skip
     * @throws InvalidMatcherException
     */
@PublicAtsApi
public void skipHeader(String actionName, int stepNumber, String headerToSkip) throws InvalidMatcherException {
    List<HeaderMatcher> stepMatchers = getStepHeaderMatchers(actionName, stepNumber);
    stepMatchers.add(new HeaderMatcher(headerToSkip, null, TemplateHeaderMatchMode.RANDOM));
}
Also used : HeaderMatcher(com.axway.ats.agent.core.templateactions.model.matchers.HeaderMatcher) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 7 with HeaderMatcher

use of com.axway.ats.agent.core.templateactions.model.matchers.HeaderMatcher in project ats-framework by Axway.

the class ActionResponseObject method resolveHttpHeaders.

@Override
protected void resolveHttpHeaders(Node actionResponse) throws XmlReaderException, InvalidMatcherException {
    Node[] expectedHeaders = xmlUtilities.getSignificantResponseHeaders(actionResponse);
    for (int iExpected = 0; iExpected < expectedHeaders.length; iExpected++) {
        String headerName = xmlUtilities.getNodeAttribute(expectedHeaders[iExpected], TOKEN_HEADER_NAME_ATTRIBUTE);
        String headerValue = xmlUtilities.getNodeAttribute(expectedHeaders[iExpected], TOKEN_HEADER_VALUE_ATTRIBUTE);
        TemplateHeaderMatchMode matchMode;
        if (headerValue.contains("${=")) {
            matchMode = TemplateHeaderMatchMode.EXTRACT;
        } else if (headerValue.contains("${")) {
            if (headerValue.startsWith("${CONTAINS=")) {
                headerValue = headerValue.substring("${CONTAINS=".length(), headerValue.length() - 1);
                matchMode = TemplateHeaderMatchMode.CONTAINS;
            } else if (headerValue.startsWith("${RANGE=")) {
                headerValue = headerValue.substring("${RANGE=".length(), headerValue.length() - 1);
                matchMode = TemplateHeaderMatchMode.RANGE;
            } else if (headerValue.startsWith("${LIST=")) {
                headerValue = headerValue.substring("${LIST=".length(), headerValue.length() - 1);
                matchMode = TemplateHeaderMatchMode.LIST;
            } else if (headerValue.startsWith("${REGEX=")) {
                headerValue = headerValue.substring("${REGEX=".length(), headerValue.length() - 1);
                matchMode = TemplateHeaderMatchMode.REGEX;
            } else if ("${RANDOM}".equals(headerValue)) {
                matchMode = TemplateHeaderMatchMode.RANDOM;
                headerValue = null;
            } else {
                // the "Location" header can contain some user parameters like ${serverUrl}
                if (headerName.equalsIgnoreCase(HeaderMatcher.LOCATION_HEADER_NAME)) {
                    // TODO: For that header again we may have variables inside. 
                    // Generally cookies are automatically processed but if there is var. we should effectively use it 
                    // as regex RANDOM area or replace var before whole location header match
                    matchMode = TemplateHeaderMatchMode.EQUALS;
                } else {
                    throw new InvalidMatcherException(headerValue + " contains unknown matcher key");
                }
            }
        } else {
            // when nothing is specified - we match the header value as is
            matchMode = TemplateHeaderMatchMode.EQUALS;
        }
        // we need the headers in order to construct the request
        httpHeaders.add(new ActionHeader(headerName, headerValue));
        // we need the headers matcher in order to verify the response
        httpHeaderMatchers.add(new HeaderMatcher(headerName, headerValue, matchMode));
    }
}
Also used : HeaderMatcher(com.axway.ats.agent.core.templateactions.model.matchers.HeaderMatcher) Node(org.w3c.dom.Node) TemplateHeaderMatchMode(com.axway.ats.agent.core.templateactions.model.matchers.mode.TemplateHeaderMatchMode) InvalidMatcherException(com.axway.ats.agent.core.templateactions.exceptions.InvalidMatcherException)

Example 8 with HeaderMatcher

use of com.axway.ats.agent.core.templateactions.model.matchers.HeaderMatcher in project ats-framework by Axway.

the class XmlUtilities method verifyResponseHeaders.

/**
     * Headers are read in the following way(the next overwrites the previous):
     *  - headers from XML
     *  - globally disabled headers
     *  - headers from test case
     *
     * @param actionName
     * @param stepIndex
     * @param expectedHeaderMatchers
     * @param actualHttpResponseNode
     * @throws XmlUtilitiesException
     * @throws InvalidMatcherException
     */
private void verifyResponseHeaders(String actionName, int stepIndex, List<HeaderMatcher> expectedHeaderMatchers, ActionParser actualHttpResponse, TemplateActionsResponseVerificationConfigurator verificationConfigurator) throws XmlUtilitiesException, InvalidMatcherException {
    Node[] actualHeaderNodes = getChildrenNodes(actualHttpResponse.getActionNodeWithoutBody(), TOKEN_HTTP_HEADER);
    // Collect all header matchers from the XML file and the test code.
    // We keep them in a map, so if same header is specified in the XML and the test, the one coming
    // from the test will get precedence
    Map<String, HeaderMatcher> headerMatchersMap = new HashMap<String, HeaderMatcher>();
    // Collect all matchers coming from the static XML file
    for (HeaderMatcher headerMatcher : expectedHeaderMatchers) {
        headerMatchersMap.put(headerMatcher.getHeaderName(), headerMatcher);
    }
    // Collect all global header matchers, this is coming from the test case
    for (HeaderMatcher globalHeaderMatcher : verificationConfigurator.getGlobalHeaderMatchers()) {
        // header matcher from global rule so we assume it is not significant if not already existing - forceOptionalHeaderIfNotAlreadyExisting=true
        headerMatchersMap = addHeaderMatcherToMap(headerMatchersMap, globalHeaderMatcher, true);
    }
    // Collect all matchers coming from the test case
    // We do not check if this is an important header, this way user can specify to check a header for
    // this action step even if it is classified globally as a not important header
    TemplateActionResponseVerificator responseVerificator = verificationConfigurator.getActionVerificator(actionName);
    if (responseVerificator != null) {
        List<HeaderMatcher> headerMatchers = responseVerificator.getStepHeaderMatchers(stepIndex);
        for (HeaderMatcher headerMatcher : headerMatchers) {
            // header matcher from Java test code so we assume it is significant - forceOptionalHeaderIfNotAlreadyExisting=false
            headerMatchersMap = addHeaderMatcherToMap(headerMatchersMap, headerMatcher, false);
        }
    }
    // Now try to match all available header matchers for this action step
    boolean[] processedHeaderNodes = new boolean[actualHeaderNodes.length];
    for (HeaderMatcher headerMatcher : headerMatchersMap.values()) {
        for (int i = 0; i < actualHeaderNodes.length; i++) {
            String actualHeaderName = getNodeAttribute(actualHeaderNodes[i], TOKEN_HEADER_NAME_ATTRIBUTE);
            if (actualHeaderName.equals(headerMatcher.getHeaderName())) {
                // mark the header node as processed
                processedHeaderNodes[i] = true;
                // try to match this header
                String actualHeaderValue = getNodeAttribute(actualHeaderNodes[i], TOKEN_HEADER_VALUE_ATTRIBUTE);
                if (!headerMatcher.performMatch(null, actualHeaderValue)) {
                    // header did not match
                    String causeMsg = "Did not match header value '" + actualHeaderValue + "' for " + headerMatcher.toString() + ".";
                    logActualResponse(causeMsg, actionName, stepIndex, actualHttpResponse, true);
                    throw new XmlUtilitiesException(causeMsg);
                }
                break;
            }
        }
    }
    // this means some expected header was not received
    for (HeaderMatcher headerMatcher : headerMatchersMap.values()) {
        if (!headerMatcher.wasProcessed() && !headerMatcher.isOptionalHeader()) {
            String causeMsg = "Did not receive the expected header for " + headerMatcher.toString() + ".";
            logActualResponse(causeMsg, actionName, stepIndex, actualHttpResponse, true);
            throw new XmlUtilitiesException(causeMsg);
        }
    }
}
Also used : HeaderMatcher(com.axway.ats.agent.core.templateactions.model.matchers.HeaderMatcher) HashMap(java.util.HashMap) XmlUtilitiesException(com.axway.ats.agent.core.templateactions.exceptions.XmlUtilitiesException) Node(org.w3c.dom.Node)

Aggregations

HeaderMatcher (com.axway.ats.agent.core.templateactions.model.matchers.HeaderMatcher)8 Node (org.w3c.dom.Node)4 HashMap (java.util.HashMap)3 TemplateActionsBaseTest (com.axway.ats.agent.core.templateactions.TemplateActionsBaseTest)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Test (org.junit.Test)2 TemplateActionsResponseVerificationConfigurator (com.axway.ats.agent.core.configuration.TemplateActionsResponseVerificationConfigurator)1 InvalidMatcherException (com.axway.ats.agent.core.templateactions.exceptions.InvalidMatcherException)1 XmlUtilitiesException (com.axway.ats.agent.core.templateactions.exceptions.XmlUtilitiesException)1 TemplateHeaderMatchMode (com.axway.ats.agent.core.templateactions.model.matchers.mode.TemplateHeaderMatchMode)1 ActionParser (com.axway.ats.agent.core.templateactions.model.objects.ActionParser)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Document (org.w3c.dom.Document)1