com.thoughtworks.webstub.stub.matcher.HeadersMatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of web-stub Show documentation
Show all versions of web-stub Show documentation
Library for stubbing external HTTP dependencies
package com.thoughtworks.webstub.stub.matcher;
import com.thoughtworks.webstub.config.HttpConfiguration;
import com.thoughtworks.webstub.config.Header;
import com.thoughtworks.webstub.utils.Predicate;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Collection;
import static com.thoughtworks.webstub.utils.CollectionUtils.exists;
import static javax.servlet.http.HttpServletResponse.SC_PRECONDITION_FAILED;
public class HeadersMatcher extends RequestPartMatcher {
public HeadersMatcher(HttpServletRequest request) {
super(request, SC_PRECONDITION_FAILED);
}
@Override
public boolean matches(HttpConfiguration configuration) throws IOException {
return !exists(getHeaders(configuration), new Predicate() {
@Override
public boolean satisfies(Header configuredHeader) {
return headerAbsentFromRequest(configuredHeader, request);
}
});
}
private Collection getHeaders(HttpConfiguration configuration) {
return configuration.request().headers();
}
private boolean headerAbsentFromRequest(Header configuredHeader, HttpServletRequest request) {
String found = request.getHeader(configuredHeader.name());
return (found == null) || !found.equals(configuredHeader.value()) ;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy