Search in sources :

Example 1 with RequestMatchers

use of org.mockserver.mock.RequestMatchers in project mockserver by mock-server.

the class ExpectationFileSystemPersistenceTest method createMockServerMatcher.

@Before
public void createMockServerMatcher() {
    mockServerLogger = new MockServerLogger();
    requestMatchers = new RequestMatchers(configuration(), mockServerLogger, new Scheduler(configuration(), mockServerLogger), new WebSocketClientRegistry(configuration(), mockServerLogger));
}
Also used : MockServerLogger(org.mockserver.logging.MockServerLogger) RequestMatchers(org.mockserver.mock.RequestMatchers) Scheduler(org.mockserver.scheduler.Scheduler) WebSocketClientRegistry(org.mockserver.closurecallback.websocketregistry.WebSocketClientRegistry) Before(org.junit.Before)

Example 2 with RequestMatchers

use of org.mockserver.mock.RequestMatchers in project mockserver by mock-server.

the class DashboardWebSocketHandlerTest method shouldRenderFilteredLogEntriesCorrectly.

private void shouldRenderFilteredLogEntriesCorrectly(boolean contains, RequestDefinition requestFilter, List<LogEntry> logEntries, List<Expectation> expectations, String... renderListSections) throws InterruptedException {
    // given
    MockServerLogger mockServerLogger = new MockServerLogger(DashboardWebSocketHandlerTest.class);
    Scheduler scheduler = new Scheduler(configuration(), mockServerLogger, true);
    HttpState httpState = new HttpState(configuration(), mockServerLogger, scheduler);
    new Scheduler.SchedulerThreadFactory("MockServer Test " + this.getClass().getSimpleName()).newThread(() -> {
        MockServerEventLog mockServerEventLog = httpState.getMockServerLog();
        for (LogEntry logEntry : logEntries) {
            mockServerEventLog.add(logEntry);
        }
        RequestMatchers requestMatchers = httpState.getRequestMatchers();
        if (!expectations.isEmpty()) {
            requestMatchers.update(expectations.toArray(new Expectation[0]), MockServerMatcherNotifier.Cause.API);
        }
    }).start();
    SECONDS.sleep(1);
    DashboardWebSocketHandler handler = new DashboardWebSocketHandler(httpState, false, true).registerListeners();
    MockChannelHandlerContext mockChannelHandlerContext = new MockChannelHandlerContext();
    handler.getClientRegistry().put(mockChannelHandlerContext, request());
    // when
    handler.sendUpdate(mockChannelHandlerContext, requestFilter);
    SECONDS.sleep(1);
    // then
    TextWebSocketFrame textWebSocketFrame = mockChannelHandlerContext.textWebSocketFrame;
    for (String renderListSection : renderListSections) {
        assertThat(textWebSocketFrame.text(), contains ? containsString(renderListSection) : is(renderListSection));
    }
}
Also used : MockServerEventLog(org.mockserver.log.MockServerEventLog) MockServerLogger(org.mockserver.logging.MockServerLogger) Scheduler(org.mockserver.scheduler.Scheduler) RequestMatchers(org.mockserver.mock.RequestMatchers) HttpState(org.mockserver.mock.HttpState) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LogEntry(org.mockserver.log.model.LogEntry)

Example 3 with RequestMatchers

use of org.mockserver.mock.RequestMatchers in project mockserver by mock-server.

the class DashboardWebSocketHandler method sendUpdate.

private void sendUpdate(ChannelOutboundInvoker ctx, RequestDefinition httpRequest, int retryCount) {
    DescriptionProcessor activeExpectationsDescriptionProcessor = new DescriptionProcessor();
    DescriptionProcessor logMessagesDescriptionProcessor = new DescriptionProcessor();
    DescriptionProcessor recordedRequestsDescriptionProcessor = new DescriptionProcessor();
    DescriptionProcessor proxiedRequestsDescriptionProcessor = new DescriptionProcessor();
    mockServerEventLog.retrieveLogEntriesInReverseForUI(httpRequest, logEntry -> true, DashboardLogEntryDTO::new, reverseLogEventsStream -> {
        List<ImmutableMap<String, Object>> activeExpectations = requestMatchers.retrieveRequestMatchers(httpRequest).stream().limit(UI_UPDATE_ITEM_LIMIT).map(requestMatcher -> {
            JsonNode expectationJsonNode = objectMapper.valueToTree(new ExpectationDTO(requestMatcher.getExpectation()));
            if (requestMatcher.getExpectation().getHttpRequest() instanceof OpenAPIDefinition) {
                JsonNode httpRequestJsonNode = expectationJsonNode.get("httpRequest");
                if (httpRequestJsonNode instanceof ObjectNode) {
                    ((ObjectNode) httpRequestJsonNode).set("requestMatchers", objectMapper.valueToTree(requestMatcher.getHttpRequests()));
                }
            }
            Description description = activeExpectationsDescriptionProcessor.description(requestMatcher.getExpectation().getHttpRequest(), requestMatcher.getExpectation().getId());
            return ImmutableMap.of("key", requestMatcher.getExpectation().getId(), "description", description != null ? description : requestMatcher.getExpectation().getId(), "value", expectationJsonNode);
        }).collect(Collectors.toList());
        List<Map<String, Object>> proxiedRequests = new LinkedList<>();
        List<Map<String, Object>> recordedRequests = new LinkedList<>();
        List<Object> logMessages = new LinkedList<>();
        Map<String, DashboardLogEntryDTOGroup> logEntryGroups = new HashMap<>();
        reverseLogEventsStream.forEach(logEntryDTO -> {
            if (logEntryDTO != null) {
                if (logMessages.size() < UI_UPDATE_ITEM_LIMIT) {
                    DashboardLogEntryDTO dashboardLogEntryDTO = logEntryDTO.setDescription(logMessagesDescriptionProcessor.description(logEntryDTO));
                    if (isNotBlank(logEntryDTO.getCorrelationId()) && logEntryDTO.getType() != TRACE) {
                        DashboardLogEntryDTOGroup logEntryGroup = logEntryGroups.get(logEntryDTO.getCorrelationId());
                        if (logEntryGroup == null) {
                            logEntryGroup = new DashboardLogEntryDTOGroup(logMessagesDescriptionProcessor);
                            logEntryGroups.put(logEntryDTO.getCorrelationId(), logEntryGroup);
                            logMessages.add(logEntryGroup);
                        }
                        logEntryGroup.getLogEntryDTOS().add(dashboardLogEntryDTO);
                    } else {
                        logMessages.add(dashboardLogEntryDTO);
                    }
                }
                if (recordedRequestsPredicate.test(logEntryDTO) && recordedRequests.size() < UI_UPDATE_ITEM_LIMIT) {
                    for (RequestDefinition request : logEntryDTO.getHttpRequests()) {
                        if (request != null) {
                            Map<String, Object> entry = new HashMap<>();
                            entry.put("key", logEntryDTO.getId() + "_request");
                            Description description = recordedRequestsDescriptionProcessor.description(request);
                            if (description != null) {
                                entry.put("description", description);
                            }
                            entry.put("value", request);
                            recordedRequests.add(entry);
                        }
                    }
                }
                if (proxiedRequestsPredicate.test(logEntryDTO) && proxiedRequests.size() < UI_UPDATE_ITEM_LIMIT) {
                    Map<String, Object> value = new HashMap<>();
                    if (logEntryDTO.getHttpRequest() != null) {
                        value.put("httpRequest", logEntryDTO.getHttpRequest());
                    }
                    if (logEntryDTO.getHttpResponse() != null) {
                        value.put("httpResponse", logEntryDTO.getHttpResponse());
                    }
                    Map<String, Object> entry = new HashMap<>();
                    entry.put("key", logEntryDTO.getId() + "_proxied");
                    Description description = proxiedRequestsDescriptionProcessor.description(logEntryDTO.getHttpRequest());
                    if (description != null) {
                        entry.put("description", description);
                    }
                    entry.put("value", value);
                    if (!value.isEmpty()) {
                        proxiedRequests.add(entry);
                    }
                }
            }
        });
        sendMessage(ctx, httpRequest, ImmutableMap.of("logMessages", logMessages, "activeExpectations", activeExpectations, "recordedRequests", recordedRequests, // reverse
        "proxiedRequests", // reverse
        proxiedRequests), retryCount);
    });
}
Also used : AttributeKey(io.netty.util.AttributeKey) ObjectMapperFactory(org.mockserver.serialization.ObjectMapperFactory) java.util(java.util) HttpRequest(org.mockserver.model.HttpRequest) HttpRequestSerializer(org.mockserver.serialization.HttpRequestSerializer) DashboardLogEntryDTO(org.mockserver.dashboard.model.DashboardLogEntryDTO) CircularHashMap(org.mockserver.collections.CircularHashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DashboardLogEntryDTOGroup(org.mockserver.dashboard.model.DashboardLogEntryDTOGroup) HttpRequest.request(org.mockserver.model.HttpRequest.request) HOST(com.google.common.net.HttpHeaders.HOST) Level(org.slf4j.event.Level) HttpState(org.mockserver.mock.HttpState) io.netty.channel(io.netty.channel) JsonNode(com.fasterxml.jackson.databind.JsonNode) LogMessageType(org.mockserver.log.model.LogEntry.LogMessageType) ExceptionHandling.connectionClosedException(org.mockserver.exception.ExceptionHandling.connectionClosedException) MockServerEventLog(org.mockserver.log.MockServerEventLog) OpenAPIDefinition(org.mockserver.model.OpenAPIDefinition) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ImmutableMap(com.google.common.collect.ImmutableMap) java.util.concurrent(java.util.concurrent) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MockServerMatcherNotifier(org.mockserver.mock.listeners.MockServerMatcherNotifier) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Collectors(java.util.stream.Collectors) org.mockserver.dashboard.serializers(org.mockserver.dashboard.serializers) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) io.netty.handler.codec.http.websocketx(io.netty.handler.codec.http.websocketx) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) LogEntry(org.mockserver.log.model.LogEntry) RequestMatchers(org.mockserver.mock.RequestMatchers) MockServerLogListener(org.mockserver.mock.listeners.MockServerLogListener) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MockServerLogger(org.mockserver.logging.MockServerLogger) ExpectationDTO(org.mockserver.serialization.model.ExpectationDTO) MockServerMatcherListener(org.mockserver.mock.listeners.MockServerMatcherListener) SECONDS(java.util.concurrent.TimeUnit.SECONDS) RequestDefinition(org.mockserver.model.RequestDefinition) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) CircularHashMap(org.mockserver.collections.CircularHashMap) RequestDefinition(org.mockserver.model.RequestDefinition) DashboardLogEntryDTOGroup(org.mockserver.dashboard.model.DashboardLogEntryDTOGroup) JsonNode(com.fasterxml.jackson.databind.JsonNode) ExpectationDTO(org.mockserver.serialization.model.ExpectationDTO) ImmutableMap(com.google.common.collect.ImmutableMap) DashboardLogEntryDTO(org.mockserver.dashboard.model.DashboardLogEntryDTO) OpenAPIDefinition(org.mockserver.model.OpenAPIDefinition) CircularHashMap(org.mockserver.collections.CircularHashMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with RequestMatchers

use of org.mockserver.mock.RequestMatchers in project mockserver by mock-server.

the class ExpectationFileWatcherTest method createMockServerMatcher.

@Before
public void createMockServerMatcher() {
    mockServerLogger = new MockServerLogger();
    requestMatchers = new RequestMatchers(configuration(), mockServerLogger, new Scheduler(configuration(), mockServerLogger), new WebSocketClientRegistry(configuration(), mockServerLogger));
}
Also used : MockServerLogger(org.mockserver.logging.MockServerLogger) RequestMatchers(org.mockserver.mock.RequestMatchers) Scheduler(org.mockserver.scheduler.Scheduler) WebSocketClientRegistry(org.mockserver.closurecallback.websocketregistry.WebSocketClientRegistry) Before(org.junit.Before)

Aggregations

MockServerLogger (org.mockserver.logging.MockServerLogger)4 RequestMatchers (org.mockserver.mock.RequestMatchers)4 Scheduler (org.mockserver.scheduler.Scheduler)3 Before (org.junit.Before)2 WebSocketClientRegistry (org.mockserver.closurecallback.websocketregistry.WebSocketClientRegistry)2 MockServerEventLog (org.mockserver.log.MockServerEventLog)2 LogEntry (org.mockserver.log.model.LogEntry)2 HttpState (org.mockserver.mock.HttpState)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 HOST (com.google.common.net.HttpHeaders.HOST)1 io.netty.channel (io.netty.channel)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 io.netty.handler.codec.http.websocketx (io.netty.handler.codec.http.websocketx)1