Skip to content

Commit ab74cf0

Browse files
committed
major refactor to reduce duplication and simplify log in preparation for new log features
1 parent db7b563 commit ab74cf0

File tree

107 files changed

+3211
-7127
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+3211
-7127
lines changed

Diff for: mockserver-client-java/src/main/java/org/mockserver/client/AbstractClient.java

+78-41
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import org.mockserver.client.netty.NettyHttpClient;
77
import org.mockserver.client.netty.SocketConnectionException;
88
import org.mockserver.client.serialization.*;
9-
import org.mockserver.client.server.MockServerClient;
9+
import org.mockserver.mock.Expectation;
10+
import org.mockserver.mock.HttpStateHandler;
1011
import org.mockserver.model.HttpRequest;
1112
import org.mockserver.model.HttpResponse;
1213
import org.mockserver.model.HttpStatusCode;
@@ -83,16 +84,16 @@ protected String calculatePath(String path) {
8384
return (!cleanedPath.startsWith("/") ? "/" : "") + cleanedPath;
8485
}
8586

86-
protected HttpResponse sendRequest(HttpRequest httpRequest) {
87-
HttpResponse httpResponse = nettyHttpClient.sendRequest(
88-
httpRequest.withHeader(HOST.toString(), host + ":" + port)
87+
protected HttpResponse sendRequest(HttpRequest request) {
88+
HttpResponse response = nettyHttpClient.sendRequest(
89+
request.withHeader(HOST.toString(), host + ":" + port)
8990
);
90-
if (httpResponse != null &&
91-
httpResponse.getStatusCode() != null &&
92-
httpResponse.getStatusCode() == BAD_REQUEST.code()) {
93-
throw new IllegalArgumentException(httpResponse.getBodyAsString());
91+
if (response != null &&
92+
response.getStatusCode() != null &&
93+
response.getStatusCode() == BAD_REQUEST.code()) {
94+
throw new IllegalArgumentException(response.getBodyAsString());
9495
}
95-
return httpResponse;
96+
return response;
9697
}
9798

9899
protected String formatErrorMessage(String message, Object... objects) {
@@ -111,12 +112,6 @@ public String contextPath() {
111112
return contextPath;
112113
}
113114

114-
public enum TYPE {
115-
LOG,
116-
EXPECTATION,
117-
BOTH;
118-
}
119-
120115
/**
121116
* Returns the server (MockServer or Proxy) is running
122117
*/
@@ -208,25 +203,25 @@ public T clear(HttpRequest httpRequest) {
208203
* Clear expectations, logs or both that match the http
209204
*
210205
* @param httpRequest the http request that is matched against when deciding whether to clear each expectation if null all expectations are cleared
211-
* @param type the type to clear, EXPECTATION, LOG or BOTH
206+
* @param type the type to clear, EXPECTATION, LOG or BOTH
212207
*/
213-
public T clear(HttpRequest httpRequest, MockServerClient.TYPE type) {
208+
public T clear(HttpRequest httpRequest, HttpStateHandler.ClearType type) {
214209
sendRequest(request().withMethod("PUT").withPath(calculatePath("clear")).withQueryStringParameter("type", type.name().toLowerCase()).withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", Charsets.UTF_8));
215210
return clientClass.cast(this);
216211
}
217212

218213
/**
219214
* Verify a list of requests have been sent in the order specified for example:
220-
*
221-
* mockServerClient
222-
* .verify(
223-
* request()
224-
* .withPath("/first_request")
225-
* .withBody("some_request_body"),
226-
* request()
227-
* .withPath("/second_request")
228-
* .withBody("some_request_body")
229-
* );
215+
* <p>
216+
* mockServerClient
217+
* .verify(
218+
* request()
219+
* .withPath("/first_request")
220+
* .withBody("some_request_body"),
221+
* request()
222+
* .withPath("/second_request")
223+
* .withBody("some_request_body")
224+
* );
230225
*
231226
* @param httpRequests the http requests that must be matched for this verification to pass
232227
* @throws AssertionError if the request has not been found
@@ -247,23 +242,23 @@ public T verify(HttpRequest... httpRequests) throws AssertionError {
247242

248243
/**
249244
* Verify a request has been sent for example:
250-
*
251-
* mockServerClient
252-
* .verify(
253-
* request()
254-
* .withPath("/some_path")
255-
* .withBody("some_request_body"),
256-
* VerificationTimes.exactly(3)
257-
* );
258-
*
245+
* <p>
246+
* mockServerClient
247+
* .verify(
248+
* request()
249+
* .withPath("/some_path")
250+
* .withBody("some_request_body"),
251+
* VerificationTimes.exactly(3)
252+
* );
253+
* <p>
259254
* VerificationTimes supports multiple static factory methods:
260-
*
261-
* once() - verify the request was only received once
262-
* exactly(n) - verify the request was only received exactly n times
263-
* atLeast(n) - verify the request was only received at least n times
255+
* <p>
256+
* once() - verify the request was only received once
257+
* exactly(n) - verify the request was only received exactly n times
258+
* atLeast(n) - verify the request was only received at least n times
264259
*
265260
* @param httpRequest the http request that must be matched for this verification to pass
266-
* @param times the number of times this request must be matched
261+
* @param times the number of times this request must be matched
267262
* @throws AssertionError if the request has not been found
268263
*/
269264
public T verify(HttpRequest httpRequest, VerificationTimes times) throws AssertionError {
@@ -297,4 +292,46 @@ public T verifyZeroInteractions() throws AssertionError {
297292
}
298293
return clientClass.cast(this);
299294
}
295+
296+
/**
297+
* Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests
298+
*
299+
* @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
300+
* @return an array of all expectations 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
301+
*/
302+
public HttpRequest[] retrieveRecordedRequests(HttpRequest httpRequest) {
303+
HttpResponse httpResponse = sendRequest(
304+
request()
305+
.withMethod("PUT")
306+
.withPath(calculatePath("retrieve"))
307+
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.REQUESTS.name())
308+
.withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", Charsets.UTF_8)
309+
);
310+
if (StringUtils.isNotEmpty(httpResponse.getBodyAsString())) {
311+
return httpRequestSerializer.deserializeArray(httpResponse.getBodyAsString());
312+
} else {
313+
return new HttpRequest[0];
314+
}
315+
}
316+
317+
/**
318+
* Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests
319+
*
320+
* @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
321+
* @return an array of all expectations 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
322+
*/
323+
public Expectation[] retrieveRecordedExpectations(HttpRequest httpRequest) {
324+
HttpResponse httpResponse = sendRequest(
325+
request()
326+
.withMethod("PUT")
327+
.withPath(calculatePath("retrieve"))
328+
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.RECORDED_EXPECTATIONS.name())
329+
.withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", Charsets.UTF_8)
330+
);
331+
if (!joptsimple.internal.Strings.isNullOrEmpty(httpResponse.getBodyAsString())) {
332+
return expectationSerializer.deserializeArray(httpResponse.getBodyAsString());
333+
} else {
334+
return new Expectation[0];
335+
}
336+
}
300337
}

Diff for: mockserver-client-java/src/main/java/org/mockserver/client/proxy/ProxyClient.java

-59
Original file line numberDiff line numberDiff line change
@@ -50,63 +50,4 @@ public ProxyClient(String host, int port, String contextPath) {
5050
super(host, port, contextPath, ProxyClient.class);
5151
}
5252

53-
/**
54-
* Pretty-print the json for all requests / responses as Expectations to the log.
55-
* They are printed into a dedicated log called mockserver_request.log
56-
*/
57-
public ProxyClient dumpToLogAsJSON() {
58-
return dumpToLogAsJSON(null);
59-
}
60-
61-
/**
62-
* Pretty-print the json for matching requests and their responses as Expectations to the log.
63-
* They are printed into a dedicated log called mockserver_request.log
64-
*
65-
* @param httpRequest the http request that is matched against when deciding what to log if null all requests are logged
66-
*/
67-
public ProxyClient dumpToLogAsJSON(HttpRequest httpRequest) {
68-
sendRequest(request().withMethod("PUT").withPath(calculatePath("dumpToLog")).withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : ""));
69-
return this;
70-
}
71-
72-
/**
73-
* Output Java code for creating all requests / responses as Expectations to the log.
74-
* They are printed into a dedicated log called mockserver_request.log
75-
*/
76-
public ProxyClient dumpToLogAsJava() {
77-
return dumpToLogAsJava(null);
78-
}
79-
80-
/**
81-
* Output Java code for creating matching requests and their responses as Expectations to the log.
82-
* They are printed into a dedicated log called mockserver_request.log
83-
*
84-
* @param httpRequest the http request that is matched against when deciding what to log if null all requests are logged
85-
*/
86-
public ProxyClient dumpToLogAsJava(HttpRequest httpRequest) {
87-
sendRequest(request().withMethod("PUT").withPath(calculatePath("dumpToLog?format=java")).withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : ""));
88-
return this;
89-
}
90-
91-
/**
92-
* Retrieve the recorded requests that match the httpRequest parameter as expectations, use null for the parameter to retrieve all requests
93-
*
94-
* @param httpRequest the http request that is matched against when deciding whether to return each expectation, use null for the parameter to retrieve for all requests
95-
* @return an array of all expectations that have been recorded by the proxy
96-
*/
97-
public Expectation[] retrieveAsExpectations(HttpRequest httpRequest) {
98-
HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("retrieve")).withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : ""));
99-
return expectationSerializer.deserializeArray(httpResponse.getBodyAsString());
100-
}
101-
102-
/**
103-
* Retrieve the recorded requests that match the httpRequest parameter as a JSON array, use null for the parameter to retrieve all requests
104-
*
105-
* @param httpRequest the http request that is matched against when deciding whether to return each expectation, use null for the parameter to retrieve for all requests
106-
* @return a JSON array of all expectations that have been recorded by the proxy
107-
*/
108-
public String retrieveAsJSON(HttpRequest httpRequest) {
109-
HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("retrieve")).withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : ""));
110-
return httpResponse.getBodyAsString();
111-
}
11253
}

0 commit comments

Comments
 (0)