Search in sources :

Example 1 with MockServerEventLog

use of org.mockserver.log.MockServerEventLog in project mockserver by mock-server.

the class HttpStateTest method shouldVerifySequenceWithCallback.

@Test
public void shouldVerifySequenceWithCallback() throws Exception {
    // given
    MockServerEventLog mockServerEventLog = httpState.getMockServerLog();
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("one")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("two")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("three")).setType(RECEIVED_REQUEST));
    CompletableFuture<String> verificationResult = new CompletableFuture<>();
    // when
    httpState.verify(new VerificationSequence().withRequests(request("one"), request("three")), verificationResult::complete);
    // then
    assertThat(verificationResult.get(5, SECONDS), is(""));
}
Also used : MockServerEventLog(org.mockserver.log.MockServerEventLog) CompletableFuture(java.util.concurrent.CompletableFuture) VerificationSequence(org.mockserver.verify.VerificationSequence) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 2 with MockServerEventLog

use of org.mockserver.log.MockServerEventLog in project mockserver by mock-server.

the class HttpStateTest method shouldVerifyWithFuture.

@Test
public void shouldVerifyWithFuture() throws Exception {
    // given
    MockServerEventLog mockServerEventLog = httpState.getMockServerLog();
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("one")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("two")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("three")).setType(RECEIVED_REQUEST));
    // when
    String result = httpState.verify(new Verification().withRequest(request("two"))).get(5, SECONDS);
    // then
    assertThat(result, is(""));
}
Also used : MockServerEventLog(org.mockserver.log.MockServerEventLog) Verification(org.mockserver.verify.Verification) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 3 with MockServerEventLog

use of org.mockserver.log.MockServerEventLog in project mockserver by mock-server.

the class HttpStateTest method shouldVerifyFailureWithCallback.

@Test
public void shouldVerifyFailureWithCallback() throws Exception {
    // given
    MockServerEventLog mockServerEventLog = httpState.getMockServerLog();
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("one")).setType(RECEIVED_REQUEST));
    CompletableFuture<String> verificationResult = new CompletableFuture<>();
    // when
    httpState.verify(new Verification().withRequest(request("two")), verificationResult::complete);
    // then
    assertThat(verificationResult.get(5, SECONDS), is("Request not found at least once, expected:<{" + NEW_LINE + "  \"path\" : \"two\"" + NEW_LINE + "}> but was:<{" + NEW_LINE + "  \"path\" : \"one\"" + NEW_LINE + "}>"));
}
Also used : MockServerEventLog(org.mockserver.log.MockServerEventLog) CompletableFuture(java.util.concurrent.CompletableFuture) Verification(org.mockserver.verify.Verification) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 4 with MockServerEventLog

use of org.mockserver.log.MockServerEventLog in project mockserver by mock-server.

the class HttpStateTest method shouldHandleVerifyRequest.

@Test
public void shouldHandleVerifyRequest() {
    // given
    MockServerEventLog mockServerEventLog = httpState.getMockServerLog();
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("one")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("two")).setType(RECEIVED_REQUEST));
    mockServerEventLog.add(new LogEntry().setHttpRequest(request("three")).setType(RECEIVED_REQUEST));
    HttpRequest expectationRetrieveExpectationsRequest = request("/mockserver/verify").withMethod("PUT").withBody(verificationSerializer.serialize(new Verification().withRequest(request("two"))));
    FakeResponseWriter responseWriter = new FakeResponseWriter();
    // when
    boolean handle = httpState.handle(expectationRetrieveExpectationsRequest, responseWriter, false);
    // then
    assertThat(handle, is(true));
    assertThat(responseWriter.response.getStatusCode(), is(202));
    assertThat(responseWriter.response.getBodyAsString(), is(""));
}
Also used : MockServerEventLog(org.mockserver.log.MockServerEventLog) HttpRequest(org.mockserver.model.HttpRequest) Verification(org.mockserver.verify.Verification) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 5 with MockServerEventLog

use of org.mockserver.log.MockServerEventLog 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)

Aggregations

MockServerEventLog (org.mockserver.log.MockServerEventLog)11 LogEntry (org.mockserver.log.model.LogEntry)11 Test (org.junit.Test)10 Verification (org.mockserver.verify.Verification)5 VerificationSequence (org.mockserver.verify.VerificationSequence)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 HttpRequest (org.mockserver.model.HttpRequest)4 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 MockServerLogger (org.mockserver.logging.MockServerLogger)1 HttpState (org.mockserver.mock.HttpState)1 RequestMatchers (org.mockserver.mock.RequestMatchers)1 Scheduler (org.mockserver.scheduler.Scheduler)1