Skip to content

Commit

Permalink
extending retrieve support in clients to support JSON and Java formats
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdbloom committed Dec 5, 2017
1 parent 1e4d7e9 commit 28e327a
Show file tree
Hide file tree
Showing 21 changed files with 873 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import org.mockserver.client.netty.SocketConnectionException;
import org.mockserver.client.serialization.*;
import org.mockserver.mock.Expectation;
import org.mockserver.mock.HttpStateHandler;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.mockserver.model.HttpStatusCode;
import org.mockserver.model.Format;
import org.mockserver.model.*;
import org.mockserver.verify.Verification;
import org.mockserver.verify.VerificationSequence;
import org.mockserver.verify.VerificationTimes;
Expand Down Expand Up @@ -205,7 +203,7 @@ public T clear(HttpRequest httpRequest) {
* @param httpRequest the http request that is matched against when deciding whether to clear each expectation if null all expectations are cleared
* @param type the type to clear, EXPECTATION, LOG or BOTH
*/
public T clear(HttpRequest httpRequest, HttpStateHandler.ClearType type) {
public T clear(HttpRequest httpRequest, ClearType type) {
sendRequest(request().withMethod("PUT").withPath(calculatePath("clear")).withQueryStringParameter("type", type.name().toLowerCase()).withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", Charsets.UTF_8));
return clientClass.cast(this);
}
Expand Down Expand Up @@ -300,17 +298,44 @@ public T verifyZeroInteractions() throws AssertionError {
* @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
*/
public HttpRequest[] retrieveRecordedRequests(HttpRequest httpRequest) {
String recordedRequests = retrieveRecordedRequests(httpRequest, Format.JSON);
if (StringUtils.isNotEmpty(recordedRequests)) {
return httpRequestSerializer.deserializeArray(recordedRequests);
} else {
return new HttpRequest[0];
}
}

/**
* Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests
*
* @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
* @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
*/
public String retrieveRecordedRequests(HttpRequest httpRequest, Format format) {
HttpResponse httpResponse = sendRequest(
request()
.withMethod("PUT")
.withPath(calculatePath("retrieve"))
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.REQUESTS.name())
.withQueryStringParameter("type", RetrieveType.REQUESTS.name())
.withQueryStringParameter("format", format.name())
.withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", Charsets.UTF_8)
);
if (StringUtils.isNotEmpty(httpResponse.getBodyAsString())) {
return httpRequestSerializer.deserializeArray(httpResponse.getBodyAsString());
return httpResponse.getBodyAsString();
}

/**
* Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests
*
* @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
* @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
*/
public Expectation[] retrieveRecordedExpectations(HttpRequest httpRequest) {
String recordedExpectations = retrieveRecordedExpectations(httpRequest, Format.JSON);
if (!Strings.isNullOrEmpty(recordedExpectations)) {
return expectationSerializer.deserializeArray(recordedExpectations);
} else {
return new HttpRequest[0];
return new Expectation[0];
}
}

Expand All @@ -320,18 +345,15 @@ public HttpRequest[] retrieveRecordedRequests(HttpRequest httpRequest) {
* @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
* @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
*/
public Expectation[] retrieveRecordedExpectations(HttpRequest httpRequest) {
public String retrieveRecordedExpectations(HttpRequest httpRequest, Format format) {
HttpResponse httpResponse = sendRequest(
request()
.withMethod("PUT")
.withPath(calculatePath("retrieve"))
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.RECORDED_EXPECTATIONS.name())
.withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name())
.withQueryStringParameter("format", format.name())
.withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", Charsets.UTF_8)
);
if (!joptsimple.internal.Strings.isNullOrEmpty(httpResponse.getBodyAsString())) {
return expectationSerializer.deserializeArray(httpResponse.getBodyAsString());
} else {
return new Expectation[0];
}
return httpResponse.getBodyAsString();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.mockserver.client.server;

import com.google.common.base.Charsets;
import joptsimple.internal.Strings;
import com.google.common.base.Strings;
import org.mockserver.client.AbstractClient;
import org.mockserver.matchers.TimeToLive;
import org.mockserver.matchers.Times;
import org.mockserver.mock.Expectation;
import org.mockserver.mock.HttpStateHandler;
import org.mockserver.model.Format;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.mockserver.model.RetrieveType;

import static org.mockserver.character.Character.NEW_LINE;
import static org.mockserver.model.HttpRequest.request;
Expand Down Expand Up @@ -107,17 +108,29 @@ void sendExpectation(Expectation expectation) {
* @return an array of all expectations that have been setup
*/
public Expectation[] retrieveActiveExpectations(HttpRequest httpRequest) {
String activeExpectations = retrieveActiveExpectations(httpRequest, Format.JSON);
if (!Strings.isNullOrEmpty(activeExpectations)) {
return expectationSerializer.deserializeArray(activeExpectations);
} else {
return new Expectation[0];
}
}

/**
* Retrieve the already setup expectations match the httpRequest parameter, use null for the parameter to retrieve all expectations
*
* @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
* @return an array of all expectations that have been setup
*/
public String retrieveActiveExpectations(HttpRequest httpRequest, Format format) {
HttpResponse httpResponse = sendRequest(
request()
.withMethod("PUT")
.withPath(calculatePath("retrieve"))
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.ACTIVE_EXPECTATIONS.name())
.withQueryStringParameter("type", RetrieveType.ACTIVE_EXPECTATIONS.name())
.withQueryStringParameter("format", format.name())
.withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", Charsets.UTF_8)
);
if (!Strings.isNullOrEmpty(httpResponse.getBodyAsString())) {
return expectationSerializer.deserializeArray(httpResponse.getBodyAsString());
} else {
return new Expectation[0];
}
return httpResponse.getBodyAsString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.mockserver.client.proxy;

import com.google.common.base.Charsets;
import org.apache.velocity.runtime.directive.contrib.For;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -14,15 +15,13 @@
import org.mockserver.client.serialization.VerificationSequenceSerializer;
import org.mockserver.client.serialization.VerificationSerializer;
import org.mockserver.mock.Expectation;
import org.mockserver.mock.HttpStateHandler;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpStatusCode;
import org.mockserver.model.StringBody;
import org.mockserver.model.*;
import org.mockserver.verify.Verification;
import org.mockserver.verify.VerificationSequence;
import org.mockserver.verify.VerificationTimes;

import java.io.UnsupportedEncodingException;
import java.text.Normalizer;

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

// when
proxyClient.clear(someRequestMatcher, HttpStateHandler.ClearType.LOG);
proxyClient.clear(someRequestMatcher, ClearType.LOG);

// then
verify(mockHttpClient).sendRequest(
Expand Down Expand Up @@ -248,7 +247,8 @@ public void shouldRetrieveRequests() throws UnsupportedEncodingException {
.withHeader(HOST.toString(), "localhost:" + 1090)
.withMethod("PUT")
.withPath("/retrieve")
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.REQUESTS.name())
.withQueryStringParameter("type", RetrieveType.REQUESTS.name())
.withQueryStringParameter("format", Format.JSON.name())
.withBody(someRequestMatcher.toString(), Charsets.UTF_8));
verify(mockHttpRequestSerializer).deserializeArray("body");
}
Expand All @@ -269,7 +269,8 @@ public void shouldRetrieveRequestsWithNullRequest() throws UnsupportedEncodingEx
.withHeader(HOST.toString(), "localhost:" + 1090)
.withMethod("PUT")
.withPath("/retrieve")
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.REQUESTS.name())
.withQueryStringParameter("type", RetrieveType.REQUESTS.name())
.withQueryStringParameter("format", Format.JSON.name())
.withBody("", Charsets.UTF_8)
);
verify(mockHttpRequestSerializer).deserializeArray("body");
Expand Down Expand Up @@ -299,7 +300,8 @@ public void shouldRetrieveRecordedExpectations() throws UnsupportedEncodingExcep
.withHeader(HOST.toString(), "localhost:" + 1090)
.withMethod("PUT")
.withPath("/retrieve")
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.RECORDED_EXPECTATIONS.name())
.withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name())
.withQueryStringParameter("format", Format.JSON.name())
.withBody(someRequestMatcher.toString(), Charsets.UTF_8)
);
verify(mockExpectationSerializer).deserializeArray("body");
Expand All @@ -321,7 +323,8 @@ public void shouldRetrieveExpectationsWithNullRequest() throws UnsupportedEncodi
.withHeader(HOST.toString(), "localhost:" + 1090)
.withMethod("PUT")
.withPath("/retrieve")
.withQueryStringParameter("type", HttpStateHandler.RetrieveType.RECORDED_EXPECTATIONS.name())
.withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name())
.withQueryStringParameter("format", Format.JSON.name())
.withBody("", Charsets.UTF_8)
);
verify(mockExpectationSerializer).deserializeArray("body");
Expand Down
Loading

0 comments on commit 28e327a

Please sign in to comment.