aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-feed-client
diff options
context:
space:
mode:
authorjonmv <venstad@gmail.com>2023-01-04 08:24:03 +0100
committerjonmv <venstad@gmail.com>2023-01-04 08:24:03 +0100
commitc9746a990c0daf8103dc7a09ab0d80ee87565a3b (patch)
treec1b5b2122d3c33deb8df402c3c5470d284e4c3e8 /vespa-feed-client
parentb7933275fab6d14da23cac136560c17c5bc16ffb (diff)
Add new test
Diffstat (limited to 'vespa-feed-client')
-rw-r--r--vespa-feed-client/src/test/java/ai/vespa/feed/client/impl/ApacheClusterTest.java66
-rw-r--r--vespa-feed-client/src/test/java/ai/vespa/feed/client/impl/WireMockExtension.java42
2 files changed, 108 insertions, 0 deletions
diff --git a/vespa-feed-client/src/test/java/ai/vespa/feed/client/impl/ApacheClusterTest.java b/vespa-feed-client/src/test/java/ai/vespa/feed/client/impl/ApacheClusterTest.java
new file mode 100644
index 00000000000..e665a9f0693
--- /dev/null
+++ b/vespa-feed-client/src/test/java/ai/vespa/feed/client/impl/ApacheClusterTest.java
@@ -0,0 +1,66 @@
+package ai.vespa.feed.client.impl;
+
+import ai.vespa.feed.client.HttpResponse;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.URI;
+import java.time.Duration;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.zip.GZIPOutputStream;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.any;
+import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
+import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
+import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
+import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class ApacheClusterTest {
+
+ @RegisterExtension
+ final WireMockExtension server = new WireMockExtension();
+
+ @Test
+ void testClient() throws IOException, ExecutionException, InterruptedException, TimeoutException {
+ try (ApacheCluster cluster = new ApacheCluster(new FeedClientBuilderImpl(List.of(URI.create("http://localhost:" + server.port())))
+ .setGzipRequests(true))) {
+ server.stubFor(any(anyUrl()))
+ .setResponse(okJson("{}").build());
+
+ CompletableFuture<HttpResponse> vessel = new CompletableFuture<>();
+ cluster.dispatch(new HttpRequest("POST",
+ "/path",
+ Map.of("name1", () -> "value1",
+ "name2", () -> "value2"),
+ "content".getBytes(UTF_8),
+ Duration.ofSeconds(1)),
+ vessel);
+ HttpResponse response = vessel.get(5, TimeUnit.SECONDS);
+ assertEquals("{}", new String(response.body(), UTF_8));
+ assertEquals(200, response.code());
+
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ try (OutputStream zip = new GZIPOutputStream(buffer)) { zip.write("content".getBytes(UTF_8)); }
+ server.verify(1, anyRequestedFor(anyUrl()));
+ server.verify(1, postRequestedFor(urlEqualTo("/path")).withHeader("name1", equalTo("value1"))
+ .withHeader("name2", equalTo("value2"))
+ .withHeader("Content-Type", equalTo("application/json; charset=UTF-8"))
+ .withHeader("Content-Encoding", equalTo("gzip"))
+ .withRequestBody(equalTo("content")));
+ server.resetRequests();
+ }
+ }
+
+}
diff --git a/vespa-feed-client/src/test/java/ai/vespa/feed/client/impl/WireMockExtension.java b/vespa-feed-client/src/test/java/ai/vespa/feed/client/impl/WireMockExtension.java
new file mode 100644
index 00000000000..ef61213889b
--- /dev/null
+++ b/vespa-feed-client/src/test/java/ai/vespa/feed/client/impl/WireMockExtension.java
@@ -0,0 +1,42 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package ai.vespa.feed.client.impl;
+
+import com.github.tomakehurst.wiremock.WireMockServer;
+import com.github.tomakehurst.wiremock.core.Options;
+import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+/**
+ * Allows wiremock to be used as a JUnit 5 extension, like
+ * <pre>
+ *
+ * &#64RegisterExtension
+ * WireMockExtension mockServer1 = new WireMockExtension();
+ * </pre>
+ */
+public class WireMockExtension extends WireMockServer implements BeforeEachCallback, AfterEachCallback {
+
+ public WireMockExtension() {
+ this(WireMockConfiguration.options()
+ .dynamicPort()
+ .dynamicHttpsPort());
+ }
+
+ public WireMockExtension(Options options) {
+ super(options);
+ }
+
+ @Override
+ public void beforeEach(ExtensionContext extensionContext) {
+ start();
+ }
+
+ @Override
+ public void afterEach(ExtensionContext extensionContext) {
+ stop();
+ resetAll();
+ }
+
+}