All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockserver.matchers.ParameterStringMatcher Maven / Gradle / Ivy

package org.mockserver.matchers;

import io.netty.handler.codec.http.QueryStringDecoder;
import org.mockserver.model.KeyToMultiValue;
import org.mockserver.model.NottableString;
import org.mockserver.model.Parameter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jamesdbloom
 */
public class ParameterStringMatcher extends BodyMatcher {
    private final MultiValueMapMatcher matcher;

    public ParameterStringMatcher(List parameters) {
        this.matcher = new MultiValueMapMatcher(KeyToMultiValue.toMultiMap(parameters));
    }

    public boolean matches(String matched) {
        boolean result = false;

        if (matcher.matches(parseString(matched))) {
            result = true;
        }

        if (!result) {
            logger.trace("Failed to match [{}] with [{}]", matched, this.matcher);
        }

        return reverseResultIfNot(result);
    }

    private List parseString(String matched) {
        Map mappedParameters = new HashMap();
        Map> parameters = new QueryStringDecoder("?" + matched).parameters();
        for (String name : parameters.keySet()) {
            // TODO(jamesdbloom) support nottable parameters
            for (String value : parameters.get(name)) {
                if (mappedParameters.containsKey(name)) {
                    mappedParameters.get(name).addValue(value);
                } else {
                    mappedParameters.put(name, new Parameter(name, value));
                }
            }
        }
        return new ArrayList(mappedParameters.values());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy