org.mockserver.matchers.ParameterStringMatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockserver-core Show documentation
Show all versions of mockserver-core Show documentation
Functionality used by all MockServer modules for matching and expectations
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