Permalink
Browse files

initial work to move to Netty

  • Loading branch information...
1 parent caea98c commit 40ecdf5886142fce5a797f701da6b57e4949ed52 @jamesdbloom committed Jan 30, 2014
Showing with 7,825 additions and 2,977 deletions.
  1. +12 −11 build.sh
  2. +1 −1 git-hooks/pre-commit
  3. +2 −1 gradle.properties
  4. +3 −0 mockserver-client/build.gradle
  5. +7 −0 mockserver-client/pom.xml
  6. +40 −11 mockserver-client/src/main/java/org/mockserver/client/http/ApacheHttpClient.java
  7. +9 −9 mockserver-client/src/main/java/org/mockserver/client/proxy/ProxyClient.java
  8. +76 −1 mockserver-client/src/main/java/org/mockserver/client/server/MockServerClient.java
  9. +18 −15 ...r/mappers/{jetty/HttpClientResponseMapper.java → ApacheHttpClientToMockServerResponseMapper.java}
  10. +17 −6 mockserver-client/src/test/java/org/mockserver/client/proxy/ProxyClientTest.java
  11. +19 −15 mockserver-client/src/test/java/org/mockserver/client/server/MockServerClientTest.java
  12. +6 −6 ...s/jetty/{HttpClientResponseMapperTest.java → ApacheHttpClientToMockServerResponseMapperTest.java}
  13. +4 −0 mockserver-core/build.gradle
  14. +14 −0 mockserver-core/pom.xml
  15. +19 −26 mockserver-core/src/main/java/org/mockserver/client/serialization/ExpectationSerializer.java
  16. +1 −17 mockserver-core/src/main/java/org/mockserver/client/serialization/HttpRequestSerializer.java
  17. +130 −0 mockserver-core/src/main/java/org/mockserver/client/serialization/ObjectMapperFactory.java
  18. +38 −0 mockserver-core/src/main/java/org/mockserver/client/serialization/model/BodyDTO.java
  19. +22 −33 mockserver-core/src/main/java/org/mockserver/client/serialization/model/HttpRequestDTO.java
  20. +40 −0 mockserver-core/src/main/java/org/mockserver/client/serialization/model/ParameterBodyDTO.java
  21. +27 −0 mockserver-core/src/main/java/org/mockserver/client/serialization/model/StringBodyDTO.java
  22. +74 −0 mockserver-core/src/main/java/org/mockserver/collections/CaseInsensitiveRegexHashMap.java
  23. +203 −0 mockserver-core/src/main/java/org/mockserver/collections/CaseInsensitiveRegexMultiMap.java
  24. +5 −1 mockserver-core/src/main/java/org/mockserver/collections/CircularMultiMap.java
  25. +10 −11 ...rg/mockserver/mappers/{HttpServletRequestMapper.java → HttpServletToMockServerRequestMapper.java}
  26. +11 −9 .../mockserver/mappers/{HttpServletResponseMapper.java → MockServerToHttpServletResponseMapper.java}
  27. +9 −0 mockserver-core/src/main/java/org/mockserver/matchers/BodyMatcher.java
  28. +56 −0 mockserver-core/src/main/java/org/mockserver/matchers/ExactStringMatcher.java
  29. +66 −46 mockserver-core/src/main/java/org/mockserver/matchers/HttpRequestMatcher.java
  30. +10 −51 mockserver-core/src/main/java/org/mockserver/matchers/MapMatcher.java
  31. +3 −2 mockserver-core/src/main/java/org/mockserver/matchers/MatcherBuilder.java
  32. +57 −0 mockserver-core/src/main/java/org/mockserver/matchers/ParameterStringMatcher.java
  33. +83 −0 mockserver-core/src/main/java/org/mockserver/matchers/RegexStringMatcher.java
  34. +0 −87 mockserver-core/src/main/java/org/mockserver/matchers/StringMatcher.java
  35. +68 −0 mockserver-core/src/main/java/org/mockserver/matchers/XPathStringMatcher.java
  36. +11 −17 mockserver-core/src/main/java/org/mockserver/mock/MockServer.java
  37. +24 −0 mockserver-core/src/main/java/org/mockserver/model/Body.java
  38. +138 −53 mockserver-core/src/main/java/org/mockserver/model/HttpRequest.java
  39. +74 −25 mockserver-core/src/main/java/org/mockserver/model/HttpResponse.java
  40. +22 −9 mockserver-core/src/main/java/org/mockserver/model/KeyToMultiValue.java
  41. +61 −0 mockserver-core/src/main/java/org/mockserver/model/ParameterBody.java
  42. +23 −0 mockserver-core/src/main/java/org/mockserver/model/StringBody.java
  43. +7 −7 mockserver-core/src/main/java/org/mockserver/proxy/filters/LogFilter.java
  44. +219 −62 ...-core/src/test/java/org/mockserver/client/serialization/ExpectationSerializerIntegrationTest.java
  45. +31 −115 mockserver-core/src/test/java/org/mockserver/client/serialization/ExpectationSerializerTest.java
  46. +37 −40 ...-core/src/test/java/org/mockserver/client/serialization/HttpRequestSerializerIntegrationTest.java
  47. +10 −17 mockserver-core/src/test/java/org/mockserver/client/serialization/HttpRequestSerializerTest.java
  48. +180 −0 mockserver-core/src/test/java/org/mockserver/collections/CaseInsensitiveRegexHashMapTest.java
  49. +316 −0 mockserver-core/src/test/java/org/mockserver/collections/CaseInsensitiveRegexMultiMapTest.java
  50. +1 −1 mockserver-core/src/test/java/org/mockserver/collections/CircularMultiMapTest.java
  51. +18 −12 ...erver/mappers/{HttpServletRequestMapperTest.java → HttpServletToMockServerRequestMapperTest.java}
  52. +8 −5 ...ver/mappers/{HttpServletResponseMapperTest.java → MockServerToHttpServletResponseMapperTest.java}
  53. +42 −0 mockserver-core/src/test/java/org/mockserver/matchers/ExactStringMatcherTest.java
  54. +103 −34 mockserver-core/src/test/java/org/mockserver/matchers/HttpRequestMatcherTest.java
  55. +100 −5 mockserver-core/src/test/java/org/mockserver/matchers/MapMatcherTest.java
  56. +35 −19 mockserver-core/src/test/java/org/mockserver/matchers/MatcherBuilderTest.java
  57. +79 −0 mockserver-core/src/test/java/org/mockserver/matchers/ParameterStringMatcherTest.java
  58. +67 −0 mockserver-core/src/test/java/org/mockserver/matchers/RegexStringMatcherTest.java
  59. +0 −93 mockserver-core/src/test/java/org/mockserver/matchers/StringMatcherTest.java
  60. +58 −0 mockserver-core/src/test/java/org/mockserver/matchers/XPathStringMatcherTest.java
  61. +198 −79 mockserver-core/src/test/java/org/mockserver/mock/MockServerBasicResponsesTest.java
  62. +5 −8 mockserver-core/src/test/java/org/mockserver/model/HttpRequestTest.java
  63. +55 −0 mockserver-core/src/test/java/org/mockserver/test/Assert.java
  64. +18 −1 mockserver-examples/build.gradle
  65. +7 −5 mockserver-examples/pom.xml
  66. +1 −1 ...server-examples/src/test/java/org/mockserver/web/controller/BooksPageEndToEndIntegrationTest.java
  67. +2 −2 mockserver-examples/src/test/java/org/mockserver/web/controller/BooksPageIntegrationTest.java
  68. +13 −1 mockserver-integration-testing/build.gradle
  69. +22 −0 mockserver-integration-testing/pom.xml
  70. +711 −383 ...-testing/src/main/java/org/mockserver/integration/server/AbstractClientServerIntegrationTest.java
  71. +9 −8 mockserver-integration-testing/src/main/java/org/mockserver/test/Assert.java
  72. +8 −8 mockserver-integration-testing/src/test/java/org/mockserver/test/AssertTest.java
  73. +1 −1 mockserver-javascript/src/main/javascript/mockServerClient.js
  74. +11 −0 mockserver-jetty/build.gradle
  75. +15 −3 mockserver-jetty/pom.xml
  76. +2 −8 mockserver-maven-plugin/pom.xml
  77. +2 −6 mockserver-maven-plugin/src/integration-tests/mockserver-run-from-command-line/pom.xml
  78. +2 −6 mockserver-maven-plugin/src/integration-tests/mockserver-run-stop-forked-from-command-line/pom.xml
  79. +6 −7 mockserver-maven-plugin/src/integration-tests/mockserver-starts-stops-forked-server-from-pom/pom.xml
  80. +6 −3 ...s-forked-server-from-pom/src/test/java/org/mockserver/ClientServerMavenPluginIntegrationTest.java
  81. +6 −7 ...ver-maven-plugin/src/integration-tests/mockserver-starts-stops-server-from-pom-explicitly/pom.xml
  82. +2 −6 mockserver-maven-plugin/src/integration-tests/proxy-run-from-command-line/pom.xml
  83. +2 −6 mockserver-maven-plugin/src/integration-tests/proxy-run-stop-forked-from-command-line/pom.xml
  84. +11 −12 mockserver-maven-plugin/src/integration-tests/proxy-starts-stops-forked-server-from-pom/pom.xml
  85. +11 −0 ...ps-forked-server-from-pom/src/test/java/org/mockserver/ClientProxyMavenPluginIntegrationTest.java
  86. +7 −8 mockserver-maven-plugin/src/integration-tests/proxy-starts-stops-server-from-pom-explicitly/pom.xml
  87. +2 −7 mockserver-maven-plugin/src/integration-tests/skip/pom.xml
  88. +1 −1 mockserver-maven-plugin/src/main/java/org/mockserver/maven/MockServerRunForkedMojo.java
  89. +1 −1 mockserver-maven-plugin/src/test/java/org/mockserver/maven/MockServerRunForkedMojoTest.java
  90. +20 −0 mockserver-netty/build.gradle
  91. +75 −0 mockserver-netty/pom.xml
  92. +57 −0 mockserver-netty/src/main/java/org/mockserver/mappers/MockServerToNettyResponseMapper.java
  93. +73 −0 mockserver-netty/src/main/java/org/mockserver/mappers/NettyToMockServerRequestMapper.java
  94. +55 −0 mockserver-netty/src/main/java/org/mockserver/mappers/NettyToMockServerResponseMapper.java
  95. +57 −0 mockserver-netty/src/main/java/org/mockserver/model/NettyHttpRequest.java
  96. +29 −0 mockserver-netty/src/main/java/org/mockserver/model/NettyHttpResponse.java
  97. +105 −0 mockserver-netty/src/main/java/org/mockserver/netty/HttpSnoopClient.java
  98. +67 −0 mockserver-netty/src/main/java/org/mockserver/netty/HttpSnoopClientHandler.java
  99. +62 −0 mockserver-netty/src/main/java/org/mockserver/netty/HttpSnoopClientInitializer.java
  100. +63 −0 mockserver-netty/src/main/java/org/mockserver/netty/HttpSnoopServer.java
  101. +182 −0 mockserver-netty/src/main/java/org/mockserver/netty/HttpSnoopServerHandler.java
  102. +47 −0 mockserver-netty/src/main/java/org/mockserver/netty/HttpSnoopServerInitializer.java
  103. +162 −0 mockserver-netty/src/main/java/org/mockserver/netty/mockserver/MockServerHandler.java
  104. +65 −0 mockserver-netty/src/main/java/org/mockserver/netty/mockserver/MockServerInitializer.java
  105. +117 −0 mockserver-netty/src/main/java/org/mockserver/netty/mockserver/NettyMockServer.java
  106. +294 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/LogHandler.java
  107. +257 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/LoggingHandler.java
  108. +64 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/direct/BasicHttpDecoder.java
  109. +66 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/direct/DirectProxy.java
  110. +184 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/direct/DirectProxyDownstreamHandler.java
  111. +59 −0 ...erver-netty/src/main/java/org/mockserver/netty/proxy/direct/DirectProxyDownstreamInitializer.java
  112. +59 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/direct/DirectProxyInitializer.java
  113. +233 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/direct/DirectProxyUpstreamHandler.java
  114. +116 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/http/HttpProxy.java
  115. +224 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/http/HttpProxyHandler.java
  116. +64 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/http/HttpProxyInitializer.java
  117. +82 −0 ...erver-netty/src/main/java/org/mockserver/netty/proxy/http/connect/HexDumpProxyBackendHandler.java
  118. +124 −0 ...rver-netty/src/main/java/org/mockserver/netty/proxy/http/connect/HexDumpProxyFrontendHandler.java
  119. +46 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/socks/DirectClientHandler.java
  120. +37 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/socks/DirectClientInitializer.java
  121. +68 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/socks/RelayHandler.java
  122. +48 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/socks/SocksProxy.java
  123. +98 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/socks/SocksProxyConnectHandler.java
  124. +72 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/socks/SocksProxyHandler.java
  125. +33 −0 mockserver-netty/src/main/java/org/mockserver/netty/proxy/socks/SocksProxyInitializer.java
  126. +39 −0 mockserver-netty/src/main/resources/logback.xml
  127. +30 −25 ...est.java → mockserver-netty/src/test/java/org/mockserver/proxy/NettyHttpProxyIntegrationTest.java
  128. +44 −0 mockserver-netty/src/test/java/org/mockserver/server/ClientServerNettyIntegrationTest.java
  129. +6 −5 mockserver-proxy-war/build.gradle
  130. +1 −11 mockserver-proxy-war/pom.xml
  131. +6 −6 mockserver-proxy-war/src/main/java/org/mockserver/proxy/ProxyServlet.java
  132. +19 −19 mockserver-proxy-war/src/test/java/org/mockserver/proxy/ProxyServletTest.java
  133. +0 −43 mockserver-tomcat/build.gradle
  134. +0 −139 mockserver-tomcat/pom.xml
  135. +0 −126 mockserver-tomcat/src/main/java/org/mockserver/tomcat/cli/Main.java
  136. +0 −38 mockserver-tomcat/src/main/java/org/mockserver/tomcat/integration/ClientAndProxy.java
  137. +0 −34 mockserver-tomcat/src/main/java/org/mockserver/tomcat/integration/ClientAndServer.java
  138. +0 −102 mockserver-tomcat/src/main/java/org/mockserver/tomcat/proxy/ProxyRunner.java
  139. +0 −275 mockserver-tomcat/src/main/java/org/mockserver/tomcat/runner/AbstractRunner.java
  140. +0 −23 mockserver-tomcat/src/main/java/org/mockserver/tomcat/server/MockServerRunner.java
  141. +0 −114 mockserver-tomcat/src/test/java/org/mockserver/tomcat/cli/MainTest.java
  142. +0 −68 mockserver-tomcat/src/test/java/org/mockserver/tomcat/proxy/ProxyCommandLineIntegrationTest.java
  143. +0 −48 mockserver-tomcat/src/test/java/org/mockserver/tomcat/proxy/ProxyRunnerTest.java
  144. +0 −147 mockserver-tomcat/src/test/java/org/mockserver/tomcat/runner/AbstractRunnerTest.java
  145. +0 −42 mockserver-tomcat/src/test/java/org/mockserver/tomcat/server/ClientAndServerIntegrationTest.java
  146. +0 −51 ...erver-tomcat/src/test/java/org/mockserver/tomcat/server/MockServerCommandLineIntegrationTest.java
  147. +0 −42 mockserver-tomcat/src/test/java/org/mockserver/tomcat/server/MockServerRunnerIntegrationTest.java
  148. +0 −18 mockserver-tomcat/src/test/java/org/mockserver/tomcat/server/MockServerRunnerTest.java
  149. +4 −1 mockserver-vertx/build.gradle
  150. +8 −4 mockserver-vertx/pom.xml
  151. +10 −4 ...ava/org/mockserver/mappers/{HttpServerResponseMapper.java → MockServerToVertXResponseMapper.java}
  152. +14 −10 .../java/org/mockserver/mappers/{HttpServerRequestMapper.java → VertXToMockServerRequestMapper.java}
  153. +1 −1 mockserver-vertx/src/main/java/org/mockserver/mappers/vertx/HttpClientRequestMapper.java
  154. +6 −6 mockserver-vertx/src/main/java/org/mockserver/proxy/ProxyVertical.java
  155. +68 −20 mockserver-vertx/src/main/java/org/mockserver/server/MockServerVertical.java
  156. +1 −1 mockserver-vertx/src/test/java/org/mockserver/ClientServerVertxIntegrationTest.java
  157. +2 −2 ...mockserver/mappers/{HttpServerResponseMapperTest.java → MockServerToVertXResponseMapperTest.java}
  158. +22 −16 ...g/mockserver/mappers/{HttpServerRequestMapperTest.java → VertXToMockServerRequestMapperTest.java}
  159. +1 −1 ...s/vertx/{HttpClientResponseMapperTest.java → ApacheHttpClientToMockServerResponseMapperTest.java}
  160. +6 −6 mockserver-vertx/src/test/java/org/mockserver/proxy/ProxyVerticalTest.java
  161. +15 −11 mockserver-vertx/src/test/java/org/mockserver/server/MockServerVerticalTest.java
  162. +1 −1 mockserver-vertx/src/test/resources/logback-test.xml
  163. +6 −5 mockserver-war/build.gradle
  164. +0 −10 mockserver-war/pom.xml
  165. +30 −19 mockserver-war/src/main/java/org/mockserver/server/MockServerServlet.java
  166. +13 −13 mockserver-war/src/test/java/org/mockserver/server/MockServerServletTest.java
  167. +35 −7 pom.xml
  168. +12 −12 www.mock-server.com/index.html
View
@@ -1,20 +1,21 @@
#!/usr/bin/env bash
-# java 1.7 build
-export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home
-if [ -z "$1" ]
-then
- gradle clean test
- mvn clean install
-else
- mvn clean $1
-fi
-
# java 1.6 build
export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
if [ -z "$1" ]
then
- mvn clean install
+ mvn clean install -Pit
else
mvn clean $1
+fi
+
+if [ $? -eq 0 ]; then
+ # java 1.7 build
+ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home
+ if [ -z "$1" ]
+ then
+ gradle clean test && mvn clean install -Pit
+ else
+ mvn clean $1
+ fi
fi
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
-gradle clean compileTestJava && mvn clean install -Pit
+./build.sh
View
@@ -3,11 +3,12 @@
###################################
# mock-server
-mockServerVersion=2.3-SNAPSHOT
+mockServerVersion=2.5-SNAPSHOT
# http
servletVersion=3.0.1
jettyVersion=9.1.0.v20131115
+nettyVersion=4.0.15.Final
vertxVersion=2.1M2
tomcatVersion=7.0.47
apacheClientVersion=4.3.2
@@ -3,6 +3,9 @@ dependencies {
compile "org.apache.httpcomponents:httpclient:$apacheClientVersion"
compile "org.apache.httpcomponents:httpcore:$apacheCoreVersion"
+ // netty
+ compile "io.netty:netty-all:$nettyVersion"
+
// json
compile "org.codehaus.jackson:jackson-mapper-asl:$jacksonMapperAslVersion"
@@ -31,6 +31,13 @@
<artifactId>httpcore</artifactId>
</dependency>
+ <!-- Netty -->
+ <dependency>
+ <groupId>io.netty</groupId>
+ <artifactId>netty-all</artifactId>
+ <version>4.0.15.Final</version>
+ </dependency>
+
<!-- json (object serialisation) -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
@@ -1,11 +1,11 @@
package org.mockserver.client.http;
+import io.netty.handler.codec.http.QueryStringDecoder;
import org.apache.commons.io.Charsets;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHeaders;
import org.apache.http.client.CircularRedirectException;
-import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URLEncodedUtils;
@@ -22,11 +22,8 @@
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
-import org.mockserver.mappers.jetty.HttpClientResponseMapper;
-import org.mockserver.model.Cookie;
-import org.mockserver.model.Header;
-import org.mockserver.model.HttpRequest;
-import org.mockserver.model.HttpResponse;
+import org.mockserver.mappers.ApacheHttpClientToMockServerResponseMapper;
+import org.mockserver.model.*;
import org.mockserver.socket.SSLFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -47,7 +44,7 @@
public class ApacheHttpClient {
private static final Logger logger = LoggerFactory.getLogger(ApacheHttpClient.class);
- private static final HttpClientResponseMapper httpClientResponseMapper = new HttpClientResponseMapper();
+ private static final ApacheHttpClientToMockServerResponseMapper APACHE_TO_MOCK_SERVER_RESPONSE_MAPPER = new ApacheHttpClientToMockServerResponseMapper();
private final CloseableHttpClient httpClient;
public ApacheHttpClient() {
@@ -96,7 +93,7 @@ public String sendPUTRequest(String baseUri, String path, String body) {
public HttpResponse sendRequest(HttpRequest httpRequest) {
try {
- URI url = new URI(URLEncoder.encodeURL(httpRequest.getURL()));
+ URI url = buildUrl(httpRequest);
HttpMethod httpMethod = HttpMethod.parseString(httpRequest.getMethod());
if (logger.isDebugEnabled()) {
System.out.println(httpMethod + " => " + url);
@@ -137,12 +134,13 @@ public HttpResponse sendRequest(HttpRequest httpRequest) {
.writerWithDefaultPrettyPrinter()
.writeValueAsString(httpRequest));
}
+
if (proxiedRequest instanceof HttpEntityEnclosingRequest) {
- ((HttpEntityEnclosingRequest) proxiedRequest).setEntity(new StringEntity(httpRequest.getBody()));
+ ((HttpEntityEnclosingRequest) proxiedRequest).setEntity(new StringEntity((httpRequest.getBody() != null ? httpRequest.getBody().toString() : "")));
}
- return httpClientResponseMapper.mapHttpClientResponseToHttpResponse(this.httpClient.execute(proxiedRequest));
+ return APACHE_TO_MOCK_SERVER_RESPONSE_MAPPER.mapApacheHttpClientResponseToMockServerResponse(this.httpClient.execute(proxiedRequest));
} catch (IOException ioe) {
- if(ioe.getCause() instanceof CircularRedirectException) {
+ if (ioe.getCause() instanceof CircularRedirectException) {
logger.debug("Circular redirect aborting request", ioe);
return new HttpResponse();
} else {
@@ -153,6 +151,37 @@ public HttpResponse sendRequest(HttpRequest httpRequest) {
}
}
+ private URI buildUrl(HttpRequest httpRequest) throws URISyntaxException {
+ URI url = new URI(URLEncoder.encodeURL(httpRequest.getURL()));
+ if (url.getQuery() != null) {
+ httpRequest.withQueryStringParameters(new QueryStringDecoder("?" + url.getQuery()).parameters());
+ }
+ StringBuilder queryString = new StringBuilder();
+ List<Parameter> queryStringParameters = httpRequest.getQueryStringParameters();
+ for (int i = 0; i < queryStringParameters.size(); i++) {
+ Parameter parameter = queryStringParameters.get(i);
+ if (parameter.getValues().isEmpty()) {
+ queryString.append(parameter.getName());
+ queryString.append('=');
+ } else {
+ List<String> values = parameter.getValues();
+ for (int j = 0; j < values.size(); j++) {
+ String value = values.get(j);
+ queryString.append(parameter.getName());
+ queryString.append('=');
+ queryString.append(value);
+ if (j < (values.size() - 1)) {
+ queryString.append('&');
+ }
+ }
+ }
+ if (i < (queryStringParameters.size() - 1)) {
+ queryString.append('&');
+ }
+ }
+ return new URI(url.getScheme(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), (queryString.toString().isEmpty() ? null : queryString.toString()), url.getFragment());
+ }
+
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
switch (httpMethod) {
case GET:
@@ -11,7 +11,7 @@
* @author jamesdbloom
*/
public class ProxyClient {
- private final String uri;
+ private final String uriBase;
private ApacheHttpClient apacheHttpClient;
private HttpRequestSerializer httpRequestSerializer = new HttpRequestSerializer();
private ExpectationSerializer expectationSerializer = new ExpectationSerializer();
@@ -25,7 +25,7 @@
* @param port the port for the proxy to communicate with
*/
public ProxyClient(String host, int port) {
- uri = "http://" + host + ":" + port;
+ uriBase = "http://" + host + ":" + port;
apacheHttpClient = new ApacheHttpClient();
}
@@ -44,7 +44,7 @@ public ProxyClient dumpToLogAsJSON() {
* @param httpRequest the http request that is matched against when deciding what to log if null all requests are logged
*/
public ProxyClient dumpToLogAsJSON(HttpRequest httpRequest) {
- apacheHttpClient.sendPUTRequest(uri, "/dumpToLog", httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "");
+ apacheHttpClient.sendPUTRequest(uriBase, "/dumpToLog", httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "");
return this;
}
@@ -63,15 +63,15 @@ public ProxyClient dumpToLogAsJava() {
* @param httpRequest the http request that is matched against when deciding what to log if null all requests are logged
*/
public ProxyClient dumpToLogAsJava(HttpRequest httpRequest) {
- apacheHttpClient.sendPUTRequest(uri, "/dumpToLog?type=java", httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "");
+ apacheHttpClient.sendPUTRequest(uriBase, "/dumpToLog?type=java", httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "");
return this;
}
/**
* Reset the proxy by clearing recorded requests
*/
public ProxyClient reset() {
- apacheHttpClient.sendPUTRequest(uri, "/reset", "");
+ apacheHttpClient.sendPUTRequest(uriBase, "/reset", "");
return this;
}
@@ -81,7 +81,7 @@ public ProxyClient reset() {
* @param httpRequest the http request that is matched against when deciding whether to clear recorded requests
*/
public ProxyClient clear(HttpRequest httpRequest) {
- apacheHttpClient.sendPUTRequest(uri, "/clear", httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "");
+ apacheHttpClient.sendPUTRequest(uriBase, "/clear", httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "");
return this;
}
@@ -137,7 +137,7 @@ public ProxyClient verify(HttpRequest httpRequest, Times times) throws Assertion
}
private String butFoundAssertionErrorMessage() {
- String allRequests = apacheHttpClient.sendPUTRequest(uri, "/retrieve", "");
+ String allRequests = apacheHttpClient.sendPUTRequest(uriBase, "/retrieve", "");
return " but " + (StringUtils.isNotEmpty(allRequests) ? "only found " + allRequests : "found no requests");
}
@@ -148,7 +148,7 @@ private String butFoundAssertionErrorMessage() {
* @return an array of all expectations that have been recorded by the proxy
*/
public Expectation[] retrieveAsExpectations(HttpRequest httpRequest) {
- return expectationSerializer.deserializeArray(apacheHttpClient.sendPUTRequest(uri, "/retrieve", (httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "")));
+ return expectationSerializer.deserializeArray(apacheHttpClient.sendPUTRequest(uriBase, "/retrieve", (httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "")));
}
/**
@@ -158,6 +158,6 @@ private String butFoundAssertionErrorMessage() {
* @return a JSON array of all expectations that have been recorded by the proxy
*/
public String retrieveAsJSON(HttpRequest httpRequest) {
- return apacheHttpClient.sendPUTRequest(uri, "/retrieve", (httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : ""));
+ return apacheHttpClient.sendPUTRequest(uriBase, "/retrieve", (httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : ""));
}
}
@@ -136,7 +136,82 @@ public MockServerClient clear(HttpRequest httpRequest) {
}
protected void sendExpectation(Expectation expectation) {
- apacheHttpClient.sendPUTRequest(uriBase, "/", expectation != null ? expectationSerializer.serialize(expectation) : "");
+ apacheHttpClient.sendPUTRequest(uriBase, "/expectation", expectation != null ? expectationSerializer.serialize(expectation) : "");
}
+ /**
+ * Verify a request has been sent for example:
+ *
+ * mockServerClient
+ * .verify(
+ * request()
+ * .withPath("/some_path")
+ * .withBody("some_request_body")
+ * );
+ *
+ * @param httpRequest the http request that must be matched for this verification to pass
+ * @throws AssertionError if the request has not been found
+ */
+ public MockServerClient verify(HttpRequest httpRequest) throws AssertionError {
+ return verify(httpRequest, org.mockserver.client.proxy.Times.atLeast(1));
+ }
+
+ /**
+ * Verify a request has been sent for example:
+ *
+ * mockServerClient
+ * .verify(
+ * request()
+ * .withPath("/some_path")
+ * .withBody("some_request_body"),
+ * Times.exactly(3)
+ * );
+ *
+ * @param httpRequest the http request that must be matched for this verification to pass
+ * @param times the number of times this request must be matched
+ * @throws AssertionError if the request has not been found
+ */
+ public MockServerClient verify(HttpRequest httpRequest, org.mockserver.client.proxy.Times times) throws AssertionError {
+ if (httpRequest == null) throw new IllegalArgumentException("verify(HttpRequest) requires a non null HttpRequest object");
+
+ Expectation[] expectations = retrieveAsExpectations(httpRequest);
+ if (expectations == null) {
+ throw new AssertionError("Expected " + httpRequestSerializer.serialize(httpRequest) + butFoundAssertionErrorMessage());
+ }
+ if (times.isExact()) {
+ if (expectations.length != times.getCount()) {
+ throw new AssertionError("Expected " + httpRequestSerializer.serialize(httpRequest) + butFoundAssertionErrorMessage());
+ }
+ } else {
+ if (expectations.length < times.getCount()) {
+ throw new AssertionError("Expected " + httpRequestSerializer.serialize(httpRequest) + butFoundAssertionErrorMessage());
+ }
+ }
+ return this;
+ }
+
+ private String butFoundAssertionErrorMessage() {
+ String allRequests = apacheHttpClient.sendPUTRequest(uriBase, "/retrieve", "");
+ return " but " + (StringUtils.isNotEmpty(allRequests) ? "only found " + allRequests : "found no requests");
+ }
+
+ /**
+ * Retrieve the recorded requests that match the httpRequest parameter as expectations, use null for the parameter to retrieve all requests
+ *
+ * @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 recorded by the proxy
+ */
+ public Expectation[] retrieveAsExpectations(HttpRequest httpRequest) {
+ return expectationSerializer.deserializeArray(apacheHttpClient.sendPUTRequest(uriBase, "/retrieve", (httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "")));
+ }
+
+ /**
+ * Retrieve the recorded requests that match the httpRequest parameter as a JSON array, use null for the parameter to retrieve all requests
+ *
+ * @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 a JSON array of all expectations that have been recorded by the proxy
+ */
+ public String retrieveAsJSON(HttpRequest httpRequest) {
+ return apacheHttpClient.sendPUTRequest(uriBase, "/retrieve", (httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : ""));
+ }
}
Oops, something went wrong.

0 comments on commit 40ecdf5

Please sign in to comment.