From 28e327a0bc5f01246b780d86e830d4af4974108c Mon Sep 17 00:00:00 2001 From: James Bloom Date: Tue, 5 Dec 2017 08:29:08 +0000 Subject: [PATCH] extending retrieve support in clients to support JSON and Java formats --- .../org/mockserver/client/AbstractClient.java | 54 ++- .../client/server/MockServerClient.java | 29 +- .../client/proxy/ProxyClientTest.java | 21 +- .../MockServerClientIntegrationTest.java | 400 ++++++++++++++++- .../client/server/MockServerClientTest.java | 23 +- .../websocket/WebSocketClientHandler.java | 2 +- .../serialization/ExpectationSerializer.java | 2 +- .../serialization/HttpRequestSerializer.java | 2 +- .../serialization/HttpResponseSerializer.java | 2 +- .../VerificationSequenceSerializer.java | 2 +- .../serialization/VerificationSerializer.java | 2 +- .../org/mockserver/mock/HttpStateHandler.java | 16 - .../java/org/mockserver/model/ClearType.java | 10 + .../java/org/mockserver/model/Format.java | 9 + .../org/mockserver/model/RetrieveType.java | 10 + .../xmlschema/XmlSchemaValidator.java | 2 +- .../AbstractClientServerIntegrationTest.java | 410 +++++++++++++++--- .../mockserver/MockServerHandlerTest.java | 6 +- .../proxy/http/HttpProxyHandlerTest.java | 3 +- .../mockserver/proxy/ProxyServletTest.java | 3 +- .../server/MockServerServletTest.java | 5 +- 21 files changed, 873 insertions(+), 140 deletions(-) create mode 100644 mockserver-core/src/main/java/org/mockserver/model/ClearType.java create mode 100644 mockserver-core/src/main/java/org/mockserver/model/Format.java create mode 100644 mockserver-core/src/main/java/org/mockserver/model/RetrieveType.java diff --git a/mockserver-client-java/src/main/java/org/mockserver/client/AbstractClient.java b/mockserver-client-java/src/main/java/org/mockserver/client/AbstractClient.java index 2753f392d..044800af1 100644 --- a/mockserver-client-java/src/main/java/org/mockserver/client/AbstractClient.java +++ b/mockserver-client-java/src/main/java/org/mockserver/client/AbstractClient.java @@ -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; @@ -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); } @@ -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]; } } @@ -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(); } } diff --git a/mockserver-client-java/src/main/java/org/mockserver/client/server/MockServerClient.java b/mockserver-client-java/src/main/java/org/mockserver/client/server/MockServerClient.java index 966a0ee98..a88d380bb 100644 --- a/mockserver-client-java/src/main/java/org/mockserver/client/server/MockServerClient.java +++ b/mockserver-client-java/src/main/java/org/mockserver/client/server/MockServerClient.java @@ -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; @@ -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(); } } diff --git a/mockserver-client-java/src/test/java/org/mockserver/client/proxy/ProxyClientTest.java b/mockserver-client-java/src/test/java/org/mockserver/client/proxy/ProxyClientTest.java index 431db3cb1..00f058a16 100644 --- a/mockserver-client-java/src/test/java/org/mockserver/client/proxy/ProxyClientTest.java +++ b/mockserver-client-java/src/test/java/org/mockserver/client/proxy/ProxyClientTest.java @@ -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; @@ -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; @@ -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( @@ -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"); } @@ -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"); @@ -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"); @@ -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"); diff --git a/mockserver-client-java/src/test/java/org/mockserver/client/server/MockServerClientIntegrationTest.java b/mockserver-client-java/src/test/java/org/mockserver/client/server/MockServerClientIntegrationTest.java index 231761060..6e199e004 100644 --- a/mockserver-client-java/src/test/java/org/mockserver/client/server/MockServerClientIntegrationTest.java +++ b/mockserver-client-java/src/test/java/org/mockserver/client/server/MockServerClientIntegrationTest.java @@ -4,11 +4,12 @@ import org.junit.rules.ExpectedException; import org.mockserver.client.serialization.ExpectationSerializer; import org.mockserver.client.serialization.HttpRequestSerializer; +import org.mockserver.client.serialization.java.ExpectationToJavaSerializer; +import org.mockserver.client.serialization.java.HttpRequestToJavaSerializer; import org.mockserver.echo.http.EchoServer; import org.mockserver.filters.LogFilter; import org.mockserver.matchers.TimeToLive; import org.mockserver.mock.Expectation; -import org.mockserver.mock.HttpStateHandler; import org.mockserver.model.*; import org.mockserver.socket.PortFactory; import org.mockserver.verify.VerificationTimes; @@ -526,7 +527,7 @@ public void shouldSendClearRequestWithType() throws Exception { request() .withPath("/some_path") .withBody(new StringBody("some_request_body")), - HttpStateHandler.ClearType.LOG + ClearType.LOG ); // then @@ -612,6 +613,8 @@ public void shouldRetrieveRequests() { request() .withMethod("PUT") .withPath("/retrieve") + .withQueryStringParameter("type", RetrieveType.REQUESTS.name()) + .withQueryStringParameter("format", Format.JSON.name()) .withHeaders( new Header("host", "localhost:" + freePort), new Header("accept-encoding", "gzip,deflate"), @@ -655,6 +658,8 @@ public void shouldRetrieveRequestsWithNullRequest() { request() .withMethod("PUT") .withPath("/retrieve") + .withQueryStringParameter("type", RetrieveType.REQUESTS.name()) + .withQueryStringParameter("format", Format.JSON.name()) .withHeaders( new Header("host", "localhost:" + freePort), new Header("accept-encoding", "gzip,deflate"), @@ -671,7 +676,103 @@ public void shouldRetrieveRequestsWithNullRequest() { } @Test - public void shouldRetrieveSetupExpectations() { + public void shouldRetrieveRequestsAsJson() { + // given + String serializedRequests = new HttpRequestSerializer().serialize(Arrays.asList( + request("/some_request_one"), + request("/some_request_two") + )); + echoServer.withNextResponse( + response() + .withStatusCode(201) + .withBody(new StringBody(serializedRequests)) + ); + + // when + String recordedResponse = mockServerClient.retrieveRecordedRequests( + request() + .withPath("/some_path") + .withBody(new StringBody("some_request_body")), + Format.JSON + ); + + // then + assertThat(recordedResponse, is(serializedRequests)); + assertThat(logFilter.retrieveRequests(request()).size(), is(1)); + String result = logFilter.verify(verification().withRequest( + request() + .withMethod("PUT") + .withPath("/retrieve") + .withQueryStringParameter("type", RetrieveType.REQUESTS.name()) + .withQueryStringParameter("format", Format.JSON.name()) + .withHeaders( + new Header("host", "localhost:" + freePort), + new Header("accept-encoding", "gzip,deflate"), + new Header("connection", "keep-alive"), + new Header("content-type", "text/plain; 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); + } + } + + @Test + public void shouldRetrieveRequestsAsJava() { + // given + String serializedRequest = new HttpRequestToJavaSerializer().serialize(Arrays.asList( + request("/some_request_one"), + request("/some_request_two") + )); + echoServer.withNextResponse( + response() + .withStatusCode(201) + .withBody(new StringBody(serializedRequest)) + ); + + // when + String actualResponse = mockServerClient.retrieveRecordedRequests( + request() + .withPath("/some_path") + .withBody(new StringBody("some_request_body")), + Format.JAVA + ); + + // then + assertThat(actualResponse, is(serializedRequest)); + assertThat(logFilter.retrieveRequests(request()).size(), is(1)); + String result = logFilter.verify(verification().withRequest( + request() + .withMethod("PUT") + .withPath("/retrieve") + .withQueryStringParameter("type", RetrieveType.REQUESTS.name()) + .withQueryStringParameter("format", Format.JAVA.name()) + .withHeaders( + new Header("host", "localhost:" + freePort), + new Header("accept-encoding", "gzip,deflate"), + new Header("connection", "keep-alive"), + new Header("content-type", "text/plain; 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); + } + } + + @Test + public void shouldRetrieveActiveExpectations() { // given echoServer.withNextResponse( response() @@ -699,15 +800,14 @@ public void shouldRetrieveSetupExpectations() { request() .withMethod("PUT") .withPath("/retrieve") + .withQueryStringParameter("type", RetrieveType.ACTIVE_EXPECTATIONS.name()) + .withQueryStringParameter("format", Format.JSON.name()) .withHeaders( new Header("host", "localhost:" + freePort), new Header("accept-encoding", "gzip,deflate"), new Header("connection", "keep-alive"), new Header("content-type", "text/plain; charset=utf-8") ) - .withQueryStringParameters( - new Parameter("type", "ACTIVE_EXPECTATIONS") - ) .withSecure(false) .withKeepAlive(true) .withBody(new StringBody("{" + NEW_LINE + @@ -721,7 +821,7 @@ public void shouldRetrieveSetupExpectations() { } @Test - public void shouldRetrieveSetupExpectationsWithNullRequest() { + public void shouldRetrieveActiveExpectationsWithNullRequest() { // given echoServer.withNextResponse( response() @@ -745,6 +845,8 @@ public void shouldRetrieveSetupExpectationsWithNullRequest() { request() .withMethod("PUT") .withPath("/retrieve") + .withQueryStringParameter("type", RetrieveType.ACTIVE_EXPECTATIONS.name()) + .withQueryStringParameter("format", Format.JSON.name()) .withHeaders( new Header("host", "localhost:" + freePort), new Header("accept-encoding", "gzip,deflate"), @@ -752,11 +854,291 @@ public void shouldRetrieveSetupExpectationsWithNullRequest() { new Header("connection", "keep-alive"), new Header("content-type", "text/plain; charset=utf-8") ) - .withQueryStringParameters( - new Parameter("type", "ACTIVE_EXPECTATIONS") + .withSecure(false) + .withKeepAlive(true) + )); + if (result != null && !result.isEmpty()) { + throw new AssertionError(result); + } + } + + @Test + public void shouldRetrieveActiveExpectationsAsJson() { + // given + String serializeExpectations = new ExpectationSerializer().serialize( + new Expectation(request("/some_request_one"), unlimited(), TimeToLive.unlimited()).thenRespond(response()), + new Expectation(request("/some_request_two"), unlimited(), TimeToLive.unlimited()).thenRespond(response()) + ); + echoServer.withNextResponse( + response() + .withStatusCode(201) + .withBody(new StringBody(serializeExpectations)) + ); + + // when + String actualResponse = mockServerClient.retrieveActiveExpectations( + request() + .withPath("/some_path") + .withBody(new StringBody("some_request_body")), + Format.JSON + ); + + // then + assertThat(actualResponse, is(serializeExpectations)); + assertThat(logFilter.retrieveRequests(request()).size(), is(1)); + String result = logFilter.verify(verification().withRequest( + request() + .withMethod("PUT") + .withPath("/retrieve") + .withQueryStringParameter("type", RetrieveType.ACTIVE_EXPECTATIONS.name()) + .withQueryStringParameter("format", Format.JSON.name()) + .withHeaders( + new Header("host", "localhost:" + freePort), + new Header("accept-encoding", "gzip,deflate"), + new Header("connection", "keep-alive"), + new Header("content-type", "text/plain; 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); + } + } + + @Test + public void shouldRetrieveActiveExpectationsAsJava() { + // given + String serializedExpectations = new ExpectationToJavaSerializer().serialize(Arrays.asList( + new Expectation(request("/some_request_one"), unlimited(), TimeToLive.unlimited()).thenRespond(response()), + new Expectation(request("/some_request_two"), unlimited(), TimeToLive.unlimited()).thenRespond(response()) + )); + echoServer.withNextResponse( + response() + .withStatusCode(201) + .withBody(new StringBody(serializedExpectations)) + ); + + // when + String actualResponse = mockServerClient.retrieveActiveExpectations( + request() + .withPath("/some_path") + .withBody(new StringBody("some_request_body")), + Format.JAVA + ); + + // then + assertThat(actualResponse, is(serializedExpectations)); + assertThat(logFilter.retrieveRequests(request()).size(), is(1)); + String result = logFilter.verify(verification().withRequest( + request() + .withMethod("PUT") + .withPath("/retrieve") + .withQueryStringParameter("type", RetrieveType.ACTIVE_EXPECTATIONS.name()) + .withQueryStringParameter("format", Format.JAVA.name()) + .withHeaders( + new Header("host", "localhost:" + freePort), + new Header("accept-encoding", "gzip,deflate"), + new Header("connection", "keep-alive"), + new Header("content-type", "text/plain; 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); + } + } + + @Test + public void shouldRetrieveRecordedExpectations() { + // given + echoServer.withNextResponse( + response() + .withStatusCode(201) + .withBody(new StringBody(new ExpectationSerializer().serialize( + new Expectation(request("/some_request_one"), unlimited(), TimeToLive.unlimited()).thenRespond(response()), + new Expectation(request("/some_request_two"), unlimited(), TimeToLive.unlimited()).thenRespond(response()) + ))) + ); + + // when + Expectation[] actualResponse = mockServerClient.retrieveRecordedExpectations( + request() + .withPath("/some_path") + .withBody(new StringBody("some_request_body")) + ); + + // then + assertThat(Arrays.asList(actualResponse), hasItems( + new Expectation(request("/some_request_one"), unlimited(), TimeToLive.unlimited()).thenRespond(response()), + new Expectation(request("/some_request_two"), unlimited(), TimeToLive.unlimited()).thenRespond(response()) + )); + assertThat(logFilter.retrieveRequests(request()).size(), is(1)); + String result = logFilter.verify(verification().withRequest( + request() + .withMethod("PUT") + .withPath("/retrieve") + .withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name()) + .withQueryStringParameter("format", Format.JSON.name()) + .withHeaders( + new Header("host", "localhost:" + freePort), + new Header("accept-encoding", "gzip,deflate"), + new Header("connection", "keep-alive"), + new Header("content-type", "text/plain; 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); + } + } + + @Test + public void shouldRetrieveRecordedExpectationsWithNullRequest() { + // given + echoServer.withNextResponse( + response() + .withStatusCode(201) + .withBody(new StringBody(new ExpectationSerializer().serialize( + new Expectation(request("/some_request_one"), unlimited(), TimeToLive.unlimited()).thenRespond(response()), + new Expectation(request("/some_request_two"), unlimited(), TimeToLive.unlimited()).thenRespond(response()) + ))) + ); + + // when + Expectation[] actualResponse = mockServerClient.retrieveRecordedExpectations(null); + + // then + assertThat(Arrays.asList(actualResponse), hasItems( + new Expectation(request("/some_request_one"), unlimited(), TimeToLive.unlimited()).thenRespond(response()), + new Expectation(request("/some_request_two"), unlimited(), TimeToLive.unlimited()).thenRespond(response()) + )); + assertThat(logFilter.retrieveRequests(request()).size(), is(1)); + String result = logFilter.verify(verification().withRequest( + request() + .withMethod("PUT") + .withPath("/retrieve") + .withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name()) + .withQueryStringParameter("format", Format.JSON.name()) + .withHeaders( + new Header("host", "localhost:" + freePort), + new Header("accept-encoding", "gzip,deflate"), + new Header("content-length", "0"), + new Header("connection", "keep-alive"), + new Header("content-type", "text/plain; charset=utf-8") + ) + .withSecure(false) + .withKeepAlive(true) + )); + if (result != null && !result.isEmpty()) { + throw new AssertionError(result); + } + } + + @Test + public void shouldRetrieveRecordedExpectationsAsJson() { + // given + String serializeExpectations = new ExpectationSerializer().serialize( + new Expectation(request("/some_request_one"), unlimited(), TimeToLive.unlimited()).thenRespond(response()), + new Expectation(request("/some_request_two"), unlimited(), TimeToLive.unlimited()).thenRespond(response()) + ); + echoServer.withNextResponse( + response() + .withStatusCode(201) + .withBody(new StringBody(serializeExpectations)) + ); + + // when + String actualResponse = mockServerClient.retrieveRecordedExpectations( + request() + .withPath("/some_path") + .withBody(new StringBody("some_request_body")), + Format.JSON + ); + + // then + assertThat(actualResponse, is(serializeExpectations)); + assertThat(logFilter.retrieveRequests(request()).size(), is(1)); + String result = logFilter.verify(verification().withRequest( + request() + .withMethod("PUT") + .withPath("/retrieve") + .withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name()) + .withQueryStringParameter("format", Format.JSON.name()) + .withHeaders( + new Header("host", "localhost:" + freePort), + new Header("accept-encoding", "gzip,deflate"), + new Header("connection", "keep-alive"), + new Header("content-type", "text/plain; 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); + } + } + + @Test + public void shouldRetrieveRecordedExpectationsAsJava() { + // given + String serializedExpectations = new ExpectationToJavaSerializer().serialize(Arrays.asList( + new Expectation(request("/some_request_one"), unlimited(), TimeToLive.unlimited()).thenRespond(response()), + new Expectation(request("/some_request_two"), unlimited(), TimeToLive.unlimited()).thenRespond(response()) + )); + echoServer.withNextResponse( + response() + .withStatusCode(201) + .withBody(new StringBody(serializedExpectations)) + ); + + // when + String actualResponse = mockServerClient.retrieveRecordedExpectations( + request() + .withPath("/some_path") + .withBody(new StringBody("some_request_body")), + Format.JAVA + ); + + // then + assertThat(actualResponse, is(serializedExpectations)); + assertThat(logFilter.retrieveRequests(request()).size(), is(1)); + String result = logFilter.verify(verification().withRequest( + request() + .withMethod("PUT") + .withPath("/retrieve") + .withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name()) + .withQueryStringParameter("format", Format.JAVA.name()) + .withHeaders( + new Header("host", "localhost:" + freePort), + new Header("accept-encoding", "gzip,deflate"), + new Header("connection", "keep-alive"), + new Header("content-type", "text/plain; 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); diff --git a/mockserver-client-java/src/test/java/org/mockserver/client/server/MockServerClientTest.java b/mockserver-client-java/src/test/java/org/mockserver/client/server/MockServerClientTest.java index 4fab89c76..eee1b8a89 100644 --- a/mockserver-client-java/src/test/java/org/mockserver/client/server/MockServerClientTest.java +++ b/mockserver-client-java/src/test/java/org/mockserver/client/server/MockServerClientTest.java @@ -17,7 +17,6 @@ import org.mockserver.client.serialization.model.*; import org.mockserver.matchers.Times; import org.mockserver.mock.Expectation; -import org.mockserver.mock.HttpStateHandler; import org.mockserver.mock.action.ExpectationCallback; import org.mockserver.model.*; import org.mockserver.verify.Verification; @@ -535,7 +534,7 @@ public void shouldSendClearRequestWithType() throws Exception { when(mockHttpRequestSerializer.serialize(someRequestMatcher)).thenReturn(someRequestMatcher.toString()); // when - mockServerClient.clear(someRequestMatcher, HttpStateHandler.ClearType.LOG); + mockServerClient.clear(someRequestMatcher, ClearType.LOG); // then verify(mockHttpClient).sendRequest( @@ -588,7 +587,8 @@ public void shouldRetrieveRequests() throws UnsupportedEncodingException { .withHeader(HOST.toString(), "localhost:" + 1080) .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"); } @@ -609,7 +609,8 @@ public void shouldRetrieveRequestsWithNullRequest() throws UnsupportedEncodingEx .withHeader(HOST.toString(), "localhost:" + 1080) .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"); @@ -639,14 +640,15 @@ public void shouldRetrieveActiveExpectations() throws UnsupportedEncodingExcepti .withHeader(HOST.toString(), "localhost:" + 1080) .withMethod("PUT") .withPath("/retrieve") - .withQueryStringParameter("type", HttpStateHandler.RetrieveType.ACTIVE_EXPECTATIONS.name()) + .withQueryStringParameter("type", RetrieveType.ACTIVE_EXPECTATIONS.name()) + .withQueryStringParameter("format", Format.JSON.name()) .withBody(someRequestMatcher.toString(), Charsets.UTF_8) ); verify(mockExpectationSerializer).deserializeArray("body"); } @Test - public void shouldRetrieveSetupExpectationsWithNullRequest() throws UnsupportedEncodingException { + public void shouldRetrieveActiveExpectationsWithNullRequest() throws UnsupportedEncodingException { // given Expectation[] expectations = {}; when(mockHttpClient.sendRequest(any(HttpRequest.class))).thenReturn(response().withBody("body")); @@ -661,7 +663,8 @@ public void shouldRetrieveSetupExpectationsWithNullRequest() throws UnsupportedE .withHeader(HOST.toString(), "localhost:" + 1080) .withMethod("PUT") .withPath("/retrieve") - .withQueryStringParameter("type", HttpStateHandler.RetrieveType.ACTIVE_EXPECTATIONS.name()) + .withQueryStringParameter("type", RetrieveType.ACTIVE_EXPECTATIONS.name()) + .withQueryStringParameter("format", Format.JSON.name()) .withBody("", Charsets.UTF_8) ); verify(mockExpectationSerializer).deserializeArray("body"); @@ -691,7 +694,8 @@ public void shouldRetrieveRecordedExpectations() throws UnsupportedEncodingExcep .withHeader(HOST.toString(), "localhost:" + 1080) .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"); @@ -713,7 +717,8 @@ public void shouldRetrieveExpectationsWithNullRequest() throws UnsupportedEncodi .withHeader(HOST.toString(), "localhost:" + 1080) .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"); diff --git a/mockserver-core/src/main/java/org/mockserver/client/netty/websocket/WebSocketClientHandler.java b/mockserver-core/src/main/java/org/mockserver/client/netty/websocket/WebSocketClientHandler.java index dedf5c1ea..c01004ed3 100644 --- a/mockserver-core/src/main/java/org/mockserver/client/netty/websocket/WebSocketClientHandler.java +++ b/mockserver-core/src/main/java/org/mockserver/client/netty/websocket/WebSocketClientHandler.java @@ -1,5 +1,6 @@ package org.mockserver.client.netty.websocket; +import com.google.common.base.Strings; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; @@ -7,7 +8,6 @@ import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.websocketx.*; -import joptsimple.internal.Strings; import org.mockserver.client.netty.codec.mappers.FullHttpResponseToMockServerResponse; import org.mockserver.mappers.ContentTypeMapper; import org.slf4j.Logger; diff --git a/mockserver-core/src/main/java/org/mockserver/client/serialization/ExpectationSerializer.java b/mockserver-core/src/main/java/org/mockserver/client/serialization/ExpectationSerializer.java index 6865ceb6d..2d18b102d 100644 --- a/mockserver-core/src/main/java/org/mockserver/client/serialization/ExpectationSerializer.java +++ b/mockserver-core/src/main/java/org/mockserver/client/serialization/ExpectationSerializer.java @@ -2,7 +2,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Joiner; -import joptsimple.internal.Strings; +import com.google.common.base.Strings; import org.mockserver.client.serialization.model.ExpectationDTO; import org.mockserver.mock.Expectation; import org.mockserver.validator.jsonschema.JsonSchemaExpectationValidator; diff --git a/mockserver-core/src/main/java/org/mockserver/client/serialization/HttpRequestSerializer.java b/mockserver-core/src/main/java/org/mockserver/client/serialization/HttpRequestSerializer.java index fc8022ec3..1029a19b0 100644 --- a/mockserver-core/src/main/java/org/mockserver/client/serialization/HttpRequestSerializer.java +++ b/mockserver-core/src/main/java/org/mockserver/client/serialization/HttpRequestSerializer.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Joiner; -import joptsimple.internal.Strings; +import com.google.common.base.Strings; import org.mockserver.client.serialization.model.HttpRequestDTO; import org.mockserver.model.HttpRequest; import org.mockserver.templates.engine.model.HttpRequestTemplateObject; diff --git a/mockserver-core/src/main/java/org/mockserver/client/serialization/HttpResponseSerializer.java b/mockserver-core/src/main/java/org/mockserver/client/serialization/HttpResponseSerializer.java index 738af6c18..43d75b30c 100644 --- a/mockserver-core/src/main/java/org/mockserver/client/serialization/HttpResponseSerializer.java +++ b/mockserver-core/src/main/java/org/mockserver/client/serialization/HttpResponseSerializer.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Joiner; -import joptsimple.internal.Strings; +import com.google.common.base.Strings; import org.mockserver.client.serialization.model.HttpResponseDTO; import org.mockserver.model.HttpResponse; import org.mockserver.validator.jsonschema.JsonSchemaHttpResponseValidator; diff --git a/mockserver-core/src/main/java/org/mockserver/client/serialization/VerificationSequenceSerializer.java b/mockserver-core/src/main/java/org/mockserver/client/serialization/VerificationSequenceSerializer.java index 4666dee40..ddec6343f 100644 --- a/mockserver-core/src/main/java/org/mockserver/client/serialization/VerificationSequenceSerializer.java +++ b/mockserver-core/src/main/java/org/mockserver/client/serialization/VerificationSequenceSerializer.java @@ -1,7 +1,7 @@ package org.mockserver.client.serialization; import com.fasterxml.jackson.databind.ObjectMapper; -import joptsimple.internal.Strings; +import com.google.common.base.Strings; import org.mockserver.client.serialization.model.VerificationSequenceDTO; import org.mockserver.validator.jsonschema.JsonSchemaVerificationSequenceValidator; import org.mockserver.verify.VerificationSequence; diff --git a/mockserver-core/src/main/java/org/mockserver/client/serialization/VerificationSerializer.java b/mockserver-core/src/main/java/org/mockserver/client/serialization/VerificationSerializer.java index 0612f8ee1..6240cd36e 100644 --- a/mockserver-core/src/main/java/org/mockserver/client/serialization/VerificationSerializer.java +++ b/mockserver-core/src/main/java/org/mockserver/client/serialization/VerificationSerializer.java @@ -1,7 +1,7 @@ package org.mockserver.client.serialization; import com.fasterxml.jackson.databind.ObjectMapper; -import joptsimple.internal.Strings; +import com.google.common.base.Strings; import org.mockserver.client.serialization.model.VerificationDTO; import org.mockserver.validator.jsonschema.JsonSchemaVerificationValidator; import org.mockserver.verify.Verification; diff --git a/mockserver-core/src/main/java/org/mockserver/mock/HttpStateHandler.java b/mockserver-core/src/main/java/org/mockserver/mock/HttpStateHandler.java index 52a5da0ea..27130fc2d 100644 --- a/mockserver-core/src/main/java/org/mockserver/mock/HttpStateHandler.java +++ b/mockserver-core/src/main/java/org/mockserver/mock/HttpStateHandler.java @@ -260,20 +260,4 @@ public WebSocketClientRegistry getWebSocketClientRegistry() { return webSocketClientRegistry; } - public enum RetrieveType { - REQUESTS, - RECORDED_EXPECTATIONS, - ACTIVE_EXPECTATIONS - } - - public enum ClearType { - LOG, - EXPECTATIONS, - ALL - } - - public enum Format { - JAVA, - JSON - } } diff --git a/mockserver-core/src/main/java/org/mockserver/model/ClearType.java b/mockserver-core/src/main/java/org/mockserver/model/ClearType.java new file mode 100644 index 000000000..3973cbf6a --- /dev/null +++ b/mockserver-core/src/main/java/org/mockserver/model/ClearType.java @@ -0,0 +1,10 @@ +package org.mockserver.model; + +/** + * @author jamesdbloom + */ +public enum ClearType { + LOG, + EXPECTATIONS, + ALL +} diff --git a/mockserver-core/src/main/java/org/mockserver/model/Format.java b/mockserver-core/src/main/java/org/mockserver/model/Format.java new file mode 100644 index 000000000..384c28cce --- /dev/null +++ b/mockserver-core/src/main/java/org/mockserver/model/Format.java @@ -0,0 +1,9 @@ +package org.mockserver.model; + +/** + * @author jamesdbloom + */ +public enum Format { + JAVA, + JSON +} diff --git a/mockserver-core/src/main/java/org/mockserver/model/RetrieveType.java b/mockserver-core/src/main/java/org/mockserver/model/RetrieveType.java new file mode 100644 index 000000000..26cb0151e --- /dev/null +++ b/mockserver-core/src/main/java/org/mockserver/model/RetrieveType.java @@ -0,0 +1,10 @@ +package org.mockserver.model; + +/** + * @author jamesdbloom + */ +public enum RetrieveType { + REQUESTS, + RECORDED_EXPECTATIONS, + ACTIVE_EXPECTATIONS +} diff --git a/mockserver-core/src/main/java/org/mockserver/validator/xmlschema/XmlSchemaValidator.java b/mockserver-core/src/main/java/org/mockserver/validator/xmlschema/XmlSchemaValidator.java index fb6f79b32..3700ab83e 100644 --- a/mockserver-core/src/main/java/org/mockserver/validator/xmlschema/XmlSchemaValidator.java +++ b/mockserver-core/src/main/java/org/mockserver/validator/xmlschema/XmlSchemaValidator.java @@ -1,6 +1,6 @@ package org.mockserver.validator.xmlschema; -import joptsimple.internal.Strings; +import com.google.common.base.Strings; import org.mockserver.file.FileReader; import org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString; import org.mockserver.validator.Validator; diff --git a/mockserver-integration-testing/src/main/java/org/mockserver/integration/server/AbstractClientServerIntegrationTest.java b/mockserver-integration-testing/src/main/java/org/mockserver/integration/server/AbstractClientServerIntegrationTest.java index 006cd6231..0f04e06c8 100644 --- a/mockserver-integration-testing/src/main/java/org/mockserver/integration/server/AbstractClientServerIntegrationTest.java +++ b/mockserver-integration-testing/src/main/java/org/mockserver/integration/server/AbstractClientServerIntegrationTest.java @@ -7,10 +7,14 @@ import org.apache.commons.io.IOUtils; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; import org.mockserver.client.netty.NettyHttpClient; import org.mockserver.client.netty.SocketConnectionException; import org.mockserver.client.serialization.ExpectationSerializer; +import org.mockserver.client.serialization.HttpRequestSerializer; +import org.mockserver.client.serialization.java.ExpectationToJavaSerializer; +import org.mockserver.client.serialization.java.HttpRequestToJavaSerializer; import org.mockserver.client.server.MockServerClient; import org.mockserver.echo.http.EchoServer; import org.mockserver.matchers.HttpRequestMatcher; @@ -18,7 +22,6 @@ import org.mockserver.matchers.TimeToLive; import org.mockserver.matchers.Times; import org.mockserver.mock.Expectation; -import org.mockserver.mock.HttpStateHandler; import org.mockserver.model.*; import org.mockserver.socket.PortFactory; import org.mockserver.verify.VerificationTimes; @@ -4904,7 +4907,67 @@ public void shouldVerifySequenceOfRequestsReceivedIncludingThoseNotMatchingAnExc } @Test - public void shouldRetrieveSequenceOfRequestsReceivedIncludingThoseNotMatchingAnException() { + public void shouldVerifySequenceOfRequestsNotReceived() { + // when + mockServerClient.when(request().withPath(calculatePath("some_path.*")), exactly(6)).respond(response().withBody("some_body")); + + // then + assertEquals( + response("some_body"), + makeRequest( + request().withPath(calculatePath("some_path_one")), + headersToIgnore) + ); + assertEquals( + response("some_body"), + makeRequest( + request().withPath(calculatePath("some_path_two")), + headersToIgnore) + ); + assertEquals( + response("some_body"), + makeRequest( + request().withPath(calculatePath("some_path_three")), + headersToIgnore) + ); + try { + mockServerClient.verify(request(calculatePath("some_path_two")), request(calculatePath("some_path_one"))); + fail(); + } catch (AssertionError ae) { + assertThat(ae.getMessage(), startsWith("Request sequence not found, expected:<[ {" + NEW_LINE + + " \"path\" : \"" + calculatePath("some_path_two") + "\"" + NEW_LINE + + "}, {" + NEW_LINE + + " \"path\" : \"" + calculatePath("some_path_one") + "\"" + NEW_LINE + + "} ]> but was:<[ {" + NEW_LINE + + " \"method\" : \"GET\"," + NEW_LINE + + " \"path\" : \"" + calculatePath("some_path_one") + "\"," + NEW_LINE)); + } + try { + mockServerClient.verify(request(calculatePath("some_path_three")), request(calculatePath("some_path_two"))); + fail(); + } catch (AssertionError ae) { + assertThat(ae.getMessage(), startsWith("Request sequence not found, expected:<[ {" + NEW_LINE + + " \"path\" : \"" + calculatePath("some_path_three") + "\"" + NEW_LINE + + "}, {" + NEW_LINE + + " \"path\" : \"" + calculatePath("some_path_two") + "\"" + NEW_LINE + + "} ]> but was:<[ {" + NEW_LINE + + " \"method\" : \"GET\"," + NEW_LINE + + " \"path\" : \"" + calculatePath("some_path_one") + "\"," + NEW_LINE)); + } + try { + mockServerClient.verify(request(calculatePath("some_path_four"))); + fail(); + } catch (AssertionError ae) { + assertThat(ae.getMessage(), startsWith("Request sequence not found, expected:<[ {" + NEW_LINE + + " \"path\" : \"" + calculatePath("some_path_four") + "\"" + NEW_LINE + + "} ]> but was:<[ {" + NEW_LINE + + " \"method\" : \"GET\"," + NEW_LINE + + " \"path\" : \"" + calculatePath("some_path_one") + "\"," + NEW_LINE)); + } + } + + @Test + public void shouldRetrieveRecordedRequests() { // when mockServerClient.when(request().withPath(calculatePath("some_path.*")), exactly(4)).respond(response().withBody("some_body")); assertEquals( @@ -4927,20 +4990,20 @@ public void shouldRetrieveSequenceOfRequestsReceivedIncludingThoseNotMatchingAnE ); // then - verifyRequestMatches( + verifyRequestsMatches( mockServerClient.retrieveRecordedRequests(request().withPath(calculatePath("some_path.*"))), request(calculatePath("some_path_one")), request(calculatePath("some_path_three")) ); - verifyRequestMatches( + verifyRequestsMatches( mockServerClient.retrieveRecordedRequests(request()), request(calculatePath("some_path_one")), request(calculatePath("not_found")), request(calculatePath("some_path_three")) ); - verifyRequestMatches( + verifyRequestsMatches( mockServerClient.retrieveRecordedRequests(null), request(calculatePath("some_path_one")), request(calculatePath("not_found")), @@ -4949,7 +5012,52 @@ public void shouldRetrieveSequenceOfRequestsReceivedIncludingThoseNotMatchingAnE } @Test - public void shouldRetrieveSequenceOfExpectationsSetup() { + 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")), + headersToIgnore) + ); + assertEquals( + notFoundResponse(), + makeRequest( + request().withPath(calculatePath("not_found")), + headersToIgnore) + ); + assertEquals( + response("some_body"), + makeRequest( + request().withPath(calculatePath("some_path_three")), + headersToIgnore) + ); + + // then + verifyRequestsMatches( + new HttpRequestSerializer().deserializeArray(mockServerClient.retrieveRecordedRequests(request().withPath(calculatePath("some_path.*")), Format.JSON)), + request(calculatePath("some_path_one")), + request(calculatePath("some_path_three")) + ); + + verifyRequestsMatches( + new HttpRequestSerializer().deserializeArray(mockServerClient.retrieveRecordedRequests(request(), Format.JSON)), + request(calculatePath("some_path_one")), + request(calculatePath("not_found")), + request(calculatePath("some_path_three")) + ); + + verifyRequestsMatches( + new HttpRequestSerializer().deserializeArray(mockServerClient.retrieveRecordedRequests(null, Format.JSON)), + request(calculatePath("some_path_one")), + request(calculatePath("not_found")), + request(calculatePath("some_path_three")) + ); + } + + @Test + public void shouldRetrieveActiveExpectations() { // when mockServerClient.when(request().withPath(calculatePath("some_path.*")), exactly(4)) .respond(response().withBody("some_body")); @@ -5001,62 +5109,246 @@ public void shouldRetrieveSequenceOfExpectationsSetup() { } @Test - public void shouldVerifySequenceOfRequestsNotReceived() { + public void shouldRetrieveActiveExpectationsAsJson() { // when - mockServerClient.when(request().withPath(calculatePath("some_path.*")), exactly(6)).respond(response().withBody("some_body")); + mockServerClient.when(request().withPath(calculatePath("some_path.*")), exactly(4)) + .respond(response().withBody("some_body")); + mockServerClient.when(request().withPath(calculatePath("some_path.*"))) + .respond(response().withBody("some_body")); + mockServerClient.when(request().withPath(calculatePath("some_other_path"))) + .respond(response().withBody("some_other_body")); + mockServerClient.when(request().withPath(calculatePath("some_forward_path"))) + .forward(forward()); // then - assertEquals( - response("some_body"), - makeRequest( - request().withPath(calculatePath("some_path_one")), - headersToIgnore) + assertThat( + mockServerClient.retrieveActiveExpectations(request().withPath(calculatePath("some_path.*")), Format.JSON), + is(new ExpectationSerializer().serialize(Arrays.asList( + new Expectation(request().withPath(calculatePath("some_path.*")), exactly(4), TimeToLive.unlimited()) + .thenRespond(response().withBody("some_body")), + expectation(request().withPath(calculatePath("some_path.*"))) + .thenRespond(response().withBody("some_body")) + ))) ); - assertEquals( - response("some_body"), - makeRequest( - request().withPath(calculatePath("some_path_two")), - headersToIgnore) + + assertThat( + mockServerClient.retrieveActiveExpectations(null, Format.JSON), + is(new ExpectationSerializer().serialize(Arrays.asList( + new Expectation(request().withPath(calculatePath("some_path.*")), exactly(4), TimeToLive.unlimited()) + .thenRespond(response().withBody("some_body")), + expectation(request().withPath(calculatePath("some_path.*"))) + .thenRespond(response().withBody("some_body")), + expectation(request().withPath(calculatePath("some_other_path"))) + .thenRespond(response().withBody("some_other_body")), + expectation(request().withPath(calculatePath("some_forward_path"))) + .thenForward(forward()) + ))) ); - assertEquals( - response("some_body"), - makeRequest( - request().withPath(calculatePath("some_path_three")), - headersToIgnore) + + assertThat( + mockServerClient.retrieveActiveExpectations(request(), Format.JSON), + is(new ExpectationSerializer().serialize(Arrays.asList( + new Expectation(request().withPath(calculatePath("some_path.*")), exactly(4), TimeToLive.unlimited()) + .thenRespond(response().withBody("some_body")), + expectation(request().withPath(calculatePath("some_path.*"))) + .thenRespond(response().withBody("some_body")), + expectation(request().withPath(calculatePath("some_other_path"))) + .thenRespond(response().withBody("some_other_body")), + expectation(request().withPath(calculatePath("some_forward_path"))) + .thenForward(forward()) + ))) ); + } + + @Test + public void shouldRetrieveActiveExpectationsAsJava() { + // when + mockServerClient.when(request().withPath(calculatePath("some_path.*")), exactly(4)) + .respond(response().withBody("some_body")); + mockServerClient.when(request().withPath(calculatePath("some_path.*"))) + .respond(response().withBody("some_body")); + mockServerClient.when(request().withPath(calculatePath("some_other_path"))) + .respond(response().withBody("some_other_body")); + mockServerClient.when(request().withPath(calculatePath("some_forward_path"))) + .forward(forward()); + + // then + assertThat( + mockServerClient.retrieveActiveExpectations(request().withPath(calculatePath("some_path.*")), Format.JAVA), + is(new ExpectationToJavaSerializer().serialize(Arrays.asList( + new Expectation(request().withPath(calculatePath("some_path.*")), exactly(4), TimeToLive.unlimited()) + .thenRespond(response().withBody("some_body")), + expectation(request().withPath(calculatePath("some_path.*"))) + .thenRespond(response().withBody("some_body")) + ))) + ); + + assertThat( + mockServerClient.retrieveActiveExpectations(null, Format.JAVA), + is(new ExpectationToJavaSerializer().serialize(Arrays.asList( + new Expectation(request().withPath(calculatePath("some_path.*")), exactly(4), TimeToLive.unlimited()) + .thenRespond(response().withBody("some_body")), + expectation(request().withPath(calculatePath("some_path.*"))) + .thenRespond(response().withBody("some_body")), + expectation(request().withPath(calculatePath("some_other_path"))) + .thenRespond(response().withBody("some_other_body")), + expectation(request().withPath(calculatePath("some_forward_path"))) + .thenForward(forward()) + ))) + ); + + assertThat( + mockServerClient.retrieveActiveExpectations(request(), Format.JAVA), + is(new ExpectationToJavaSerializer().serialize(Arrays.asList( + new Expectation(request().withPath(calculatePath("some_path.*")), exactly(4), TimeToLive.unlimited()) + .thenRespond(response().withBody("some_body")), + expectation(request().withPath(calculatePath("some_path.*"))) + .thenRespond(response().withBody("some_body")), + expectation(request().withPath(calculatePath("some_other_path"))) + .thenRespond(response().withBody("some_other_body")), + expectation(request().withPath(calculatePath("some_forward_path"))) + .thenForward(forward()) + ))) + ); + } + + @Test + public void shouldRetrieveRecordedExpectations() { + // when + int testServerHttpPort = PortFactory.findFreePort(); + EchoServer secureEchoServer = new EchoServer(testServerHttpPort, false); try { - mockServerClient.verify(request(calculatePath("some_path_two")), request(calculatePath("some_path_one"))); - fail(); - } catch (AssertionError ae) { - assertThat(ae.getMessage(), startsWith("Request sequence not found, expected:<[ {" + NEW_LINE + - " \"path\" : \"" + calculatePath("some_path_two") + "\"" + NEW_LINE + - "}, {" + NEW_LINE + - " \"path\" : \"" + calculatePath("some_path_one") + "\"" + NEW_LINE + - "} ]> but was:<[ {" + NEW_LINE + - " \"method\" : \"GET\"," + NEW_LINE + - " \"path\" : \"" + calculatePath("some_path_one") + "\"," + NEW_LINE)); - } - try { - mockServerClient.verify(request(calculatePath("some_path_three")), request(calculatePath("some_path_two"))); - fail(); - } catch (AssertionError ae) { - assertThat(ae.getMessage(), startsWith("Request sequence not found, expected:<[ {" + NEW_LINE + - " \"path\" : \"" + calculatePath("some_path_three") + "\"" + NEW_LINE + - "}, {" + NEW_LINE + - " \"path\" : \"" + calculatePath("some_path_two") + "\"" + NEW_LINE + - "} ]> but was:<[ {" + NEW_LINE + - " \"method\" : \"GET\"," + NEW_LINE + - " \"path\" : \"" + calculatePath("some_path_one") + "\"," + NEW_LINE)); + mockServerClient.when(request().withPath(calculatePath("some_path.*")), exactly(4)).forward( + forward() + .withHost("127.0.0.1") + .withPort(testServerHttpPort) + ); + assertEquals( + response("some_body_one"), + makeRequest( + request().withPath(calculatePath("some_path_one")).withBody("some_body_one"), + headersToIgnore + ) + ); + assertEquals( + response("some_body_three"), + makeRequest( + request().withPath(calculatePath("some_path_three")).withBody("some_body_three"), + headersToIgnore + ) + ); + + // then + Expectation[] recordedExpectations = mockServerClient.retrieveRecordedExpectations(request().withPath(calculatePath("some_path_one"))); + assertThat(recordedExpectations.length, is(1)); + verifyRequestsMatches( + new HttpRequest[]{ + recordedExpectations[0].getHttpRequest() + }, + request(calculatePath("some_path_one")).withBody("some_body_one") + ); + assertThat(recordedExpectations[0].getHttpResponse().getBodyAsString(), is("some_body_one")); + // and + recordedExpectations = mockServerClient.retrieveRecordedExpectations(request()); + assertThat(recordedExpectations.length, is(2)); + verifyRequestsMatches( + new HttpRequest[]{ + recordedExpectations[0].getHttpRequest(), + recordedExpectations[1].getHttpRequest() + }, + request(calculatePath("some_path_one")).withBody("some_body_one"), + request(calculatePath("some_path_three")).withBody("some_body_three") + ); + assertThat(recordedExpectations[0].getHttpResponse().getBodyAsString(), is("some_body_one")); + assertThat(recordedExpectations[1].getHttpResponse().getBodyAsString(), is("some_body_three")); + // and + recordedExpectations = mockServerClient.retrieveRecordedExpectations(null); + assertThat(recordedExpectations.length, is(2)); + verifyRequestsMatches( + new HttpRequest[]{ + recordedExpectations[0].getHttpRequest(), + recordedExpectations[1].getHttpRequest() + }, + request(calculatePath("some_path_one")).withBody("some_body_one"), + request(calculatePath("some_path_three")).withBody("some_body_three") + ); + assertThat(recordedExpectations[0].getHttpResponse().getBodyAsString(), is("some_body_one")); + assertThat(recordedExpectations[1].getHttpResponse().getBodyAsString(), is("some_body_three")); + } finally { + secureEchoServer.stop(); } + } + + @Test + public void shouldRetrieveRecordedExpectationsAsJson() { + // when + int testServerHttpPort = PortFactory.findFreePort(); + EchoServer secureEchoServer = new EchoServer(testServerHttpPort, false); try { - mockServerClient.verify(request(calculatePath("some_path_four"))); - fail(); - } catch (AssertionError ae) { - assertThat(ae.getMessage(), startsWith("Request sequence not found, expected:<[ {" + NEW_LINE + - " \"path\" : \"" + calculatePath("some_path_four") + "\"" + NEW_LINE + - "} ]> but was:<[ {" + NEW_LINE + - " \"method\" : \"GET\"," + NEW_LINE + - " \"path\" : \"" + calculatePath("some_path_one") + "\"," + NEW_LINE)); + mockServerClient.when(request().withPath(calculatePath("some_path.*")), exactly(4)).forward( + forward() + .withHost("127.0.0.1") + .withPort(testServerHttpPort) + ); + assertEquals( + response("some_body_one"), + makeRequest( + request().withPath(calculatePath("some_path_one")).withBody("some_body_one"), + headersToIgnore + ) + ); + assertEquals( + response("some_body_three"), + makeRequest( + request().withPath(calculatePath("some_path_three")).withBody("some_body_three"), + headersToIgnore + ) + ); + + // then + Expectation[] recordedExpectations = new ExpectationSerializer().deserializeArray( + mockServerClient.retrieveRecordedExpectations(request().withPath(calculatePath("some_path_one")), Format.JSON) + ); + assertThat(recordedExpectations.length, is(1)); + verifyRequestsMatches( + new HttpRequest[]{ + recordedExpectations[0].getHttpRequest() + }, + request(calculatePath("some_path_one")).withBody("some_body_one") + ); + assertThat(recordedExpectations[0].getHttpResponse().getBodyAsString(), is("some_body_one")); + // and + recordedExpectations = new ExpectationSerializer().deserializeArray( + mockServerClient.retrieveRecordedExpectations(request(), Format.JSON) + ); + assertThat(recordedExpectations.length, is(2)); + verifyRequestsMatches( + new HttpRequest[]{ + recordedExpectations[0].getHttpRequest(), + recordedExpectations[1].getHttpRequest() + }, + request(calculatePath("some_path_one")).withBody("some_body_one"), + request(calculatePath("some_path_three")).withBody("some_body_three") + ); + assertThat(recordedExpectations[0].getHttpResponse().getBodyAsString(), is("some_body_one")); + assertThat(recordedExpectations[1].getHttpResponse().getBodyAsString(), is("some_body_three")); + // and + recordedExpectations = new ExpectationSerializer().deserializeArray( + mockServerClient.retrieveRecordedExpectations(null, Format.JSON) + ); + assertThat(recordedExpectations.length, is(2)); + verifyRequestsMatches( + new HttpRequest[]{ + recordedExpectations[0].getHttpRequest(), + recordedExpectations[1].getHttpRequest() + }, + request(calculatePath("some_path_one")).withBody("some_body_one"), + request(calculatePath("some_path_three")).withBody("some_body_three") + ); + assertThat(recordedExpectations[0].getHttpResponse().getBodyAsString(), is("some_body_one")); + assertThat(recordedExpectations[1].getHttpResponse().getBodyAsString(), is("some_body_three")); + } finally { + secureEchoServer.stop(); } } @@ -5125,7 +5417,7 @@ public void shouldClearExpectationsAndLogs() { ); // and then - request log cleared - verifyRequestMatches( + verifyRequestsMatches( mockServerClient.retrieveRecordedRequests(null), request(calculatePath("some_path2")) ); @@ -5197,7 +5489,7 @@ public void shouldClearExpectationsOnly() { .clear( request() .withPath(calculatePath("some_path1")), - HttpStateHandler.ClearType.EXPECTATIONS + ClearType.EXPECTATIONS ); // then - expectations cleared @@ -5216,7 +5508,7 @@ public void shouldClearExpectationsOnly() { ); // and then - request log not cleared - verifyRequestMatches( + verifyRequestsMatches( mockServerClient.retrieveRecordedRequests(null), request(calculatePath("some_path1")), request(calculatePath("some_path2")) @@ -5270,7 +5562,7 @@ public void shouldClearLogsOnly() { .clear( request() .withPath(calculatePath("some_path1")), - HttpStateHandler.ClearType.LOG + ClearType.LOG ); // then - expectations cleared @@ -5297,7 +5589,7 @@ public void shouldClearLogsOnly() { ); // and then - request log cleared - verifyRequestMatches( + verifyRequestsMatches( mockServerClient.retrieveRecordedRequests(null), request(calculatePath("some_path2")) ); @@ -5907,7 +6199,7 @@ public void shouldReturnErrorForInvalidRequest() { " - instance type (integer) does not match any allowed primitive type (allowed: [\"string\"]) for field \"/path\"")); } - protected void verifyRequestMatches(HttpRequest[] httpRequests, HttpRequest... httpRequestMatchers) { + protected void verifyRequestsMatches(HttpRequest[] httpRequests, HttpRequest... httpRequestMatchers) { if (httpRequests.length != httpRequestMatchers.length) { throw new AssertionError("Number of request matchers does not match number of requests, expected:<" + httpRequestMatchers.length + "> but was:<" + httpRequests.length + ">"); } else { diff --git a/mockserver-netty/src/test/java/org/mockserver/mockserver/MockServerHandlerTest.java b/mockserver-netty/src/test/java/org/mockserver/mockserver/MockServerHandlerTest.java index 2f6c31d5c..e4d331d9a 100644 --- a/mockserver-netty/src/test/java/org/mockserver/mockserver/MockServerHandlerTest.java +++ b/mockserver-netty/src/test/java/org/mockserver/mockserver/MockServerHandlerTest.java @@ -15,9 +15,9 @@ import org.mockserver.mock.Expectation; import org.mockserver.mock.HttpStateHandler; import org.mockserver.mock.action.ActionHandler; -import org.mockserver.mockserver.callback.WebSocketClientRegistry; import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpResponse; +import org.mockserver.model.RetrieveType; import org.mockserver.responsewriter.ResponseWriter; import java.util.Arrays; @@ -179,7 +179,7 @@ public void shouldRetrieveRecordedExpectations() { )); HttpRequest expectationRetrieveExpectationsRequest = request("/retrieve") .withMethod("PUT") - .withQueryStringParameter("type", HttpStateHandler.RetrieveType.RECORDED_EXPECTATIONS.name()) + .withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name()) .withBody( httpRequestSerializer.serialize(request("request_one")) ); @@ -216,7 +216,7 @@ public void shouldRetrieveActiveExpectations() { httpStateHandler.add(expectationOne); HttpRequest expectationRetrieveExpectationsRequest = request("/retrieve") .withMethod("PUT") - .withQueryStringParameter("type", HttpStateHandler.RetrieveType.ACTIVE_EXPECTATIONS.name()) + .withQueryStringParameter("type", RetrieveType.ACTIVE_EXPECTATIONS.name()) .withBody( httpRequestSerializer.serialize(request("request_one")) ); diff --git a/mockserver-netty/src/test/java/org/mockserver/proxy/http/HttpProxyHandlerTest.java b/mockserver-netty/src/test/java/org/mockserver/proxy/http/HttpProxyHandlerTest.java index 142b4377a..d76384cdb 100644 --- a/mockserver-netty/src/test/java/org/mockserver/proxy/http/HttpProxyHandlerTest.java +++ b/mockserver-netty/src/test/java/org/mockserver/proxy/http/HttpProxyHandlerTest.java @@ -18,6 +18,7 @@ import org.mockserver.mock.HttpStateHandler; import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpResponse; +import org.mockserver.model.RetrieveType; import org.mockserver.proxy.Proxy; import java.net.InetSocketAddress; @@ -181,7 +182,7 @@ public void shouldRetrieveRecordedExpectations() { )); HttpRequest expectationRetrieveExpectationsRequest = request("/retrieve") .withMethod("PUT") - .withQueryStringParameter("type", HttpStateHandler.RetrieveType.RECORDED_EXPECTATIONS.name()) + .withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name()) .withBody( httpRequestSerializer.serialize(request("request_one")) ); diff --git a/mockserver-proxy-war/src/test/java/org/mockserver/proxy/ProxyServletTest.java b/mockserver-proxy-war/src/test/java/org/mockserver/proxy/ProxyServletTest.java index fb0b4cef0..963996abf 100644 --- a/mockserver-proxy-war/src/test/java/org/mockserver/proxy/ProxyServletTest.java +++ b/mockserver-proxy-war/src/test/java/org/mockserver/proxy/ProxyServletTest.java @@ -14,6 +14,7 @@ import org.mockserver.mock.Expectation; import org.mockserver.mock.HttpStateHandler; import org.mockserver.model.HttpRequest; +import org.mockserver.model.RetrieveType; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; @@ -179,7 +180,7 @@ public void shouldRetrieveRecordedExpectations() { "/retrieve", httpRequestSerializer.serialize(request("request_one")) ); - expectationRetrieveExpectationsRequest.setQueryString("type=" + HttpStateHandler.RetrieveType.RECORDED_EXPECTATIONS.name()); + expectationRetrieveExpectationsRequest.setQueryString("type=" + RetrieveType.RECORDED_EXPECTATIONS.name()); // when proxyServlet.service(expectationRetrieveExpectationsRequest, response); diff --git a/mockserver-war/src/test/java/org/mockserver/server/MockServerServletTest.java b/mockserver-war/src/test/java/org/mockserver/server/MockServerServletTest.java index 9fb7f758b..77c44bcba 100644 --- a/mockserver-war/src/test/java/org/mockserver/server/MockServerServletTest.java +++ b/mockserver-war/src/test/java/org/mockserver/server/MockServerServletTest.java @@ -12,6 +12,7 @@ import org.mockserver.mock.Expectation; import org.mockserver.mock.HttpStateHandler; import org.mockserver.mock.action.ActionHandler; +import org.mockserver.model.RetrieveType; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; @@ -174,7 +175,7 @@ public void shouldRetrieveRecordedExpectations() { "/retrieve", httpRequestSerializer.serialize(request("request_one")) ); - expectationRetrieveExpectationsRequest.setQueryString("type=" + HttpStateHandler.RetrieveType.RECORDED_EXPECTATIONS.name()); + expectationRetrieveExpectationsRequest.setQueryString("type=" + RetrieveType.RECORDED_EXPECTATIONS.name()); // when mockServerServlet.service(expectationRetrieveExpectationsRequest, response); @@ -213,7 +214,7 @@ public void shouldRetrieveActiveExpectations() { "/retrieve", httpRequestSerializer.serialize(request("request_one")) ); - expectationRetrieveExpectationsRequest.setQueryString("type=" + HttpStateHandler.RetrieveType.ACTIVE_EXPECTATIONS.name()); + expectationRetrieveExpectationsRequest.setQueryString("type=" + RetrieveType.ACTIVE_EXPECTATIONS.name()); // when mockServerServlet.service(expectationRetrieveExpectationsRequest, response);