From 51e95249ff2c947a08977df5d95c38b083f0a38b Mon Sep 17 00:00:00 2001 From: Hans Joachim Desserud Date: Sun, 5 Mar 2017 11:15:36 +0100 Subject: [PATCH 1/8] Upgrade to latest version of zjsonpatch --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 66024a28c2..5dd436c703 100644 --- a/build.gradle +++ b/build.gradle @@ -61,7 +61,7 @@ dependencies { exclude group: "org.hamcrest", module: "hamcrest-core" } compile 'org.apache.commons:commons-lang3:3.4' - compile 'com.flipkart.zjsonpatch:zjsonpatch:0.2.1' + compile 'com.flipkart.zjsonpatch:zjsonpatch:0.3.0' compile 'com.github.jknack:handlebars:4.0.6', { exclude group: 'org.mozilla', module: 'rhino' } From 4fc469ed48b61fa9b264e5da15ce49434535beb0 Mon Sep 17 00:00:00 2001 From: Mason Malone Date: Sun, 2 Apr 2017 11:58:58 -0700 Subject: [PATCH 2/8] Add response templating for proxyBaseUrl This extends the response templating extension to work with proxy URLs. This is very useful when using the proxy/intercept pattern with multiple different API providers, especially when the endpoints aren't known ahead of time. The browser proxy mode is perhaps better suited to this use case, but it's not an option for HTTPS-enabled APIs due to the lack of SSL support. --- docs-v2/_docs/response-templating.md | 33 ++++++++++++++++++- .../ResponseTemplateTransformer.java | 6 ++++ .../ResponseTemplateTransformerTest.java | 13 ++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/docs-v2/_docs/response-templating.md b/docs-v2/_docs/response-templating.md index ff54d43dcc..3f2603cbf3 100644 --- a/docs-v2/_docs/response-templating.md +++ b/docs-v2/_docs/response-templating.md @@ -5,7 +5,7 @@ toc_rank: 71 description: Generating dynamic responses using Handlebars templates --- -Response headers and bodies can optionally be rendered using [Handlebars templates](http://handlebarsjs.com/). This enables attributes of the request +Response headers and bodies, as well as proxy URLs, can optionally be rendered using [Handlebars templates](http://handlebarsjs.com/). This enables attributes of the request to be used in generating the response e.g. to pass the value of a request ID header as a response header or render an identifier from part of the URL in the response body. @@ -54,6 +54,37 @@ wm.stubFor(get(urlPathEqualTo("/templated")) ``` {% endraw %} +## Proxying + +Templating also works when defining proxy URLs, e.g. + +### Java + +{% raw %} +```java +wm.stubFor(get(urlPathEqualTo("/templated")) + .willReturn(aResponse() + .proxiedFrom("{{request.headers.X-WM-Proxy-Url}}") + .withTransformers("response-template"))); +``` +{% endraw %} + + +{% raw %} +### JSON +```json +{ + "request": { + "urlPath": "/templated" + }, + "response": { + "proxyBaseUrl": "{{request.headers.X-WM-Proxy-Url}}", + "transformers": ["response-template"] + } +} +``` +{% endraw %} + ## The request model The model of the request is supplied to the header and body templates. The following request attributes are available: diff --git a/src/main/java/com/github/tomakehurst/wiremock/extension/responsetemplating/ResponseTemplateTransformer.java b/src/main/java/com/github/tomakehurst/wiremock/extension/responsetemplating/ResponseTemplateTransformer.java index 5f313bac4b..0a644c425b 100644 --- a/src/main/java/com/github/tomakehurst/wiremock/extension/responsetemplating/ResponseTemplateTransformer.java +++ b/src/main/java/com/github/tomakehurst/wiremock/extension/responsetemplating/ResponseTemplateTransformer.java @@ -94,6 +94,12 @@ public String apply(String input) { newResponseDefBuilder.withHeaders(new HttpHeaders(newResponseHeaders)); } + if (responseDefinition.getProxyBaseUrl() != null) { + Template proxyBaseUrlTemplate = uncheckedCompileTemplate(responseDefinition.getProxyBaseUrl()); + String newProxyBaseUrl = uncheckedApplyTemplate(proxyBaseUrlTemplate, model); + newResponseDefBuilder.proxiedFrom(newProxyBaseUrl); + } + return newResponseDefBuilder.build(); } diff --git a/src/test/java/com/github/tomakehurst/wiremock/extension/responsetemplating/ResponseTemplateTransformerTest.java b/src/test/java/com/github/tomakehurst/wiremock/extension/responsetemplating/ResponseTemplateTransformerTest.java index e8aed52def..1a377ada92 100644 --- a/src/test/java/com/github/tomakehurst/wiremock/extension/responsetemplating/ResponseTemplateTransformerTest.java +++ b/src/test/java/com/github/tomakehurst/wiremock/extension/responsetemplating/ResponseTemplateTransformerTest.java @@ -227,6 +227,19 @@ public Object apply(String context, Options options) throws IOException { assertThat(transformedResponseDef.getBody(), is("5")); } + @Test + public void proxyBaseUrl() { + ResponseDefinition transformedResponseDef = transform(mockRequest() + .url("/things") + .header("X-WM-Uri", "http://localhost:8000"), + aResponse().proxiedFrom("{{request.headers.X-WM-Uri}}") + ); + + assertThat(transformedResponseDef.getProxyBaseUrl(), is( + "http://localhost:8000" + )); + } + private ResponseDefinition transform(Request request, ResponseDefinitionBuilder responseDefinitionBuilder) { return transformer.transform( request, From 77743d03594da1b00d83838b29ffb21aea4b9bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Mysl=C3=ADk?= Date: Thu, 6 Apr 2017 13:23:28 +0200 Subject: [PATCH 3/8] #643 Prevent null pointer exception when getting MimeTypePart --- .../tomakehurst/wiremock/http/ContentTypeHeader.java | 2 +- .../tomakehurst/wiremock/http/ContentTypeHeaderTest.java | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/tomakehurst/wiremock/http/ContentTypeHeader.java b/src/main/java/com/github/tomakehurst/wiremock/http/ContentTypeHeader.java index a46d7c1afd..4ad1986774 100644 --- a/src/main/java/com/github/tomakehurst/wiremock/http/ContentTypeHeader.java +++ b/src/main/java/com/github/tomakehurst/wiremock/http/ContentTypeHeader.java @@ -41,7 +41,7 @@ public ContentTypeHeader or(String stringValue) { } public String mimeTypePart() { - return parts[0]; + return parts != null ? parts[0] : null; } public Optional encodingPart() { diff --git a/src/test/java/com/github/tomakehurst/wiremock/http/ContentTypeHeaderTest.java b/src/test/java/com/github/tomakehurst/wiremock/http/ContentTypeHeaderTest.java index df13e0c93e..47c871d365 100644 --- a/src/test/java/com/github/tomakehurst/wiremock/http/ContentTypeHeaderTest.java +++ b/src/test/java/com/github/tomakehurst/wiremock/http/ContentTypeHeaderTest.java @@ -24,6 +24,7 @@ import org.junit.runner.RunWith; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -93,4 +94,10 @@ public void throwsExceptionOnAttemptToSetNullHeaderValue() { request.contentTypeHeader(); } + + @Test + public void returnsNullFromMimeTypePartWhenContentTypeIsAbsent() { + ContentTypeHeader header = ContentTypeHeader.absent(); + assertThat(header.mimeTypePart(), is(nullValue())); + } } From 211a0e271c6b20f3b25579cc7bf27b1ea47a06ed Mon Sep 17 00:00:00 2001 From: Tom Akehurst Date: Thu, 6 Apr 2017 14:16:33 +0100 Subject: [PATCH 4/8] Attempt to make post serve action tests less flakey by extending timeouts. Switched away from countdown latch to async test framework code. --- .../PostServeActionExtensionTest.java | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/test/java/com/github/tomakehurst/wiremock/PostServeActionExtensionTest.java b/src/test/java/com/github/tomakehurst/wiremock/PostServeActionExtensionTest.java index b58b464707..21a310895d 100644 --- a/src/test/java/com/github/tomakehurst/wiremock/PostServeActionExtensionTest.java +++ b/src/test/java/com/github/tomakehurst/wiremock/PostServeActionExtensionTest.java @@ -18,7 +18,6 @@ import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; import static com.github.tomakehurst.wiremock.PostServeActionExtensionTest.CounterNameParameter.counterNameParameter; @@ -27,9 +26,8 @@ import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static com.github.tomakehurst.wiremock.http.RequestMethod.GET; import static com.google.common.base.MoreObjects.firstNonNull; -import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; import static org.awaitility.Awaitility.await; -import static org.awaitility.Duration.ONE_SECOND; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -70,19 +68,10 @@ public void triggersActionWhenAppliedToAStubMapping() throws Exception { client.get("/count-me"); await() - .atMost(ONE_SECOND) + .atMost(5, SECONDS) .until(getContent("/__admin/named-counter/things"), is("4")); } - private Callable getContent(final String url) { - return new Callable() { - @Override - public String call() throws Exception { - return client.get(url).content(); - } - }; - } - @Test public void continuesWithNoEffectIfANonExistentActionIsReferenced() { initWithOptions(options().dynamicPort()); @@ -101,7 +90,6 @@ public void continuesWithNoEffectIfANonExistentActionIsReferenced() { @Test public void providesServeEventWithResponseFieldPopulated() throws InterruptedException { final AtomicInteger finalStatus = new AtomicInteger(); - final CountDownLatch countDownLatch = new CountDownLatch(1); initWithOptions(options().dynamicPort().extensions(new PostServeAction() { @Override public String getName() { @@ -111,21 +99,39 @@ public String getName() { @Override public void doGlobalAction(ServeEvent serveEvent, Admin admin) { if (serveEvent.getResponse() != null) { - countDownLatch.countDown(); finalStatus.set(serveEvent.getResponse().getStatus()); } } })); wm.stubFor(get(urlPathEqualTo("/response-status")) - .willReturn(aResponse().withStatus(418)) + .willReturn(aResponse() + .withStatus(418)) ); client.get("/response-status"); - countDownLatch.await(1500, MILLISECONDS); + await() + .atMost(5, SECONDS) + .until(getValue(finalStatus), is(418)); + } - assertThat(finalStatus.get(), is(418)); + private Callable getValue(final AtomicInteger value) { + return new Callable() { + @Override + public Integer call() throws Exception { + return value.get(); + } + }; + } + + private Callable getContent(final String url) { + return new Callable() { + @Override + public String call() throws Exception { + return client.get(url).content(); + } + }; } public static class NamedCounterAction extends PostServeAction implements AdminApiExtension { From 08a817936df1b2836f9915e06e8ce60cc2ec40c8 Mon Sep 17 00:00:00 2001 From: Tom Akehurst Date: Thu, 6 Apr 2017 14:53:01 +0100 Subject: [PATCH 5/8] Added shading of Handlebars and Antlr to the build --- build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.gradle b/build.gradle index 5dd436c703..3d5562e71c 100644 --- a/build.gradle +++ b/build.gradle @@ -151,6 +151,8 @@ shadowJar { relocate "org.custommonkey", "wiremock.org.custommonkey" relocate "com.flipkart", "wiremock.com.flipkart" relocate "net.sf", "wiremock.net.sf" + relocate "com.github.jknack", "wiremock.com.github.jknack" + relocate "org.antlr", "wiremock.org.antlr" dependencies { exclude(dependency('junit:junit')) From da0cf4428aa625d0e2ca53a550b907b612cdcacc Mon Sep 17 00:00:00 2001 From: Tom Akehurst Date: Thu, 6 Apr 2017 15:07:26 +0100 Subject: [PATCH 6/8] Fixed #598 - incorrectly documented default thread count --- docs-v2/_docs/running-standalone.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs-v2/_docs/running-standalone.md b/docs-v2/_docs/running-standalone.md index 716fd9d2ed..bb725a21ea 100644 --- a/docs-v2/_docs/running-standalone.md +++ b/docs-v2/_docs/running-standalone.md @@ -82,7 +82,7 @@ exhausting the heap. The `--record-mappings` option isn't available if this one is specified. `--container-threads`: The number of threads created for incoming -requests. Defaults to 200. +requests. Defaults to 10. `--max-request-journal-entries`: Set maximum number of entries in request journal (if enabled). When this limit is reached oldest entries From cc73efbef41e66bb1e583a3a3b8962bd5bccab8f Mon Sep 17 00:00:00 2001 From: Joe Klauza Date: Sun, 9 Apr 2017 07:28:32 -0400 Subject: [PATCH 7/8] Add default request config check stale conn (#628) * added test for couchbase * Now we check for stale connections and don't try to reuse them * fix test * Removed inconvenient Couchbase test --- .../com/github/tomakehurst/wiremock/http/HttpClientFactory.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/github/tomakehurst/wiremock/http/HttpClientFactory.java b/src/main/java/com/github/tomakehurst/wiremock/http/HttpClientFactory.java index d215cf1a1b..313baaeed9 100644 --- a/src/main/java/com/github/tomakehurst/wiremock/http/HttpClientFactory.java +++ b/src/main/java/com/github/tomakehurst/wiremock/http/HttpClientFactory.java @@ -18,6 +18,7 @@ import com.github.tomakehurst.wiremock.common.KeyStoreSettings; import com.github.tomakehurst.wiremock.common.ProxySettings; import org.apache.http.HttpHost; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.*; import org.apache.http.config.SocketConfig; import org.apache.http.conn.ssl.AllowAllHostnameVerifier; @@ -52,6 +53,7 @@ public static CloseableHttpClient createClient( .disableRedirectHandling() .disableContentCompression() .setMaxConnTotal(maxConnections) + .setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()) .setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(timeoutMilliseconds).build()) .useSystemProperties() .setHostnameVerifier(new AllowAllHostnameVerifier()); From 9dcbfd02bcca05714f0bda73ce542892c3af0fa8 Mon Sep 17 00:00:00 2001 From: Jon Hinks Date: Mon, 10 Apr 2017 11:11:47 +0100 Subject: [PATCH 8/8] Suggested API doc change to fix #581 --- src/main/resources/raml/wiremock-admin-api.raml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/resources/raml/wiremock-admin-api.raml b/src/main/resources/raml/wiremock-admin-api.raml index a27b36e2ed..66730ebe16 100644 --- a/src/main/resources/raml/wiremock-admin-api.raml +++ b/src/main/resources/raml/wiremock-admin-api.raml @@ -162,6 +162,14 @@ schemas: body: application/json: example: !include examples/serve-events.example.json + delete: + description: Delete all received requests + responses: + 200: + description: Successfully deleted + body: + application/json: + example: !include examples/empty.example.json /{requestId}: