use of org.mockserver.matchers.HttpRequestMatcher in project mockserver by mock-server.
the class RequestMatchers method add.
public Expectation add(Expectation expectation, Cause cause) {
Expectation upsertedExpectation = null;
if (expectation != null) {
expectationRequestDefinitions.put(expectation.getId(), expectation.getHttpRequest());
upsertedExpectation = httpRequestMatchers.getByKey(expectation.getId()).map(httpRequestMatcher -> {
if (httpRequestMatcher.getExpectation() != null && httpRequestMatcher.getExpectation().getAction() != null) {
metrics.decrement(httpRequestMatcher.getExpectation().getAction().getType());
}
if (httpRequestMatcher.getExpectation() != null) {
// propagate created time from previous entry to avoid re-ordering on update
expectation.withCreated(httpRequestMatcher.getExpectation().getCreated());
}
httpRequestMatchers.removePriorityKey(httpRequestMatcher);
if (httpRequestMatcher.update(expectation)) {
httpRequestMatchers.addPriorityKey(httpRequestMatcher);
if (MockServerLogger.isEnabled(Level.INFO)) {
mockServerLogger.logEvent(new LogEntry().setType(UPDATED_EXPECTATION).setLogLevel(Level.INFO).setHttpRequest(expectation.getHttpRequest()).setMessageFormat(UPDATED_EXPECTATION_MESSAGE_FORMAT).setArguments(expectation.clone(), expectation.getId()));
}
if (expectation.getAction() != null) {
metrics.increment(expectation.getAction().getType());
}
} else {
httpRequestMatchers.addPriorityKey(httpRequestMatcher);
}
return httpRequestMatcher;
}).orElseGet(() -> addPrioritisedExpectation(expectation, cause)).getExpectation();
notifyListeners(this, cause);
}
return upsertedExpectation;
}
use of org.mockserver.matchers.HttpRequestMatcher in project mockserver by mock-server.
the class RequestMatchers method retrieveActiveExpectations.
public List<Expectation> retrieveActiveExpectations(RequestDefinition requestDefinition) {
if (requestDefinition == null) {
return httpRequestMatchers.stream().map(HttpRequestMatcher::getExpectation).collect(Collectors.toList());
} else {
List<Expectation> expectations = new ArrayList<>();
HttpRequestMatcher requestMatcher = matcherBuilder.transformsToMatcher(requestDefinition);
getHttpRequestMatchersCopy().forEach(httpRequestMatcher -> {
if (requestMatcher.matches(httpRequestMatcher.getExpectation().getHttpRequest())) {
expectations.add(httpRequestMatcher.getExpectation());
}
});
return expectations;
}
}
use of org.mockserver.matchers.HttpRequestMatcher in project mockserver by mock-server.
the class RequestMatchers method addPrioritisedExpectation.
private HttpRequestMatcher addPrioritisedExpectation(Expectation expectation, Cause cause) {
HttpRequestMatcher httpRequestMatcher = matcherBuilder.transformsToMatcher(expectation);
httpRequestMatchers.add(httpRequestMatcher);
httpRequestMatcher.withSource(cause);
if (expectation.getAction() != null) {
metrics.increment(expectation.getAction().getType());
}
if (MockServerLogger.isEnabled(Level.INFO)) {
mockServerLogger.logEvent(new LogEntry().setType(CREATED_EXPECTATION).setLogLevel(Level.INFO).setHttpRequest(expectation.getHttpRequest()).setMessageFormat(CREATED_EXPECTATION_MESSAGE_FORMAT).setArguments(expectation.clone(), expectation.getId()));
}
return httpRequestMatcher;
}
use of org.mockserver.matchers.HttpRequestMatcher in project mockserver by mock-server.
the class RequestMatchers method clear.
public void clear(RequestDefinition requestDefinition) {
if (requestDefinition != null) {
HttpRequestMatcher clearHttpRequestMatcher = matcherBuilder.transformsToMatcher(requestDefinition);
getHttpRequestMatchersCopy().forEach(httpRequestMatcher -> {
RequestDefinition request = httpRequestMatcher.getExpectation().getHttpRequest();
if (isNotBlank(requestDefinition.getLogCorrelationId())) {
request = request.shallowClone().withLogCorrelationId(requestDefinition.getLogCorrelationId());
}
if (clearHttpRequestMatcher.matches(request)) {
removeHttpRequestMatcher(httpRequestMatcher, requestDefinition.getLogCorrelationId());
}
});
if (MockServerLogger.isEnabled(Level.INFO)) {
mockServerLogger.logEvent(new LogEntry().setType(CLEARED).setLogLevel(Level.INFO).setCorrelationId(requestDefinition.getLogCorrelationId()).setHttpRequest(requestDefinition).setMessageFormat("cleared expectations that match:{}").setArguments(requestDefinition));
}
} else {
reset();
}
}
use of org.mockserver.matchers.HttpRequestMatcher in project mockserver by mock-server.
the class RequestMatchers method retrieveRequestMatchers.
public List<HttpRequestMatcher> retrieveRequestMatchers(RequestDefinition requestDefinition) {
if (requestDefinition == null) {
return httpRequestMatchers.stream().collect(Collectors.toList());
} else {
List<HttpRequestMatcher> httpRequestMatchers = new ArrayList<>();
HttpRequestMatcher requestMatcher = matcherBuilder.transformsToMatcher(requestDefinition);
getHttpRequestMatchersCopy().forEach(httpRequestMatcher -> {
if (requestMatcher.matches(httpRequestMatcher.getExpectation().getHttpRequest())) {
httpRequestMatchers.add(httpRequestMatcher);
}
});
return httpRequestMatchers;
}
}
Aggregations