Skip to content

Commit 4c46bc5

Browse files
committed
#654 added java client code for retrieving requests and responses
1 parent a367f4d commit 4c46bc5

File tree

13 files changed

+506
-39
lines changed

13 files changed

+506
-39
lines changed

mockserver-client-java/src/main/java/org/mockserver/client/MockServerClient.java

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import static io.netty.handler.codec.http.HttpHeaderNames.HOST;
3030
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
31+
import static org.apache.commons.lang3.StringUtils.isNotBlank;
3132
import static org.mockserver.formatting.StringFormatter.formatLogMessage;
3233
import static org.mockserver.mock.HttpStateHandler.LOG_SEPARATOR;
3334
import static org.mockserver.model.HttpRequest.request;
@@ -51,6 +52,7 @@ public class MockServerClient implements Stoppable {
5152
private Integer port;
5253
private NettyHttpClient nettyHttpClient = new NettyHttpClient(eventLoopGroup, null);
5354
private HttpRequestSerializer httpRequestSerializer = new HttpRequestSerializer(mockServerLogger);
55+
private HttpRequestResponseSerializer httpRequestResponseSerializer = new HttpRequestResponseSerializer(mockServerLogger);
5456
private PortBindingSerializer portBindingSerializer = new PortBindingSerializer(mockServerLogger);
5557
private ExpectationSerializer expectationSerializer = new ExpectationSerializer(mockServerLogger);
5658
private VerificationSerializer verificationSerializer = new VerificationSerializer(mockServerLogger);
@@ -139,7 +141,7 @@ public String contextPath() {
139141

140142
private String calculatePath(String path) {
141143
String cleanedPath = "/mockserver/" + path;
142-
if (!Strings.isNullOrEmpty(contextPath)) {
144+
if (isNotBlank(contextPath)) {
143145
cleanedPath =
144146
(!contextPath.startsWith("/") ? "/" : "") +
145147
contextPath +
@@ -168,16 +170,14 @@ private HttpResponse sendRequest(HttpRequest request) {
168170
}
169171
String serverVersion = response.getFirstHeader("version");
170172
String clientVersion = Version.getVersion();
171-
if (!Strings.isNullOrEmpty(serverVersion) &&
172-
!Strings.isNullOrEmpty(clientVersion) &&
173-
!clientVersion.equals(serverVersion)) {
173+
if (isNotBlank(serverVersion) && isNotBlank(clientVersion) && !clientVersion.equals(serverVersion)) {
174174
throw new ClientException("Client version \"" + clientVersion + "\" does not match server version \"" + serverVersion + "\"");
175175
}
176176
}
177177

178178
return response;
179179
} catch (RuntimeException rex) {
180-
if (!Strings.isNullOrEmpty(rex.getMessage()) && (rex.getMessage().contains("executor not accepting a task") || rex.getMessage().contains("loop shut down"))) {
180+
if (isNotBlank(rex.getMessage()) && (rex.getMessage().contains("executor not accepting a task") || rex.getMessage().contains("loop shut down"))) {
181181
throw new IllegalStateException(this.getClass().getSimpleName() + " has already been closed, please create new " + this.getClass().getSimpleName() + " instance");
182182
} else {
183183
throw rex;
@@ -408,6 +408,40 @@ public String retrieveRecordedRequests(HttpRequest httpRequest, Format format) {
408408
return httpResponse.getBodyAsString();
409409
}
410410

411+
/**
412+
* Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests
413+
*
414+
* @param httpRequest the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requests
415+
* @return an array of all requests that have been recorded by the MockServer in the order they have been received and including duplicates where the same request has been received multiple times
416+
*/
417+
public HttpRequestAndHttpResponse[] retrieveRecordedRequestsAndResponses(HttpRequest httpRequest) {
418+
String recordedRequests = retrieveRecordedRequestsAndResponses(httpRequest, Format.JSON);
419+
if (StringUtils.isNotEmpty(recordedRequests) && !recordedRequests.equals("[]")) {
420+
return httpRequestResponseSerializer.deserializeArray(recordedRequests);
421+
} else {
422+
return new HttpRequestAndHttpResponse[0];
423+
}
424+
}
425+
426+
/**
427+
* Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests
428+
*
429+
* @param httpRequest the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requests
430+
* @param format the format to retrieve the expectations, either JAVA or JSON
431+
* @return an array of all requests that have been recorded by the MockServer in the order they have been received and including duplicates where the same request has been received multiple times
432+
*/
433+
public String retrieveRecordedRequestsAndResponses(HttpRequest httpRequest, Format format) {
434+
HttpResponse httpResponse = sendRequest(
435+
request()
436+
.withMethod("PUT")
437+
.withPath(calculatePath("retrieve"))
438+
.withQueryStringParameter("type", RetrieveType.REQUEST_RESPONSES.name())
439+
.withQueryStringParameter("format", format.name())
440+
.withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", StandardCharsets.UTF_8)
441+
);
442+
return httpResponse.getBodyAsString();
443+
}
444+
411445
/**
412446
* Retrieve the request-response combinations that have been recorded as a list of expectations, only those that match the httpRequest parameter are returned, use null to retrieve all requests
413447
*
@@ -416,7 +450,7 @@ public String retrieveRecordedRequests(HttpRequest httpRequest, Format format) {
416450
*/
417451
public Expectation[] retrieveRecordedExpectations(HttpRequest httpRequest) {
418452
String recordedExpectations = retrieveRecordedExpectations(httpRequest, Format.JSON);
419-
if (!Strings.isNullOrEmpty(recordedExpectations) && !recordedExpectations.equals("[]")) {
453+
if (isNotBlank(recordedExpectations) && !recordedExpectations.equals("[]")) {
420454
return expectationSerializer.deserializeArray(recordedExpectations);
421455
} else {
422456
return new Expectation[0];
@@ -585,7 +619,7 @@ public void sendExpectation(Expectation... expectations) {
585619
*/
586620
public Expectation[] retrieveActiveExpectations(HttpRequest httpRequest) {
587621
String activeExpectations = retrieveActiveExpectations(httpRequest, Format.JSON);
588-
if (!Strings.isNullOrEmpty(activeExpectations) && !activeExpectations.equals("[]")) {
622+
if (isNotBlank(activeExpectations) && !activeExpectations.equals("[]")) {
589623
return expectationSerializer.deserializeArray(activeExpectations);
590624
} else {
591625
return new Expectation[0];

0 commit comments

Comments
 (0)