Search in sources :

Example 1 with HttpRequestMatcher

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;
}
Also used : WebSocketClientRegistry(org.mockserver.closurecallback.websocketregistry.WebSocketClientRegistry) java.util(java.util) HttpRequestMatcher(org.mockserver.matchers.HttpRequestMatcher) MatchDifference(org.mockserver.matchers.MatchDifference) UUIDService(org.mockserver.uuid.UUIDService) TRACE(org.slf4j.event.Level.TRACE) CircularPriorityQueue(org.mockserver.collections.CircularPriorityQueue) CircularHashMap(org.mockserver.collections.CircularHashMap) Name(org.mockserver.metrics.Metrics.Name) Scheduler(org.mockserver.scheduler.Scheduler) DEBUG(org.slf4j.event.Level.DEBUG) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Level(org.slf4j.event.Level) LogMessageType(org.mockserver.log.model.LogEntry.LogMessageType) NULL(org.mockserver.mock.SortableExpectationId.NULL) MatcherBuilder(org.mockserver.matchers.MatcherBuilder) MockServerMatcherNotifier(org.mockserver.mock.listeners.MockServerMatcherNotifier) Collectors(java.util.stream.Collectors) EXPECTATION_SORTABLE_PRIORITY_COMPARATOR(org.mockserver.mock.SortableExpectationId.EXPECTATION_SORTABLE_PRIORITY_COMPARATOR) Configuration(org.mockserver.configuration.Configuration) org.mockserver.model(org.mockserver.model) Stream(java.util.stream.Stream) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) LogEntry(org.mockserver.log.model.LogEntry) LogEntryMessages(org.mockserver.log.model.LogEntryMessages) MockServerLogger(org.mockserver.logging.MockServerLogger) Metrics(org.mockserver.metrics.Metrics) LogEntry(org.mockserver.log.model.LogEntry)

Example 2 with HttpRequestMatcher

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;
    }
}
Also used : HttpRequestMatcher(org.mockserver.matchers.HttpRequestMatcher)

Example 3 with HttpRequestMatcher

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;
}
Also used : HttpRequestMatcher(org.mockserver.matchers.HttpRequestMatcher) LogEntry(org.mockserver.log.model.LogEntry)

Example 4 with 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();
    }
}
Also used : HttpRequestMatcher(org.mockserver.matchers.HttpRequestMatcher) LogEntry(org.mockserver.log.model.LogEntry)

Example 5 with HttpRequestMatcher

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;
    }
}
Also used : HttpRequestMatcher(org.mockserver.matchers.HttpRequestMatcher)

Aggregations

HttpRequestMatcher (org.mockserver.matchers.HttpRequestMatcher)9 LogEntry (org.mockserver.log.model.LogEntry)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CircularHashMap (org.mockserver.collections.CircularHashMap)2 RequestDefinition (org.mockserver.model.RequestDefinition)2 java.util (java.util)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StringUtils.isBlank (org.apache.commons.lang3.StringUtils.isBlank)1 StringUtils.isNotBlank (org.apache.commons.lang3.StringUtils.isNotBlank)1 Test (org.junit.Test)1 WebSocketClientRegistry (org.mockserver.closurecallback.websocketregistry.WebSocketClientRegistry)1 CircularPriorityQueue (org.mockserver.collections.CircularPriorityQueue)1 Configuration (org.mockserver.configuration.Configuration)1 LogMessageType (org.mockserver.log.model.LogEntry.LogMessageType)1 LogEntryMessages (org.mockserver.log.model.LogEntryMessages)1 RequestAndExpectationId (org.mockserver.log.model.RequestAndExpectationId)1