Skip to content

Commit 28e327a

Browse files
committed
extending retrieve support in clients to support JSON and Java formats
1 parent 1e4d7e9 commit 28e327a

File tree

21 files changed

+873
-140
lines changed

21 files changed

+873
-140
lines changed

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

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
import org.mockserver.client.netty.SocketConnectionException;
88
import org.mockserver.client.serialization.*;
99
import org.mockserver.mock.Expectation;
10-
import org.mockserver.mock.HttpStateHandler;
11-
import org.mockserver.model.HttpRequest;
12-
import org.mockserver.model.HttpResponse;
13-
import org.mockserver.model.HttpStatusCode;
10+
import org.mockserver.model.Format;
11+
import org.mockserver.model.*;
1412
import org.mockserver.verify.Verification;
1513
import org.mockserver.verify.VerificationSequence;
1614
import org.mockserver.verify.VerificationTimes;
@@ -205,7 +203,7 @@ public T clear(HttpRequest httpRequest) {
205203
* @param httpRequest the http request that is matched against when deciding whether to clear each expectation if null all expectations are cleared
206204
* @param type the type to clear, EXPECTATION, LOG or BOTH
207205
*/
208-
public T clear(HttpRequest httpRequest, HttpStateHandler.ClearType type) {
206+
public T clear(HttpRequest httpRequest, ClearType type) {
209207
sendRequest(request().withMethod("PUT").withPath(calculatePath("clear")).withQueryStringParameter("type", type.name().toLowerCase()).withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", Charsets.UTF_8));
210208
return clientClass.cast(this);
211209
}
@@ -300,17 +298,44 @@ public T verifyZeroInteractions() throws AssertionError {
300298
* @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
301299
*/
302300
public HttpRequest[] retrieveRecordedRequests(HttpRequest httpRequest) {
301+
String recordedRequests = retrieveRecordedRequests(httpRequest, Format.JSON);
302+
if (StringUtils.isNotEmpty(recordedRequests)) {
303+
return httpRequestSerializer.deserializeArray(recordedRequests);
304+
} else {
305+
return new HttpRequest[0];
306+
}
307+
}
308+
309+
/**
310+
* Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests
311+
*
312+
* @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
313+
* @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
314+
*/
315+
public String retrieveRecordedRequests(HttpRequest httpRequest, Format format) {
303316
HttpResponse httpResponse = sendRequest(
304317
request()
305318
.withMethod("PUT")
306319
.withPath(calculatePath("retrieve"))
307-
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.REQUESTS.name())
320+
.withQueryStringParameter("type", RetrieveType.REQUESTS.name())
321+
.withQueryStringParameter("format", format.name())
308322
.withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", Charsets.UTF_8)
309323
);
310-
if (StringUtils.isNotEmpty(httpResponse.getBodyAsString())) {
311-
return httpRequestSerializer.deserializeArray(httpResponse.getBodyAsString());
324+
return httpResponse.getBodyAsString();
325+
}
326+
327+
/**
328+
* Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests
329+
*
330+
* @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
331+
* @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
332+
*/
333+
public Expectation[] retrieveRecordedExpectations(HttpRequest httpRequest) {
334+
String recordedExpectations = retrieveRecordedExpectations(httpRequest, Format.JSON);
335+
if (!Strings.isNullOrEmpty(recordedExpectations)) {
336+
return expectationSerializer.deserializeArray(recordedExpectations);
312337
} else {
313-
return new HttpRequest[0];
338+
return new Expectation[0];
314339
}
315340
}
316341

@@ -320,18 +345,15 @@ public HttpRequest[] retrieveRecordedRequests(HttpRequest httpRequest) {
320345
* @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
321346
* @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
322347
*/
323-
public Expectation[] retrieveRecordedExpectations(HttpRequest httpRequest) {
348+
public String retrieveRecordedExpectations(HttpRequest httpRequest, Format format) {
324349
HttpResponse httpResponse = sendRequest(
325350
request()
326351
.withMethod("PUT")
327352
.withPath(calculatePath("retrieve"))
328-
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.RECORDED_EXPECTATIONS.name())
353+
.withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name())
354+
.withQueryStringParameter("format", format.name())
329355
.withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", Charsets.UTF_8)
330356
);
331-
if (!joptsimple.internal.Strings.isNullOrEmpty(httpResponse.getBodyAsString())) {
332-
return expectationSerializer.deserializeArray(httpResponse.getBodyAsString());
333-
} else {
334-
return new Expectation[0];
335-
}
357+
return httpResponse.getBodyAsString();
336358
}
337359
}

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package org.mockserver.client.server;
22

33
import com.google.common.base.Charsets;
4-
import joptsimple.internal.Strings;
4+
import com.google.common.base.Strings;
55
import org.mockserver.client.AbstractClient;
66
import org.mockserver.matchers.TimeToLive;
77
import org.mockserver.matchers.Times;
88
import org.mockserver.mock.Expectation;
9-
import org.mockserver.mock.HttpStateHandler;
9+
import org.mockserver.model.Format;
1010
import org.mockserver.model.HttpRequest;
1111
import org.mockserver.model.HttpResponse;
12+
import org.mockserver.model.RetrieveType;
1213

1314
import static org.mockserver.character.Character.NEW_LINE;
1415
import static org.mockserver.model.HttpRequest.request;
@@ -107,17 +108,29 @@ void sendExpectation(Expectation expectation) {
107108
* @return an array of all expectations that have been setup
108109
*/
109110
public Expectation[] retrieveActiveExpectations(HttpRequest httpRequest) {
111+
String activeExpectations = retrieveActiveExpectations(httpRequest, Format.JSON);
112+
if (!Strings.isNullOrEmpty(activeExpectations)) {
113+
return expectationSerializer.deserializeArray(activeExpectations);
114+
} else {
115+
return new Expectation[0];
116+
}
117+
}
118+
119+
/**
120+
* Retrieve the already setup expectations match the httpRequest parameter, use null for the parameter to retrieve all expectations
121+
*
122+
* @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
123+
* @return an array of all expectations that have been setup
124+
*/
125+
public String retrieveActiveExpectations(HttpRequest httpRequest, Format format) {
110126
HttpResponse httpResponse = sendRequest(
111127
request()
112128
.withMethod("PUT")
113129
.withPath(calculatePath("retrieve"))
114-
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.ACTIVE_EXPECTATIONS.name())
130+
.withQueryStringParameter("type", RetrieveType.ACTIVE_EXPECTATIONS.name())
131+
.withQueryStringParameter("format", format.name())
115132
.withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", Charsets.UTF_8)
116133
);
117-
if (!Strings.isNullOrEmpty(httpResponse.getBodyAsString())) {
118-
return expectationSerializer.deserializeArray(httpResponse.getBodyAsString());
119-
} else {
120-
return new Expectation[0];
121-
}
134+
return httpResponse.getBodyAsString();
122135
}
123136
}

mockserver-client-java/src/test/java/org/mockserver/client/proxy/ProxyClientTest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.mockserver.client.proxy;
22

33
import com.google.common.base.Charsets;
4+
import org.apache.velocity.runtime.directive.contrib.For;
45
import org.junit.Before;
56
import org.junit.Rule;
67
import org.junit.Test;
@@ -14,15 +15,13 @@
1415
import org.mockserver.client.serialization.VerificationSequenceSerializer;
1516
import org.mockserver.client.serialization.VerificationSerializer;
1617
import org.mockserver.mock.Expectation;
17-
import org.mockserver.mock.HttpStateHandler;
18-
import org.mockserver.model.HttpRequest;
19-
import org.mockserver.model.HttpStatusCode;
20-
import org.mockserver.model.StringBody;
18+
import org.mockserver.model.*;
2119
import org.mockserver.verify.Verification;
2220
import org.mockserver.verify.VerificationSequence;
2321
import org.mockserver.verify.VerificationTimes;
2422

2523
import java.io.UnsupportedEncodingException;
24+
import java.text.Normalizer;
2625

2726
import static io.netty.handler.codec.http.HttpHeaderNames.HOST;
2827
import static org.hamcrest.CoreMatchers.is;
@@ -195,7 +194,7 @@ public void shouldSendClearRequestWithType() throws Exception {
195194
when(mockHttpRequestSerializer.serialize(someRequestMatcher)).thenReturn(someRequestMatcher.toString());
196195

197196
// when
198-
proxyClient.clear(someRequestMatcher, HttpStateHandler.ClearType.LOG);
197+
proxyClient.clear(someRequestMatcher, ClearType.LOG);
199198

200199
// then
201200
verify(mockHttpClient).sendRequest(
@@ -248,7 +247,8 @@ public void shouldRetrieveRequests() throws UnsupportedEncodingException {
248247
.withHeader(HOST.toString(), "localhost:" + 1090)
249248
.withMethod("PUT")
250249
.withPath("/retrieve")
251-
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.REQUESTS.name())
250+
.withQueryStringParameter("type", RetrieveType.REQUESTS.name())
251+
.withQueryStringParameter("format", Format.JSON.name())
252252
.withBody(someRequestMatcher.toString(), Charsets.UTF_8));
253253
verify(mockHttpRequestSerializer).deserializeArray("body");
254254
}
@@ -269,7 +269,8 @@ public void shouldRetrieveRequestsWithNullRequest() throws UnsupportedEncodingEx
269269
.withHeader(HOST.toString(), "localhost:" + 1090)
270270
.withMethod("PUT")
271271
.withPath("/retrieve")
272-
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.REQUESTS.name())
272+
.withQueryStringParameter("type", RetrieveType.REQUESTS.name())
273+
.withQueryStringParameter("format", Format.JSON.name())
273274
.withBody("", Charsets.UTF_8)
274275
);
275276
verify(mockHttpRequestSerializer).deserializeArray("body");
@@ -299,7 +300,8 @@ public void shouldRetrieveRecordedExpectations() throws UnsupportedEncodingExcep
299300
.withHeader(HOST.toString(), "localhost:" + 1090)
300301
.withMethod("PUT")
301302
.withPath("/retrieve")
302-
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.RECORDED_EXPECTATIONS.name())
303+
.withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name())
304+
.withQueryStringParameter("format", Format.JSON.name())
303305
.withBody(someRequestMatcher.toString(), Charsets.UTF_8)
304306
);
305307
verify(mockExpectationSerializer).deserializeArray("body");
@@ -321,7 +323,8 @@ public void shouldRetrieveExpectationsWithNullRequest() throws UnsupportedEncodi
321323
.withHeader(HOST.toString(), "localhost:" + 1090)
322324
.withMethod("PUT")
323325
.withPath("/retrieve")
324-
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.RECORDED_EXPECTATIONS.name())
326+
.withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name())
327+
.withQueryStringParameter("format", Format.JSON.name())
325328
.withBody("", Charsets.UTF_8)
326329
);
327330
verify(mockExpectationSerializer).deserializeArray("body");

0 commit comments

Comments
 (0)