com.github.restdriver.clientdriver.DefaultRequestMatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest-client-driver Show documentation
Show all versions of rest-client-driver Show documentation
Test Driver to test your RESTful services
/**
* Copyright © 2010-2011 Nokia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.restdriver.clientdriver;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import org.hamcrest.StringDescription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.restdriver.clientdriver.exception.ClientDriverInternalException;
/**
* Implementation of {@link RequestMatcher}. This implementation expects exact match in terms of the HTTP method, the
* path & query string, and any body of the request.
*/
public final class DefaultRequestMatcher implements RequestMatcher {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRequestMatcher.class);
@Override
public boolean isMatch(RealRequest realRequest, ClientDriverRequest expectedRequest) {
// TODO: Better diagnostics from this method. See https://github.com/rest-driver/rest-driver/issues/7
boolean sameMethod = isSameMethod(realRequest, expectedRequest);
if (!sameMethod) {
return false;
}
boolean sameBasePath = isSameBasePath(realRequest, expectedRequest);
if (!sameBasePath) {
return false;
}
boolean sameQueryString = hasSameQueryString(realRequest, expectedRequest);
if (!sameQueryString) {
return false;
}
boolean sameHeaders = hasSameHeaders(realRequest, expectedRequest);
if (!sameHeaders) {
return false;
}
boolean sameBody = hasSameBody(realRequest, expectedRequest);
if (!sameBody) {
return false;
}
return true;
}
private boolean isSameMethod(RealRequest realRequest, ClientDriverRequest expectedRequest) {
if (realRequest.getMethod() != expectedRequest.getMethod()) {
LOGGER.info("REJECTED on method: expected " + expectedRequest.getMethod() + " != " + realRequest.getMethod());
return false;
}
return true;
}
private boolean isSameBasePath(RealRequest realRequest, ClientDriverRequest expectedRequest) {
if (!isStringOrPatternMatch(realRequest.getPath(), expectedRequest.getPath())) {
LOGGER.info("REJECTED on path: expected " + expectedRequest.getPath() + " != " + realRequest.getPath());
return false;
}
return true;
}
private boolean hasSameQueryString(RealRequest realRequest, ClientDriverRequest expectedRequest) {
Map> actualParams = realRequest.getParams();
Map> expectedParams = expectedRequest.getParams();
if (actualParams.size() != expectedParams.size()) {
LOGGER.info("REJECTED on number of params: expected " + expectedParams.size() + " != " + actualParams.size());
return false;
}
for (String expectedKey : expectedParams.keySet()) {
Collection actualParamValues = actualParams.get(expectedKey);
if (actualParamValues == null || actualParamValues.size() == 0) {
LOGGER.info("REJECTED on missing param key: expected " + expectedKey + "=" + expectedParams.get(expectedKey));
return false;
}
Collection
© 2015 - 2024 Weber Informatics LLC | Privacy Policy