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

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

There is a newer version: 5.15.0
Show newest version
package org.mockserver.matchers;

import io.netty.handler.codec.http.QueryStringDecoder;
import org.mockserver.model.KeyToMultiValue;
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 implements Matcher {
    private final MapMatcher matcher;

    public ParameterStringMatcher(List parameters) {
        this.matcher = new MapMatcher(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 result;
    }

    private List parseString(String matched) {
        Map mappedParameters = new HashMap();
        Map> parameters = new QueryStringDecoder("?" + matched).parameters();
        for (String name : parameters.keySet()) {
            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