use of org.mockserver.serialization.HttpRequestSerializer in project mockserver by mock-server.
the class AbstractExtendedMockingIntegrationTest method shouldRetrieveRecordedRequestsAsJsonWithJsonBody.
@Test
public void shouldRetrieveRecordedRequestsAsJsonWithJsonBody() {
// when
mockServerClient.when(request()).respond(response().withBody("some_body"));
assertEquals(response("some_body"), makeRequest(request().withBody("{\"digests\": [ ]}"), HEADERS_TO_IGNORE));
assertEquals(response("some_body"), makeRequest(request().withBody("{\"digests\": [\"sha256:one\"]}"), HEADERS_TO_IGNORE));
assertEquals(response("some_body"), makeRequest(request().withBody("{\"digests\": [ ]}"), HEADERS_TO_IGNORE));
assertEquals(response("some_body"), makeRequest(request().withBody("{\"digests\": [\"sha256:two\"]}"), HEADERS_TO_IGNORE));
// then
verifyRequestsMatches(new HttpRequestSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedRequests(request().withBody("{\"digests\": [ ]}"), Format.JSON)), request().withBody("{\"digests\": [ ]}"), request().withBody("{\"digests\": [ ]}"));
verifyRequestsMatches(new HttpRequestSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedRequests(request().withBody(json("{\"digests\": [ ]}")), Format.JSON)), request().withBody("{\"digests\": [ ]}"), request().withBody("{\"digests\": [\"sha256:one\"]}"), request().withBody("{\"digests\": [ ]}"), request().withBody("{\"digests\": [\"sha256:two\"]}"));
verifyRequestsMatches(new HttpRequestSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedRequests(request().withBody("{\"digests\": [\"sha256:one\"]}"), Format.JSON)), request().withBody("{\"digests\": [\"sha256:one\"]}"));
verifyRequestsMatches(new HttpRequestSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedRequests(request().withBody(json("{\"digests\": [\"sha256:one\"]}")), Format.JSON)), request().withBody("{\"digests\": [\"sha256:one\"]}"));
verifyRequestsMatches(new HttpRequestSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedRequests(request().withBody("{\"digests\": [\"sha256:two\"]}"), Format.JSON)), request().withBody("{\"digests\": [\"sha256:two\"]}"));
verifyRequestsMatches(new HttpRequestSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedRequests(request().withBody(json("{\"digests\": [\"sha256:two\"]}")), Format.JSON)), request().withBody("{\"digests\": [\"sha256:two\"]}"));
verifyRequestsMatches(new HttpRequestSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedRequests(request().withBody(json("{ }")), Format.JSON)), request().withBody("{\"digests\": [ ]}"), request().withBody("{\"digests\": [\"sha256:one\"]}"), request().withBody("{\"digests\": [ ]}"), request().withBody("{\"digests\": [\"sha256:two\"]}"));
verifyRequestsMatches(new HttpRequestSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedRequests(request(), Format.JSON)), request().withBody("{\"digests\": [ ]}"), request().withBody("{\"digests\": [\"sha256:one\"]}"), request().withBody("{\"digests\": [ ]}"), request().withBody("{\"digests\": [\"sha256:two\"]}"));
}
use of org.mockserver.serialization.HttpRequestSerializer in project mockserver by mock-server.
the class DashboardWebSocketHandler method registerListeners.
@VisibleForTesting
protected DashboardWebSocketHandler registerListeners() {
if (objectWriter == null) {
objectMapper = ObjectMapperFactory.createObjectMapper(new DashboardLogEntryDTOSerializer(), new DashboardLogEntryDTOGroupSerializer(), new DescriptionSerializer(), new ThrowableSerializer());
if (prettyPrint) {
objectWriter = objectMapper.writerWithDefaultPrettyPrinter();
} else {
objectWriter = objectMapper.writer();
}
}
if (httpRequestSerializer == null) {
httpRequestSerializer = new HttpRequestSerializer(mockServerLogger);
}
if (semaphore == null) {
semaphore = new Semaphore(1);
}
if (throttleExecutorService == null) {
throttleExecutorService = Executors.newScheduledThreadPool(1);
}
if (scheduler == null) {
scheduler = new ThreadPoolExecutor(1, 1, 0L, SECONDS, new LinkedBlockingQueue<>(10), Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy());
}
throttleExecutorService.scheduleAtFixedRate(() -> {
if (semaphore.availablePermits() == 0) {
semaphore.release(1);
}
}, 0, 1, SECONDS);
if (mockServerEventLog == null) {
mockServerEventLog = httpState.getMockServerLog();
mockServerEventLog.registerListener(this);
requestMatchers = httpState.getRequestMatchers();
requestMatchers.registerListener(this);
scheduler.submit(() -> {
try {
MILLISECONDS.sleep(100);
} catch (InterruptedException ignore) {
}
// ensure any exception added during initialisation are caught
updated(mockServerEventLog);
updated(requestMatchers, null);
});
}
return this;
}
use of org.mockserver.serialization.HttpRequestSerializer in project mockserver by mock-server.
the class MockServerClientIntegrationTest method shouldRetrieveRequestsAsJson.
@Test
public void shouldRetrieveRequestsAsJson() {
// given
String serializedRequests = new HttpRequestSerializer(MOCK_SERVER_LOGGER).serialize(Arrays.asList(request("/some_request_one"), request("/some_request_two")));
echoServerOne.withNextResponse(response().withStatusCode(201).withContentType(APPLICATION_JSON).withBody(new StringBody(serializedRequests)));
// when
String recordedResponse = mockServerClientOne.retrieveRecordedRequests(request().withPath("/some_path").withBody(new StringBody("some_request_body")), Format.JSON);
// then
assertThat(recordedResponse, is(serializedRequests));
assertThat(retrieveRequests(request()).size(), is(1));
String result = verify(verification().withRequest(request().withMethod("PUT").withPath("/mockserver/retrieve").withQueryStringParameter("type", RetrieveType.REQUESTS.name()).withQueryStringParameter("format", Format.JSON.name()).withHeaders(new Header("host", "localhost:" + echoServerOne.getPort()), new Header("accept-encoding", "gzip,deflate"), new Header("connection", "keep-alive"), new Header("content-type", "application/json; charset=utf-8")).withSecure(false).withKeepAlive(true).withBody(new StringBody("{" + NEW_LINE + " \"path\" : \"/some_path\"," + NEW_LINE + " \"body\" : \"some_request_body\"" + NEW_LINE + "}"))));
if (result != null && !result.isEmpty()) {
throw new AssertionError(result);
}
}
use of org.mockserver.serialization.HttpRequestSerializer in project mockserver by mock-server.
the class ExpectationFileSystemPersistenceIntegrationTest method shouldPersistExpectationsToJsonOnRemove.
@Test
public void shouldPersistExpectationsToJsonOnRemove() throws Exception {
// given
String persistedExpectationsPath = ConfigurationProperties.persistedExpectationsPath();
ConfigurationProperties.persistExpectations(true);
MockServer mockServer = null;
try {
File persistedExpectations = File.createTempFile("persistedExpectations", "json");
ConfigurationProperties.persistedExpectationsPath(persistedExpectations.getAbsolutePath());
// when
mockServer = new MockServer();
Expectation[] expectations = { new Expectation(request().withPath("/simpleFirst")).withId("one").thenRespond(response().withBody("some first response")), new Expectation(request().withPath("/simpleSecond")).withId("two").thenRespond(response().withBody("some second response")), new Expectation(request().withPath("/simpleThird")).withId("three").thenRespond(response().withBody("some third response")) };
httpClient.sendRequest(request().withMethod("PUT").withHeader(HOST.toString(), "localhost:" + mockServer.getLocalPort()).withPath("/mockserver/expectation").withBody(new ExpectationSerializer(mockServerLogger).serialize(expectations))).get(10, TimeUnit.SECONDS);
httpClient.sendRequest(request().withMethod("PUT").withHeader(HOST.toString(), "localhost:" + mockServer.getLocalPort()).withPath("/mockserver/clear").withBody(new HttpRequestSerializer(mockServerLogger).serialize(request().withPath("/simpleSecond")))).get(10, TimeUnit.SECONDS);
MILLISECONDS.sleep(2500);
// then
String expectedFileContents = "[ {" + NEW_LINE + " \"httpRequest\" : {" + NEW_LINE + " \"path\" : \"/simpleFirst\"" + NEW_LINE + " }," + NEW_LINE + " \"httpResponse\" : {" + NEW_LINE + " \"body\" : \"some first response\"" + NEW_LINE + " }," + NEW_LINE + " \"id\" : \"one\"," + NEW_LINE + " \"priority\" : 0," + NEW_LINE + " \"timeToLive\" : {" + NEW_LINE + " \"unlimited\" : true" + NEW_LINE + " }," + NEW_LINE + " \"times\" : {" + NEW_LINE + " \"unlimited\" : true" + NEW_LINE + " }" + NEW_LINE + "}, {" + NEW_LINE + " \"httpRequest\" : {" + NEW_LINE + " \"path\" : \"/simpleThird\"" + NEW_LINE + " }," + NEW_LINE + " \"httpResponse\" : {" + NEW_LINE + " \"body\" : \"some third response\"" + NEW_LINE + " }," + NEW_LINE + " \"id\" : \"three\"," + NEW_LINE + " \"priority\" : 0," + NEW_LINE + " \"timeToLive\" : {" + NEW_LINE + " \"unlimited\" : true" + NEW_LINE + " }," + NEW_LINE + " \"times\" : {" + NEW_LINE + " \"unlimited\" : true" + NEW_LINE + " }" + NEW_LINE + "} ]";
assertThat(persistedExpectations.getAbsolutePath() + " does not match expected content", new String(Files.readAllBytes(persistedExpectations.toPath()), StandardCharsets.UTF_8), is(expectedFileContents));
} finally {
ConfigurationProperties.persistedExpectationsPath(persistedExpectationsPath);
ConfigurationProperties.persistExpectations(false);
stopQuietly(mockServer);
}
}
use of org.mockserver.serialization.HttpRequestSerializer in project mockserver by mock-server.
the class AbstractExtendedMockingIntegrationTest method shouldRetrieveRecordedRequestsAsJson.
@Test
public void shouldRetrieveRecordedRequestsAsJson() {
// when
mockServerClient.when(request().withPath(calculatePath("some_path.*")), exactly(4)).respond(response().withBody("some_body"));
assertEquals(response("some_body"), makeRequest(request().withPath(calculatePath("some_path_one")), HEADERS_TO_IGNORE));
assertEquals(notFoundResponse(), makeRequest(request().withPath(calculatePath("not_found")), HEADERS_TO_IGNORE));
assertEquals(response("some_body"), makeRequest(request().withPath(calculatePath("some_path_three")), HEADERS_TO_IGNORE));
// then
verifyRequestsMatches(new HttpRequestSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedRequests(request().withPath(calculatePath("some_path.*")), Format.JSON)), request(calculatePath("some_path_one")), request(calculatePath("some_path_three")));
verifyRequestsMatches(new HttpRequestSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedRequests(request(), Format.JSON)), request(calculatePath("some_path_one")), request(calculatePath("not_found")), request(calculatePath("some_path_three")));
verifyRequestsMatches(new HttpRequestSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedRequests(null, Format.JSON)), request(calculatePath("some_path_one")), request(calculatePath("not_found")), request(calculatePath("some_path_three")));
}
Aggregations