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));
}
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));
}
}
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);
});
}
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));
}
Aggregations