Search in sources :

Example 1 with LogEntry

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

the class MockServerEventLogRequestLogEntryVerificationTest method shouldFailVerificationWithLimitedReturnedRequests.

@Test
public void shouldFailVerificationWithLimitedReturnedRequests() {
    Integer originalMaximumNumberOfRequestToReturnInVerificationFailure = ConfigurationProperties.maximumNumberOfRequestToReturnInVerificationFailure();
    try {
        // given
        HttpRequest httpRequest = new HttpRequest().withPath("some_path");
        HttpRequest otherHttpRequest = new HttpRequest().withPath("some_other_path");
        ConfigurationProperties.maximumNumberOfRequestToReturnInVerificationFailure(1);
        // when
        mockServerEventLog.add(new LogEntry().setHttpRequest(httpRequest).setType(RECEIVED_REQUEST));
        mockServerEventLog.add(new LogEntry().setHttpRequest(otherHttpRequest).setType(RECEIVED_REQUEST));
        mockServerEventLog.add(new LogEntry().setHttpRequest(httpRequest).setType(RECEIVED_REQUEST));
        // then
        assertThat(verify(verification().withRequest(new HttpRequest().withPath("some_other_path")).withTimes(atLeast(2))), is("Request not found at least 2 times, expected:<{" + NEW_LINE + "  \"path\" : \"some_other_path\"" + NEW_LINE + "}> but was not found, found 3 other requests"));
    } finally {
        ConfigurationProperties.maximumNumberOfRequestToReturnInVerificationFailure(originalMaximumNumberOfRequestToReturnInVerificationFailure);
    }
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) LogEntry(org.mockserver.log.model.LogEntry) Test(org.junit.Test)

Example 2 with LogEntry

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

the class HttpActionHandlerTest method shouldHandleResponseTemplateActionException.

@Test
public void shouldHandleResponseTemplateActionException() {
    // given
    HttpTemplate template = template(HttpTemplate.TemplateType.JAVASCRIPT, "some_template").withDelay(milliseconds(1));
    expectation = new Expectation(request).thenRespond(template);
    when(mockHttpStateHandler.firstMatchingExpectation(request)).thenReturn(expectation);
    RuntimeException throwable = new RuntimeException("TEST_EXCEPTION");
    when(mockHttpResponseTemplateActionHandler.handle(any(HttpTemplate.class), any(HttpRequest.class))).thenThrow(throwable);
    // when
    actionHandler.processAction(request, mockResponseWriter, null, new HashSet<>(), false, true);
    // then
    verify(mockHttpResponseTemplateActionHandler).handle(template, request);
    verify(mockResponseWriter).writeResponse(request, notFoundResponse(), false);
    verify(mockServerLogger).logEvent(new LogEntry().setType(RECEIVED_REQUEST).setLogLevel(Level.INFO).setHttpRequest(request).setMessageFormat(RECEIVED_REQUEST_MESSAGE_FORMAT).setArguments(request));
    verify(mockServerLogger).logEvent(new LogEntry().setType(EXPECTATION_RESPONSE).setLogLevel(Level.INFO).setHttpRequest(request).setHttpResponse(notFoundResponse()).setMessageFormat("returning response:{}for request:{}for action:{}from expectation:{}").setArguments(notFoundResponse(), request, expectation.getAction(), expectation.getId()));
    verify(mockServerLogger).logEvent(new LogEntry().setType(WARN).setLogLevel(Level.INFO).setHttpRequest(request).setMessageFormat(throwable.getMessage()).setThrowable(throwable));
}
Also used : Expectation(org.mockserver.mock.Expectation) LogEntry(org.mockserver.log.model.LogEntry)

Example 3 with LogEntry

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

the class HttpActionHandlerTest method shouldHandleForwardTemplateActionException.

@Test
public void shouldHandleForwardTemplateActionException() {
    // given
    HttpTemplate template = template(HttpTemplate.TemplateType.JAVASCRIPT, "some_template");
    expectation = new Expectation(request).thenForward(template);
    when(mockHttpStateHandler.firstMatchingExpectation(request)).thenReturn(expectation);
    RuntimeException throwable = new RuntimeException("TEST_EXCEPTION");
    when(mockHttpForwardTemplateActionHandler.handle(any(HttpTemplate.class), any(HttpRequest.class))).thenThrow(throwable);
    // when
    actionHandler.processAction(request, mockResponseWriter, null, new HashSet<>(), false, true);
    // then
    verify(mockHttpForwardTemplateActionHandler).handle(template, request);
    verify(mockResponseWriter).writeResponse(request, notFoundResponse(), false);
    verify(mockServerLogger).logEvent(new LogEntry().setType(RECEIVED_REQUEST).setLogLevel(Level.INFO).setHttpRequest(request).setMessageFormat(RECEIVED_REQUEST_MESSAGE_FORMAT).setArguments(request));
    verify(mockServerLogger).logEvent(new LogEntry().setType(EXPECTATION_RESPONSE).setLogLevel(Level.INFO).setHttpRequest(request).setHttpResponse(notFoundResponse()).setMessageFormat("returning response:{}for request:{}for action:{}from expectation:{}").setArguments(notFoundResponse(), request, expectation.getAction(), expectation.getId()));
    verify(mockServerLogger).logEvent(new LogEntry().setType(WARN).setLogLevel(Level.INFO).setHttpRequest(request).setMessageFormat(throwable.getMessage()).setThrowable(throwable));
}
Also used : Expectation(org.mockserver.mock.Expectation) LogEntry(org.mockserver.log.model.LogEntry)

Example 4 with LogEntry

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

the class HttpActionHandlerTest method shouldProcessResponseAction.

@Test
public void shouldProcessResponseAction() {
    // given
    HttpResponse response = response("some_body").withDelay(milliseconds(1));
    expectation = new Expectation(request).thenRespond(response);
    when(mockHttpStateHandler.firstMatchingExpectation(request)).thenReturn(expectation);
    // when
    actionHandler.processAction(request, mockResponseWriter, null, new HashSet<>(), false, true);
    // then
    verify(mockHttpResponseActionHandler).handle(response);
    verify(mockResponseWriter).writeResponse(request, this.response, false);
    verify(mockServerLogger).logEvent(new LogEntry().setType(RECEIVED_REQUEST).setLogLevel(Level.INFO).setHttpRequest(request).setMessageFormat(RECEIVED_REQUEST_MESSAGE_FORMAT).setArguments(request));
    verify(mockServerLogger).logEvent(new LogEntry().setLogLevel(INFO).setType(EXPECTATION_RESPONSE).setHttpRequest(request).setHttpResponse(this.response).setMessageFormat("returning response:{}for request:{}for action:{}from expectation:{}").setArguments(this.response, request, response, expectation.getId()));
    verify(scheduler).schedule(any(Runnable.class), eq(true), eq(milliseconds(0)));
    verify(scheduler).schedule(any(Runnable.class), eq(true), eq(milliseconds(0)));
}
Also used : Expectation(org.mockserver.mock.Expectation) LogEntry(org.mockserver.log.model.LogEntry)

Example 5 with LogEntry

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

the class VelocityTemplateEngineTest method shouldHandleHttpRequestsWithVelocityForwardTemplateFirstExample.

@Test
public void shouldHandleHttpRequestsWithVelocityForwardTemplateFirstExample() throws JsonProcessingException {
    // given
    String template = "{" + NEW_LINE + "    'path' : \"/somePath\"," + NEW_LINE + "    'cookies' : [ {" + NEW_LINE + "        'name' : \"$!request.cookies['someCookie']\"," + NEW_LINE + "        'value' : \"someCookie\"" + NEW_LINE + "    }, {" + NEW_LINE + "        'name' : \"someCookie\"," + NEW_LINE + "        'value' : \"$!request.cookies['someCookie']\"" + NEW_LINE + "    } ]," + NEW_LINE + "    'keepAlive' : true," + NEW_LINE + "    'secure' : true," + NEW_LINE + "    'body' : \"some_body\"" + NEW_LINE + "}";
    HttpRequest request = request().withPath("/somePath").withCookie("someCookie", "someValue").withMethod("POST").withBody("some_body");
    // when
    HttpRequest actualHttpRequest = new VelocityTemplateEngine(logFormatter).executeTemplate(template, request, HttpRequestDTO.class);
    // then
    assertThat(actualHttpRequest, is(request().withPath("/somePath").withCookie("someCookie", "someValue").withCookie("someValue", "someCookie").withKeepAlive(true).withSecure(true).withBody("some_body")));
    verify(logFormatter).logEvent(new LogEntry().setType(TEMPLATE_GENERATED).setLogLevel(INFO).setHttpRequest(request).setMessageFormat("generated output:{}from template:{}for request:{}").setArguments(OBJECT_MAPPER.readTree("" + "{" + NEW_LINE + "    'path' : \"/somePath\"," + NEW_LINE + "    'cookies' : [ {" + NEW_LINE + "        'name' : \"someValue\"," + NEW_LINE + "        'value' : \"someCookie\"" + NEW_LINE + "    }, {" + NEW_LINE + "        'name' : \"someCookie\"," + NEW_LINE + "        'value' : \"someValue\"" + NEW_LINE + "    } ]," + NEW_LINE + "    'keepAlive' : true," + NEW_LINE + "    'secure' : true," + NEW_LINE + "    'body' : \"some_body\"" + NEW_LINE + "}"), template, request));
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) LogEntry(org.mockserver.log.model.LogEntry)

Aggregations

LogEntry (org.mockserver.log.model.LogEntry)269 Test (org.junit.Test)114 HttpRequest (org.mockserver.model.HttpRequest)79 HttpResponse (org.mockserver.model.HttpResponse)58 Level (org.slf4j.event.Level)34 MockServerLogger (org.mockserver.logging.MockServerLogger)32 Expectation (org.mockserver.mock.Expectation)32 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)25 VerificationSequence (org.mockserver.verify.VerificationSequence)20 DashboardLogEntryDTO (org.mockserver.dashboard.model.DashboardLogEntryDTO)17 OpenAPIExpectation.openAPIExpectation (org.mockserver.mock.OpenAPIExpectation.openAPIExpectation)16 MockServerEventLog (org.mockserver.log.MockServerEventLog)14 List (java.util.List)13 CompletableFuture (java.util.concurrent.CompletableFuture)13 IOException (java.io.IOException)12 InetSocketAddress (java.net.InetSocketAddress)12 ArrayList (java.util.ArrayList)10 Date (java.util.Date)9 ResponseWriter (org.mockserver.responsewriter.ResponseWriter)9 JsonNode (com.fasterxml.jackson.databind.JsonNode)8